How to Use Break or Continue Within for Loop in Twig Template

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

This can be nearly done by setting a new variable as a flag to break iterating:

{% set break = false %}
{% for post in posts if not break %}
<h2>{{ post.heading }}</h2>
{% if post.id == 10 %}
{% set break = true %}
{% endif %}
{% endfor %}

An uglier, but working example for continue:

{% set continue = false %}
{% for post in posts %}
{% if post.id == 10 %}
{% set continue = true %}
{% endif %}
{% if not continue %}
<h2>{{ post.heading }}</h2>
{% endif %}
{% if continue %}
{% set continue = false %}
{% endif %}
{% endfor %}

But there is no performance profit, only similar behaviour to the built-in break and continue statements like in flat PHP.

How to stop or break free from a Twig loop?

You could do something like this, in order to simulate the pattern:

{% set breakLoop = false %}

{% for value in array if breakLoop == false %}
{% if value == 'somethingElse' %}
{% breakLoop = true %}
{% endif %}

{% if value != 'something' and breakLoop == false %}

{{ value }}
{% endif %}
{% endfor %}

Just wrap the code inside the condition to not continue.

For breaking use a variable visible from outside the for loop.

You could also write your own, custom for loop, I guess.

Twig 3: Break in a loop

Try this code. It's working on the twig <= 2 version.

    {% set break = false %}
{% set numbers = [2,2,3,1,3] %}
{% for number in numbers if not break %}
- {{ number }} <br/>
{% if number == 3 %}
{% set break = true %}
{% endif %}
{% endfor %}

But in Twig 3, it's not working. You can try the below code it's working for twig 3.

        {% set break = false %}
{% set numbers = [2,2,3,1,3] %}
{% for number in numbers %}
{% if break == false %}
- {{ number }} <br/>
{% if number == 3 %}
{% set break = true %}
{% endif %}
{% endif %}
{% endfor %}

I have read the twig 3 document but I can't fine break/continue concept on that.

=> Output

- 2
- 2
- 3

How to break the loop in twig after certain iteration?

The slice filter comes handy for this purpose:

{% for item in items|slice(0, 1) %}
//iterate over each item
//do your logical stuff
{% endfor %}

twig break out of loop and then continue the rest in next div

Try this snipped code:

{% for request in pagination %}

{#
If is a new date, open a new div TAG and dump the data.
If the first element of the collection do not have this info
consider to use loop.index0 as OR operation.
#}
{% if request.isNewDate %}
<div class="row">
{# print and format the date #}
<h2 class="date-heading">{{ request.date }}</h2>
{% endif %}

{# Render data of an element, consider to wrap this data in a single twig and incude #}
<div class="col4">

<h5 class="">{{ request.getGuest.getFirstName }}</h5>
<h6 class="text-transform-none">{{ request.getGuest.getCollege }}<br>
{{ request.getGuest.getDegree }}<br>
({{ request.getGuest.getCourse }})
</h6>
</div>

{# remember to close the div #}
{% if request.isNewDate %}
</div>
{% endif %}

{% endfor %}

Hope this help

Render a form ONLY ONCE inside a for loop in a twig template

Either create a 2nd loop with a flag or use the filter filter.

{% set show_audio_form = false %}
{% for item in field %}
{% if item.audio %}
{% set show_audio_form = true %}
{% endif %}
{% endfor %}
{% if show_audio_form %}{{ audio_options_form }}{% endif %}
{% for item in field %}
... display audio ...
{% endfor %}

{% if items|filter(v => v.audio|default)| length %}
{{ audio_options_form }}
{% endif %}
{% for item in items %}
... display audio ...
{% endfor %}

demo



Related Topics



Leave a reply



Submit