use of enumerate() in Jinja templates

7,822 views
Skip to first unread message

Dmitry Makovey

unread,
Aug 13, 2016, 2:51:32 AM8/13/16
to Ansible Project
Hi,
is there a pattern for implementing loop similar to python:

for (i,name) in enumerate(list_of_items):
   
print i, name



when I try to implement it in Jinja I get error (no enumerate is not available):

{% for (i,name) in enumerate(list_of_items) %}
item
{{ i }} = {{ name }}
{%endfor %}


 what would be the best way of achieving similar result without implementing my own filter?

Dmitry Makovey

unread,
Aug 13, 2016, 2:57:35 AM8/13/16
to Ansible Project
discovered https://groups.google.com/forum/embed/#!topic/pocoo-libs/gDPqQiRBhdg which mentiones loop.index variable. That'll do for me.

Reply all
Reply to author
Forward
0 new messages