Does anyone know how to loop through an array and find the first of an item then break the loop in twig?
Like this
Loop->find 3
- 2
- 2
- 3 - then break loop here
- 1
- 3
Does anyone know how to loop through an array and find the first of an item then break the loop in twig?
Like this
Loop->find 3
- 2
- 2
- 3 - then break loop here
- 1
- 3
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