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
andcontinue
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
Check to See If a String Is Serialized
Accessing Laravel .Env Variables in Blade
Laravel Named Route for Resource Controller
MySQL and PHP - Insert Null Rather Than Empty String
Laravel Advanced Wheres How to Pass Variable into Function
Pull All Images from a Specified Directory and Then Display Them
Issues with PHP 5.3 and Sessions Folder
Detect Exif Orientation and Rotate Image Using Imagemagick
Relative Path in Require_Once Doesn't Work
When Do I Use Static Variables/Functions in PHP
Codeigniter: Best Way to Structure Partial Views
Laravel No Supported Encrypter Found. the Cipher And/Or Key Length Are Invalid
Symfony2 Routing - Route Subdomains
How to Print SQL Statement in Codeigniter Model
Mysqli_Real_Connect(): (Hy000/2002): No Such File or Directory
How Best to Get Someone's 'Rank' from a Scores Table with PHP and MySQL Without Looping