admin管理员组文章数量:1325761
I am working on a flask + jinja2 website which involves plotting some stored markers on a map.
Python code
resultroute['checkpointlist'] = CheckPoint.query.filter_by(route_id=route.code)
return render_template('routes/edit.html',route=resultroute)
Javascript in edit.html
function addExistingMarkers() {
//Individual access to elements
var name0 = '{{route.checkpointlist[0].name}}';
var lat0 = {{route.checkpointlist[0].latitude}};
var long0 = {{route.checkpointlist[0].longitude}};
var marker = new google.maps.Marker({
position: new google.maps.LatLng({{ route.checkpointlist[0].latitude }}, {{ route.checkpointlist[0].longitude }}),
map: map,
title: '{{ route.checkpointlist[0].name }}'
});
//Trying to iterate over the list
{% for checkpoint in route.checkpointlist %}
var lat = checkpoint.latitude;
var long = checkpoint.longitude;
var cpname = checkpoint.name;
var location = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
draggable:true,
title:cpname,
animation: google.maps.Animation.DROP,
position: location,
});
{% end for %}
}
Only one marker is getting placed which is from the individual access of [0] element. But somehow the for loop is not working.
I am working on a flask + jinja2 website which involves plotting some stored markers on a map.
Python code
resultroute['checkpointlist'] = CheckPoint.query.filter_by(route_id=route.code)
return render_template('routes/edit.html',route=resultroute)
Javascript in edit.html
function addExistingMarkers() {
//Individual access to elements
var name0 = '{{route.checkpointlist[0].name}}';
var lat0 = {{route.checkpointlist[0].latitude}};
var long0 = {{route.checkpointlist[0].longitude}};
var marker = new google.maps.Marker({
position: new google.maps.LatLng({{ route.checkpointlist[0].latitude }}, {{ route.checkpointlist[0].longitude }}),
map: map,
title: '{{ route.checkpointlist[0].name }}'
});
//Trying to iterate over the list
{% for checkpoint in route.checkpointlist %}
var lat = checkpoint.latitude;
var long = checkpoint.longitude;
var cpname = checkpoint.name;
var location = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
draggable:true,
title:cpname,
animation: google.maps.Animation.DROP,
position: location,
});
{% end for %}
}
Only one marker is getting placed which is from the individual access of [0] element. But somehow the for loop is not working.
Share Improve this question edited Jun 20, 2015 at 5:14 prasunnair asked Jun 20, 2015 at 5:06 prasunnairprasunnair 923 silver badges12 bronze badges2 Answers
Reset to default 3{% for checkpoint in route.checkpointlist %}
var lat = {{checkpoint.latitude}};
var long = {{checkpoint.longitude}};
var cpname = {{checkpoint.name}};
var location = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
draggable: true,
title: cpname,
animation: google.maps.Animation.DROP,
position: location,
});
{% end for %}
You need to include the double braces when referencing variable in a Jinja template.
Your tips helped me to build my JS function,
but I would like to make an adjustment,
you need to use {% endfor %}
instead of {% end for %}
.
本文标签:
版权声明:本文标题:python - Jinja2 for loop in javascript on a list not working but accessing individual elements works - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191445a2430287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论