3

I want to use break in the Twig template engine.

{% for key, db_staff_language in db_staff_languages %}
  {% for staff_language in model_data.staff_languages %}
    {% if staff_language.id == db_staff_language.id %}
        <option value="{{db_staff_language.id}}" selected="selected">{{db_staff_language.staff_languages_data_translation[0].value}}</option>
    {% else %}
        <option value="{{db_staff_language.id}}">{{db_staff_language.staff_languages_data_translation[0].value}}</option>
    {% endif %}
    {% break %}   {# <-- Not working #}
  {% endfor %}
{% endfor %}

As I couldn't solve the problem with Twig, I also tried other things, like:

-----------------------------------------------
{% autoescape true %}
<?php echo 'test' ?>
{% endautoescape %}
-----------------------------------------------
{% php %}
<?php echo 'test' ?>
{% endphp %}
-----------------------------------------------
{% verbatim %}
<?php echo "test"; ?>
{% endverbatim %}
-----------------------------------------------
{{ raw }}
<?php echo "test"; ?>
{{ endraw }}
-----------------------------------------------

Is there a solution for this problem?

SuStartX
  • 31
  • 1
  • 4
  • Quoting [the documentation](http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition): "Unlike in PHP, it's not possible to break or continue in a loop. You can however filter the sequence during iteration which allows you to skip items." – Marcello Mönkemeyer Jul 15 '15 at 11:29
  • Unlike in PHP, it's not possible to break or continue in a loop. Check if you can adding-a-condition as described [here in the doc](http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition) – Matteo Jul 15 '15 at 11:29
  • 2
    possible duplicate of [How can I use break or continue within for in twig template?](http://stackoverflow.com/questions/21672796/how-can-i-use-break-or-continue-within-for-in-twig-template) – Ron van der Heijden Jul 15 '15 at 14:13

1 Answers1

4

Twig doesn't include a break statement. According to the official manual, the equivalent instruction is the for + if condition.

Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44