检查for循环Twig中的每个项目

时间:2014-10-04 10:50:17

标签: symfony for-loop twig jquery-gmap3

我开始了解Twig,但是如何检查Twig中for循环的第一个,第二个,第三个......项目?

我想为每个地址添加一个gmap,每个地址都有gmap_one,gmap_two,gmap_three,....

    {% for company in companies %}
    <li>
            <p>{{ company.Company() }}</p>
            <p>{{ company.Contactperson() }}</p>
            <p>{{ company.Phone() }}</p>

            <div class="gmap gmap_one">
            <script>
                    var gmap_address_one = {{ company.Address() }};
            </script>
    </li>
    {% endfor %}

1 个答案:

答案 0 :(得分:0)

问题解决了:

{% set gmap = ['one', 'two', 'three'] %}
{% for company in companies %}
<li>
        <p>{{ company.Company() }}</p>
        <p>{{ company.Contactperson() }}</p>
        <p>{{ company.Phone() }}</p>

        <div class="gmap gmap_{{ gmap[ loop.index-1] }}">
        <script>
                var gmap_address_{{ gmap[ loop.index-1] }} = {{ company.Address() }};
        </script>
</li>
{% endfor %}