如何开始循环。
项目名称列表
{% for item in selection %}
<div>{{ item.name }}</div>
{% if loop.index % 4 == 0 %}
<div class="xyz">
<p>Content</p>
</div>
{% endif %}
{% endfor %}
在一个小树枝模板中,显示了每4个项目后用来添加DIV的 。是否有一种方法可以在每个第四项以“#2之后”的位置开始时显示第二个DIV?因此,最后,我得到两个Divs在每个第二个项目之后交替进行交替?
In a twig template showing a list of item names I use
{% for item in selection %}
<div>{{ item.name }}</div>
{% if loop.index % 4 == 0 %}
<div class="xyz">
<p>Content</p>
</div>
{% endif %}
{% endfor %}
to add a div after every 4th item. Is there a way to show a second div after every 4th item starting with the position "after #2"? So in the end I get the two divs alternating after every second item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用过滤器
batch
在树枝中创建重复结构。以下片段将按两个分组,使您更容易交替交替您想要的
demo
It's far easier to use the filter
batch
to create repeating structures in twig.The following snippet will group the items by two, making it easier to alternate the extra div you want
demo