How to increment a variable on a for loop in jinja template?

前端 未结 7 2091
旧时难觅i
旧时难觅i 2020-11-27 12:02

I would like to do something like:

variable p is from test.py which is a list [\'a\',\'b\',\'c\',\'d\']

{% for i in p %}
{{variable++}}
{{variable}}
         


        
7条回答
  •  粉色の甜心
    2020-11-27 12:31

    After 2.10, to solve the scope problem, you can do something like this:

    {% set count = namespace(value=0) %}
    {% for i in p %}
      {{ count.value }}
      {% set count.value = count.value + 1 %}
    {% endfor %}
    

提交回复
热议问题