Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 if statement in vs equals on dict

Tags:

I'm new to Jinja2 and using it as part of Flask. I've got two statements below. The one with "in" works. The one with "equals" isn't. The equals version is getting a syntax error shown below. I'm curious as to why, as the way the equals version is written, to me at least, is easier to read.

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

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

The errors message from jinja2.exceptions.TemplateSyntaxError

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

Thank you.

like image 364
TimothySwieter Avatar asked Mar 06 '14 23:03

TimothySwieter


1 Answers

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

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

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

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

like image 100
Andrew Clark Avatar answered Oct 06 '22 19:10

Andrew Clark