tags:

views:

18

answers:

2

Converting from Django, I'm used to doing something like this:

{% if not var1 %} {% endif %}

and having it work if I didn't put var1 into the context. Jinja2 gives me an undefined error. Is there an easy way to say {% if var1 == None %} or similar?

A: 

Ah. In the Environment setup, we had undefined = StrictUndefined, which prevented undefined values from being set to anything.

from jinja2 import Undefined JINJA2_ENVIRONMENT_OPTIONS = { 'undefined' : Undefined }

fixed it.

freyley
+1  A: 

From the Jinja2 template designer documentation:

{% if variable is defined %}
    value of variable: {{ variable }}
{% else %}
    variable is not defined
{% endif %}
crewbum