我有这个循环从左到右显示六个链接。我想在第四个链接后添加一个中断,但我是 Twig 的新手,我不知道如何添加它。我需要在 For 循环中再有一个循环吗?
{% if contactLinks|length <= 6 %}
{% for link in contactLinks %}
{{ link|raw }}
{% endfor %}
{% elseif contactLinks|length >= 6 %}
{% for link in contactLinks %}
{{ link|raw }}
{% endfor %}
{% endif %}
> Twig 有一个特殊的循环变量,你需要使用它。看这里:
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable
{% for link in contactLinks %}
{{ link|raw }}
{% if loop.index == 4 %}
<br/>
{% endif %}
{% endfor %}
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable