Jinja2 if statement in vs is equal to dict

I am new to Jinja2 and use it as part of Flask. I have two statements below. One with an β€œin” works. One who is "equal" does not. The equals version receives the syntax error shown below. I’m curious why, as it is written, the equal version, it’s at least easier for me to read.

{% if "SN" in P01["type"] %} {% include 'sn.html' %} {% endif %} {% if P01["type"] equals "SN" %} {% include 'sn.html' %} {% endif %} 

Error message from jinja2.exceptions.TemplateSyntaxError

TemplateSyntaxError: expected token 'end of statement block', received 'equals'

Thanks.

+7
python if-statement jinja2
source share
1 answer

In Jinja2, you should use == instead of equals , for example:

 {% if P01["type"] == "SN" %} {% include 'sn.html' %} {% endif %} 

http://jinja.pocoo.org/docs/switching/#conditions

I'm sure this is what you are looking for, but you should notice that it has a different meaning than "SN" in P01["type"] , using in is a subscript test, so for example, "foo" in "foobar" will be True.

+16
source share

All Articles