python 3.x - In a nested for-loop, how can I access the outer loop index in a jinja template? -
{{loop.index}}
correctly dereferences innermost loop. i'm not seeing way identify loop index i'd like, however, if have more 1 loop nested.
yes. part of documentation answers question!
the special loop variable points innermost loop. if it’s desired have access outer loop it’s possible alias it:
<table> {% row in table %} <tr> {% set rowloop = loop %} {% 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
Comments
Post a Comment