'If' expression in jinja2 template

I am trying to write an if expression in a jinja template

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

statement I'm trying to translate into Python

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

This is the error I get:

TemplateSyntaxError: expected token 'name' received by 'string'

+13
python templates jinja2
source share
2 answers

Why a cycle?

You could just do this:

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

When you initially performed string comparisons, you should have used == instead.

+27
source share

We must remember that {% endif %} comes after {% else %} .

So this is an example:

 {% if someTest %} <p> Something is True </p> {% else %} <p> Something is False </p> {% endif %} 
+9
source share

All Articles