Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果循环为 3,我想停止循环。 如果我的数组有 5 条记录,我只需要显示 3 条记录
{% for image in post.images %} {% if loop.index < '3' %} {{ loop.index}} {% endif %} {% endfor %}
所以我只想显示 3 个循环
1 2 3
{% for image in post.images|slice(0, 3) %}
解决了
您可以在 twig 中组合 for 和 if 语句。像下面这样的东西会起作用,但我猜它实际上不会是你最终打印出来的循环索引?
{% for image in post.images if loop.index <= 3 %} {{ loop.index }} {% endfor %}