I have the following Jinja template:
{% set mybool = False %}
{% for thing in things %}
<div class='indent1'>
<ul>
{% if current_user %}
{% if current_user.username == thing['created_by']['username'] %}
{% set mybool = True %}
<li>mybool: {{ mybool }}</li> <!-- prints True -->
<li><a href='#'>Edit</a></li>
{% endif %}
{% endif %}
<li>Flag</li>
</ul>
</div>
<hr />
{% endfor %}
{% if not mybool %}
<!-- always prints this -->
<p>mybool is false!</p>
{% else %}
<p>mybool is true!</p>
{% endif %}
If the condition is met in the for
loop, I'd like to change mybool
to true so I can display mybool is true!
below. However, it looks like the scope of the inner mybool
is limited to the if
statement, so the desired mybool
is never set.
How can I set the "global" mybool
so I can use it in the last if
statement?
EDIT
I've found some suggestions (only the cached page views correctly), but they don't seem to work. Perhaps they're deprecated in Jinja2...
EDIT
Solution provided below. I am still curious why the suggestions above do not work though. Does anyone know for sure that they were deprecated?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…