在树枝中的另一个循环中访问 loop.index [英] access loop.index when within another loop in twig
本文介绍了在树枝中的另一个循环中访问 loop.index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在第二个循环中时如何访问循环的索引?像这样:
How can i access the loop's index when i'm in a second loop? like this:
{% for i in range(0, 3) %}
{% for j in range(0, 9) %}
{{ loop1.index + loop2.index }} // ?
{% endfor %}
{% endfor %}
推荐答案
实际上不需要设置额外的变量.对于两个嵌套循环,twig 提供了所谓的 parent.loop
上下文.
In fact there's no need to set an extra variable. For two nested loops twig provides the so called parent.loop
context.
要访问父项 loop.index
,请执行以下操作:
To access the parents loop.index
do this:
{% for i in range(0, 3) %}
{% for j in range(0, 9) %}
{{ loop.parent.loop.index + loop.index }}
{% endfor %}
{% endfor %}
这两个文档条目应该会有所帮助
Those two documentation entries should be helpful
这篇关于在树枝中的另一个循环中访问 loop.index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文