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 %}
<!-- exists is true -->
{% endif %}
To enable Jinja’s “do” expression-statement extension: e = jinja2.Environment(extensions=["jinja2.ext.do",])