我尝试在 Twig 中根据渲染的元素数量设置不同的类。例如,如果只有一个,如果只有两个,如果只有三个等等,我想在
<ul>
{% for item in items %}
<li>
{% include 'components/list/person.twig' with item %}
</li>
{% endfor %}
</ul>
最佳答案
如果你想知道循环的长度,可以使用预定义的 loop variable这就是所谓的长度:
loop.length: The number of items in the sequence
例如:
{% for user in users %}
{{ loop.index }}/{{ loop.length }} - {{ user.username }}
{% endfor %}
希望这有帮助
关于php - Twig 循环。控制类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42487121/