To use an array as a counter you need to modify your code (and do some hacks!) I will show you the way of doing what you want without extensions.
Ok, the main problem in your code is that when you set
a variable on a jinja statement it keeps its value just in that scope (is like a local variable definition on a function in python) and since jinja doesn't provide a strightforward way of setting outside variables created on the template you need to do some hacks. This magic works using a list:
import jinja2
env = jinja2.Environment()
print env.from_string("""
{%- set counter = [0] -%}
{%- for f in somearray -%}
{%- set _ = counter.append(counter.pop()+1) -%}
{%- endfor -%}
{{counter[0]}}
""").render(somearray=[1,2,3,4])
Steps:
- Create a list with the value of your counter.
- Use
set _ = ...
to perform some action that will have no effect on your template (just setting the varibale _
to None
which is almost nothing in your context at least you decide you need a variable called _
).
- Use the combination
append
/pop
to change the value of the counter (this works in jinja since it allows calling functions on variables on a set statement)
- Use the value in the list whenever you need the value of the counter.
And if you wonder what is the output of previous code here it comes:
4
Hope it helps.