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

后端 未结 1 1422
暖寄归人
暖寄归人 2021-01-24 10:43

I have a twig template to display audio options (Auto play and Continuos play) as shown in the screen shot: Click to see the screen shot of the page

The following is th

相关标签:
1条回答
  • 2021-01-24 11:29

    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

    0 讨论(0)
提交回复
热议问题