简体   繁体   中英

How to “break” a loop in Twig?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM