Dwa*_*ton 3 php templating templating-engine twig
我正在开发一个循环100次重复数据的项目.每隔8个帖子我都会插入一个广告块,因为我使用索引值对每个块输出进行编号,我需要从变量中减去,因为广告块没有编号.所以我现在面临的问题如下.
Block #1
Block #2
Block #3
Block #4
Block #5
Block #6
Block #7
Advertisement Block
Block #9
Run Code Online (Sandbox Code Playgroud)
因为它将广告块计为索引的一次迭代,所以当它应该是8时,跟随它的具有数字的块现在是9.有没有办法增加变量然后从中减去1的值广告块每次显示?
在标准的PHP中,我可以轻松地做到这一点,但是对于Twig,我已经尝试了一些事情,但我不知道我能做些什么.
如果我理解正确,你可以这样做:
{% for foo in bar %}
{% if (loop.index % 8 == 0 and loop.index > 0) %}
{# You advertisement here #}
{% endif %}
{# Your standard block here #}
<p>This is block #{{ loop.index + 1 + loop.index // 8 }}</p>
{% endfor %}