简体   繁体   中英

Variable not found in Symfony Twig

I am new to the Symfony framework. What I am trying to do is to fetch the data from database and display them in Google Map. Here is my controller class:

//removed code

However, when I tried to run it, it says 'Variable "locations" does not exist.' . I followed this Tutorial . I not sure why the locations is not declared since I already did it in Controller class.

Sorry but I am new to Symfony. Any helps will be appreciated! Thanks!

If you want to access variable in script use it like this.

<script>
{% for location in locations %}
    var map;
    function initMap() {
        var myLatLng = {lat: '{{ location.latitude }}', lng: '{{ location.latitude }}' };
        map = new google.maps.Map(document.getElementById('map'), {
            center: myLatLng,
            zoom: 10
        });

        var marker = new google.maps.Marker({
           position: myLatLng,
           map: map,
           title: 'Hello World!'
       });
   }
{% endfor %}
</script>

No need to repeat script tag in loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM