'if' statement in jinja2 template

Luisito picture Luisito · Nov 15, 2016 · Viewed 137.7k times · Source

I'm trying to write an if statement in jinja template

{% for key in data %}
    {% if key is 'priority' %}
        <p>('Priority: ' + str(data[key])</p>
    {% endif %}
{% endfor %}

the statement I'm trying to translate in python is

if key == priority:
    print(print('Priority: ' + str(data[key]))

This is the error i'm getting:

TemplateSyntaxError: expected token 'name', got 'string'

Answer

Nick picture Nick · Nov 15, 2016

Why the loop?

You could simply do this:

{% if 'priority' in data %}
    <p>Priority: {{ data['priority'] }}</p>
{% endif %}

When you were originally doing your string comparison, you should have used == instead.