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

前端 未结 7 2090
旧时难觅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:32

    You could use loop.index:

    {% for i in p %}
      {{ loop.index }}
    {% endfor %}
    

    Check the template designer documentation.

    In more recent versions, due to scoping rules, the following would not work:

    {% set count = 1 %}
    {% for i in p %}
      {{ count }}
      {% set count = count + 1 %}
    {% endfor %}
    

提交回复
热议问题