Can a Jinja variable's scope extend beyond in an inner block?

后端 未结 8 928
夕颜
夕颜 2020-11-27 06:07

I have the following Jinja template:

{% set mybool = False %}
{% for thing in things %}
    
    {%
8条回答
  •  隐瞒了意图╮
    2020-11-27 06:39

    Answer to a related question: I wanted to have a global counter of the number of times I entered a certain if-block in the template, and ended up with the below.

    At the top of the template:

    {% set counter = ['1'] %}
    

    In the if-block I want to count:

    {% if counter.append('1') %}{% endif %}
    

    When displaying the count:

    {{ counter|length }}
    

    The string '1' can be replaced with any string or digit, I believe. It is still a hack, but not a very large one.

提交回复
热议问题