Loops 细枝周期循环中的使用条件
如果循环为3,我想停止循环。Loops 细枝周期循环中的使用条件,loops,symfony,twig,Loops,Symfony,Twig,如果循环为3,我想停止循环。 如果我的数组有5条记录,我只需要显示3条记录 {% for image in post.images %} {% if loop.index < '3' %} {{ loop.index}} {% endif %} {% endfor %} 已解决您可以在twig中组合for和if语句。下面类似的东西会起作用,但我猜它实际上不会是最终打印出来的循环索引 {% for image in post.images if loop.index
如果我的数组有5条记录,我只需要显示3条记录
{% for image in post.images %}
{% if loop.index < '3' %}
{{ loop.index}}
{% endif %}
{% endfor %}
已解决您可以在twig中组合for和if语句。下面类似的东西会起作用,但我猜它实际上不会是最终打印出来的循环索引
{% for image in post.images if loop.index <= 3 %}
{{ loop.index }}
{% endfor %}
{%用于post.images if loop.index中的图像
{% for image in post.images|slice(0, 3) %}
{% for image in post.images if loop.index <= 3 %}
{{ loop.index }}
{% endfor %}