增加树枝中的循环索引

Increase loop index in twig

下面是我的代码。我想增加内部循环之间的 J 循环索引,所以我增加了 J 变量,但它不起作用。

`{% for j in 0..(products|length-1) %}
{% for f in 0..(rows-1) %}
{% set j = j + 1 %}
{% endfor %}
{% endfor %}`

有没有其他方法可以增加循环索引?

由于循环的编译方式

,无法更改 twig 的循环索引

{% for i in 1..5 %} 例如被编译为

$context['_seq'] = twig_ensure_traversable(range(1, 5));
foreach ($context['_seq'] as $context["_key"] => $context["i"]) {
    //..
}

我确实有另一种方法可以让你用 twig

来解决这个问题
{% set rows = 2 %}
{% set items = ((products|length) / rows) | round %}

{% for product in products %}
    {% if loop.index0 % items == 0 %}
<div class="row">
    {% endif %}
    <div class="product">
        {{ product }}
    </div>
    {% if loop.index % items == 0 or loop.last %}
</div>
    {% endif %}
{% endfor %}