python - Jinja2 for loop in javascript on a list not working but accessing individual elements works -
i working on flask + jinja2 website involves plotting stored markers on 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 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 iterate on list {% 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 %} }
only 1 marker getting placed individual access of [0] element. somehow loop not working.
{% 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 %}
you need include double braces when referencing variable in jinja template.
Comments
Post a Comment