How to use loop index in template?

2,935 views
Skip to first unread message

Joe

unread,
Oct 29, 2011, 4:56:59 AM10/29/11
to Tornado Web Server
I have a trouble. I need to know the loop index just like
{{ loop.index }} in jinja2. But I don't know what it is in tornado's
template.
Does anyone know that?

Alek Storm

unread,
Oct 29, 2011, 3:31:30 PM10/29/11
to python-...@googlegroups.com
Instead of:

{% for item in items %}
    value: {{item}}
{% end %}

Do:

{% for i in xrange(len(items)) %}
    index: {{i}}
    value: {{items[i]}}
{% end %}

Roberto Aguilar

unread,
Oct 29, 2011, 3:33:10 PM10/29/11
to python-...@googlegroups.com
using python's enumerate works:

{% for idx, item in enumerate(stuff) %}
{{ idx }} = {{ item }}<br />
{% end %}

-Roberto.

Reply all
Reply to author
Forward
0 new messages