title: How can I make a simple counter with Jinja2 templates?
body: <p>I have two for loops, both alike in dignity. I'd like to have a counter incremented during each inner iteration.</p>
  
  <p>For example, consider this template:</p>
  
  <pre><code>from jinja2 import Template
  
  print Template("""
  {% set count = 0 -%}
  {% for i in 'a', 'b', 'c' -%}
    {% for j in 'x', 'y', 'z' -%}
      i={{i}}, j={{j}}, count={{count}}
      {% set count = count + 1 -%}
    {% endfor -%}
  {% endfor -%}
  """).render()
  </code></pre>
  
  <p>Shouldn't this print <code>count=0</code> through <code>count=8</code>? <strong>Nope, it doesn't.</strong></p>
  
  <pre><code>i=a, j=x, count=0
  i=a, j=y, count=1
  i=a, j=z, count=2
  i=b, j=x, count=0
  i=b, j=y, count=1
  i=b, j=z, count=2
  i=c, j=x, count=0
  i=c, j=y, count=1
  i=c, j=z, count=2
  </code></pre>
  
  <p>What gives?</p>
  
  <p><em>Note:</em> I can't simply save the outer <code>loop</code> variable to calculate the counter because, in my software, the number of inner iterations is variable.</p>