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

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

I have the following Jinja template:

{% set mybool = False %}
{% for thing in things %}
    
    {%
8条回答
  •  伪装坚强ぢ
    2020-11-27 06:37

    One way around this limitation is to enable the "do" expression-statement extension and use an array instead of boolean:

    {% set exists = [] %}
    {% for i in range(5) %}
          {% if True %}
              {% do exists.append(1) %}
          {% endif %}
    {% endfor %}
    {% if exists %}
        
    {% endif %}
    

    To enable Jinja's "do" expression-statement extension: e = jinja2.Environment(extensions=["jinja2.ext.do",])

提交回复
热议问题