在嵌套的 for 循环中,如何访问 jinja 模板中的外循环索引?
In a nested for-loop, how can I access the outer loop index in a jinja template?
{{loop.index}}
正确地解除了对最内层循环的引用。但是,如果我嵌套了多个循环,我没有找到一种方法来确定我想要的循环索引。
是的。这部分文档正好回答了我的问题!
The special loop variable always points to the innermost loop. If it’s
desired to have access to an outer loop it’s possible to alias it:
<table>
{% for row in table %}
<tr>
{% set rowloop = loop %}
{% for cell in row %}
<td id="cell-{{ rowloop.index }}-{{ loop.index }}">{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
http://jinja.pocoo.org/docs/dev/tricks/#accessing-the-parent-loop
{{loop.index}}
正确地解除了对最内层循环的引用。但是,如果我嵌套了多个循环,我没有找到一种方法来确定我想要的循环索引。
是的。这部分文档正好回答了我的问题!
The special loop variable always points to the innermost loop. If it’s desired to have access to an outer loop it’s possible to alias it:
<table>
{% for row in table %}
<tr>
{% set rowloop = loop %}
{% for cell in row %}
<td id="cell-{{ rowloop.index }}-{{ loop.index }}">{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
http://jinja.pocoo.org/docs/dev/tricks/#accessing-the-parent-loop