In Jinja2, how do you test if a variable is undefined?

Jinja2

Jinja2 Problem Overview


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?

Jinja2 Solutions


Solution 1 - Jinja2

From the Jinja2 template designer documentation:

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

Solution 2 - Jinja2

{% 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.

Solution 3 - Jinja2

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

Solution 4 - Jinja2

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 }

Solution 5 - Jinja2

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) -%}

Solution 6 - Jinja2

{% 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)

Solution 7 - Jinja2

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 %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfreyleyView Question on Stackoverflow
Solution 1 - Jinja2GarrettView Answer on Stackoverflow
Solution 2 - Jinja2Carsten AggerView Answer on Stackoverflow
Solution 3 - Jinja2czeraszView Answer on Stackoverflow
Solution 4 - Jinja2freyleyView Answer on Stackoverflow
Solution 5 - Jinja2Lubomir VargaView Answer on Stackoverflow
Solution 6 - Jinja2lee penkmanView Answer on Stackoverflow
Solution 7 - Jinja2velisView Answer on Stackoverflow