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'
Jinja2 being a templating language has no need for wide choice of loop types so we only get for loop. For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python.
A Jinja template doesn't need to have a specific extension: . html , . xml , or any other extension is just fine. A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template.
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.
We need to remember that the {% endif %}
comes after the {% else %}
.
So this is an example:
{% if someTest %} <p> Something is True </p> {% else %} <p> Something is False </p> {% endif %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With