仅在树枝模板的for循环内渲染一次表单 [英] Render a form ONLY ONCE inside a for loop in a twig template

查看:58
本文介绍了仅在树枝模板的for循环内渲染一次表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树枝模板来显示音频选项(自动播放和连续播放),如屏幕截图所示: 单击以查看页面的屏幕截图

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

以下是我的树枝文件中的代码. {{audio_options_form}}会显示带有复选框的表单.如果{{item.audio}}为true,我只希望显示复选框一次.请让我知道我应该进行哪些更改:

The following is the code in my twig file. {{ audio_options_form}} renders the form with the checkboxes.I want to display the check boxes only once if {{ item.audio }} is true. Please let me know what changes should I make:

 <div id="textcontent">
 {% set field = data|slice(2) %}
  {% set field = field|slice(0, -1) %}
<div class="col-md-12">
<div class = "source-box audio">
    {% for item in field %}
     {% if item.audio %}
    {{ audio_options_form }}

      {% if data.play.autoplay == 1 %}
        <audio autoplay controls src= "{{item.audio}}" id = "audio-play">
          Your browser does not support the
          <code>audio</code> element.
        </audio>
      {% elseif data.play.continuousplay == 1 %}
        <audio autoplay controls src= "{{item.audio}}" id = "audio-play" onended="continousPlay('{{data.lastlevel}}')">
          Your browser does not support the
          <code>audio</code> element.
        </audio>
      {% else %}
        <audio controls src= "{{item.audio}}" id = "audio-play">
          Your browser does not support the
          <code>audio</code> element.
        </audio>
      {% endif %}
      <div id="source-controls">
       {# {% if allow_edit == 1 %}
          <a class="edit-text" href="{{ path('heritage_text_manager.editsource', {'sourceid': item.uniqueid}, {'query': {'destination': path('heritage_ui.contentpage', {'textid': textid})}}) }}">{{ 'Edit'|t }}</a>
        {% endif %} #}
        <a class="more use-ajax" data-toggle="modal" data-dialog-type="modal" href="{{ path('heritage_ui.metadata', {'sourceid': item.uniqueid}) }}">{{ 'More'|t }}</a>  
      </div>

    {% endif %}

  {% endfor %}

</div>

推荐答案

要么创建带有标志的第二循环,要么使用过滤器filter.

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 %}

演示

这篇关于仅在树枝模板的for循环内渲染一次表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆