How to output loop.counter in python jinja template?

The Solution to How to output loop.counter in python jinja template? is


The counter variable inside the loop is called loop.index in jinja2.

>>> from jinja2 import Template

>>> s = "{% for element in elements %}{{loop.index}} {% endfor %}"
>>> Template(s).render(elements=["a", "b", "c", "d"])
1 2 3 4

See http://jinja.pocoo.org/docs/templates/ for more.

~ Answered on 2012-08-27 18:08:59


Most Viewed Questions: