How can I use break or continue within for loop in Twig template?

前端 未结 5 1656
眼角桃花
眼角桃花 2020-12-01 00:35

I try to use a simple loop, in my real code this loop is more complex, and I need to break this iteration like:

{% for post in posts %}
    {% i         


        
5条回答
  •  情话喂你
    2020-12-01 01:10

    I have found a good work-around for continue (love the break sample above). Here I do not want to list "agency". In PHP I'd "continue" but in twig, I came up with alternative:

    {% for basename, perms in permsByBasenames %} 
        {% if basename == 'agency' %}
            {# do nothing #}
        {% else %}
            {{ basename }}
        {% endif %}
    {% endfor %}
    

    OR I simply skip it if it doesn't meet my criteria:

    {% for tr in time_reports %}
        {% if not tr.isApproved %}
            .....
        {% endif %}
    {% endfor %}
    

提交回复
热议问题