3

{{loop.index}} correctly dereferences the innermost loop. I'm not seeing a way to identify which loop index I'd like, however, if I have more than one loop nested.

http://jinja.pocoo.org/docs/dev/templates/

tarabyte
  • 17,837
  • 15
  • 76
  • 117
  • 1
    Possible duplicate of [Get loop index of outer loop](https://stackoverflow.com/questions/1567291/get-loop-index-of-outer-loop) – new name Dec 04 '17 at 00:25

1 Answers1

7

YES. This part of the documentation exactly answers my question!

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

tarabyte
  • 17,837
  • 15
  • 76
  • 117