Creating an array within for loop with Twig

前端 未结 2 1793
醉梦人生
醉梦人生 2021-02-14 23:21

I\'m trying to create an array and store values in it within for loop but failed so far. How can I do it with Twig?

I\'ve read these but being new in Twig makes it hard

2条回答
  •  故里飘歌
    2021-02-15 00:07

    Solved by following Vision's suggestion:

    {% set brands = [] %}
    {% for car in cars %}
        {% if car not in brands %}
            {% set brands = brands|merge([car]) %}
        {% endif %}
    {% endfor %}
    
    {% for brand in brands %}
       {{ brand }}
    {% endfor %}
    

    Also I'll take bartek's comment into consideration next time. This was one off.

提交回复
热议问题