【问题标题】:Twig for loop put every 1 element in a new containerTwig for 循环将每个 1 元素放入一个新容器中
【发布时间】:2016-11-01 05:51:07
【问题描述】:

如何在每个循环中使用自己唯一的类名进行渲染

{% for row in rows %}
    {{ row.content }}
{% endfor %}


<div class="item1"> Item 1 </div>
<div class="item2"> Item 2 </div>
<div class="item3"> Item 3 </div>
<div class="item4"> Item 4 </div>

等我如何实现这一点,请帮助谢谢。

或者有没有其他方法可以单独调用for循环中的每个元素???

【问题讨论】:

    标签: symfony drupal twig


    【解决方案1】:

    你可以使用loop.index:

    {% for row in rows %}
    
       <div class="item{{ loop.index }}">{{ row.content }}</div>
    {% endfor %}
    

    【讨论】:

    • 非常感谢...有没有办法我可以单独使用和索引号来提取内容,例如 {{ row.content[0] }} 或 {{ row.content.0} }
    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2014-01-08
    • 2021-08-24
    • 2022-01-21
    • 2013-04-06
    相关资源
    最近更新 更多