312

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?

8 Answers 8

531

From the Jinja2 template designer documentation:

{% if variable is defined %} value of variable: {{ variable }} {% else %} variable is not defined {% endif %} 
Sign up to request clarification or add additional context in comments.

6 Comments

I believe this to be the case but my search terms often do not reflect that.
Additionally, you can use {% if variable is not defined %} to test the inverse.
@dannyman Since 0.2.0
{% if variable is defined and variable %} would also check for emptiness
@Vivek your answer ist the best. Usually I give my variables empty values test: ~ and your solution could recognise that. THANK YOU :)
|
61

{% if variable is defined %} is true if the variable is None.

Since not is None is not allowed, that means that

{% if variable != None %}

is really your only option.

4 Comments

If variable is always evaluated to True when not None, {% if variable != None %} is equivalent to {% if variable %}.
If you want to check for None use lowercase none {% if variable is not none %}
@FelipeAlvarez Do you have a link to documentation on this? Thanks in advance.
@ryanwebjackson This is the builtin test called none.
23

You could also define a variable in a jinja2 template like this:

{% if step is not defined %} {% set step = 1 %} {% endif %} 

And then You can use it like this:

{% if step == 1 %} <div class="col-xs-3 bs-wizard-step active"> {% elif step > 1 %} <div class="col-xs-3 bs-wizard-step complete"> {% else %} <div class="col-xs-3 bs-wizard-step disabled"> {% endif %} 

Otherwise (if You wouldn't use {% set step = 1 %}) the upper code would throw:

UndefinedError: 'step' is undefined 

Comments

19

You can use kind of Jinja Elvis operator

{{ 'OK' if variable is defined else 'N/A' }} 

or additionally check emptiness

{{ 'OK' if (variable is defined and variable) else 'N/A' }} 

Jinja templates - Template Designer Documentation

1 Comment

This is the ternary expression format, but I don't think it counts as the Elvis operator, which is a single operator, and also uses the variable as the output if it is defined. The 'default' function is more like the Elvis operator: {{ variable | default('N/A') }} as Lubomir Varga explains
13

In the Environment setup, we had undefined = StrictUndefined, which prevented undefined values from being set to anything. This fixed it:

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

Comments

10

Consider using default filter if it is what you need. For example:

{% set host = jabber.host | default(default.host) -%} 

or use more fallback values with "hardcoded" one at the end like:

{% set connectTimeout = config.stackowerflow.connect.timeout | default(config.stackowerflow.timeout) | default(config.timeout) | default(42) -%} 

Comments

1

I had an issue like this in Ansible. Ended up having to do a test on both @Garret and @Carsten / @azalea answers, so:

{% if variable is defined and variable %} value of variable: {{ variable }} {% else %} variable is not defined or is falsy {% endif %} 

Comments

0

{% if variable is defined %} works to check if something is undefined.

You can get away with using {% if not var1 %} if you default your variables to False eg

class MainHandler(BaseHandler): def get(self): var1 = self.request.get('var1', False) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.