I'm trying to convert this html
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
Error: {{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}
to jade. I tried
with messages = get_flashed_messages()
if messages
for message in messages
.alert.alert-danger(role="alert")
Error: {{message}}
endfor
endif
endwith
But "messages = get_flashed_messages()" just gets printed to the page. I'm still a bit confused about when to use jade syntax and when to use flask/Jinja2 syntax.
I don't use jade or pyjade much, but I have used jinja2 with python 3.x. Based on the jade and pyjade docs, what you have is not valid jade syntax: Jinja's with statement is not supported in jade nor does pyjade support embedding python code in your jade template (see also for example Using python code in pyjade).
Therefore you have to use a different construct. This should be rather straightforward as the with
merely creates an inner scope for variables of the with statement; outside of this scope, the with
variables are not visible. The "if messages" is required only if messages
can be None
rather than empty list. So try something like this:
if messages
for message in get_flashed_messages()
.alert.alert-danger(role="alert")
Error: {{message}}
I presume you are using python 2.7 because with python 3.4 even the basic examples from the pyjade website don't work.
But your use of endfor
and such makes me think you have not looked at the jade documentation, which says nothing about these keywords and shows clearly that they are not needed. So as they say, "RTFM", it always helps, then your question can be more specific.