1

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.

Dramal
  • 167
  • 2
  • 10

1 Answers1

0

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.

Community
  • 1
  • 1
Oliver
  • 27,510
  • 9
  • 72
  • 103
  • Thanks, this ended up working and helped clear up some of my confusion. I'm not yet comfortable with the idea of Flask working directly with my jade files. For now I'm going with the silly method of using the jade files to compile html code with jinja code inside it. Is this crazy? – Dramal Feb 02 '15 at 01:53
  • @dramal Ok please remember that on SO it is important to close/upvote the answer for others to know answered; else please be specific about what is insufficient (the method you ask about seems more like a separate question). Welcome to SO! – Oliver Feb 02 '15 at 04:08