Jinja2 template variable if None Object set a default value

Jinja2

Jinja2 Problem Overview


How to make a variable in jijna2 default to "" if object is None instead of doing something like this?

      {% if p %}   
		{{ p.User['first_name']}}
      {% else %}
		NONE
	  {%endif %}

So if object p is None I want to default the values of p (first_name and last_name) to "". Basically

nvl(p.User[first_name'], "")

Error receiving:

Error:  jinja2.exceptions.UndefinedError
    UndefinedError: 'None' has no attribute 'User'

Jinja2 Solutions


Solution 1 - Jinja2

Use the none test (not to be confused with Python's None object!):

{% if p is not none %}   
    {{ p.User['first_name'] }}
{% else %}
    NONE
{% endif %}

or:

{{ p.User['first_name'] if p is not none else 'NONE' }}

or if you need an empty string:

{{ p.User['first_name'] if p is not none }}

Solution 2 - Jinja2

{{p.User['first_name'] or 'My default string'}}

Solution 3 - Jinja2

According to docs you can just do:

{{ p|d('', true) }}

Cause None casts to False in a boolean context.

Solution 4 - Jinja2

As addition to other answers, one can write something else if variable is None like this:

{{ variable or '' }}

Solution 5 - Jinja2

Following this doc you can do this that way:

{{ p.User['first_name']|default('NONE') }}

Solution 6 - Jinja2

To avoid throw a exception while "p" or "p.User" is None, you can use:

{{ (p and p.User and p.User['first_name']) or "default_value" }}

Solution 7 - Jinja2

With ChainableUndefined, you can do that.

>>> import jinja2
>>> env = jinja2.Environment(undefined=jinja2.ChainableUndefined)
>>> env.from_string("{{ foo.bar['baz'] | default('val') }}").render()
'val'

source

Solution 8 - Jinja2

As another solution (kind of similar to some previous ones):

{{ ( p is defined and p.User is defined and p.User['first_name'] ) |default("NONE", True) }}

Note the last variable (p.User['first_name']) does not have the if defined test after it.

Solution 9 - Jinja2

if you want to set your own default value instead of None than simply do this

{{your_var|default:'default name'}}

Solution 10 - Jinja2

I usually define an nvl function, and put it in globals and filters.

def nvl(*args):
    for item in args:
        if item is not None:
            return item
    return None

app.jinja_env.globals['nvl'] = nvl
app.jinja_env.filters['nvl'] = nvl

Usage in a template:

<span>Welcome {{ nvl(person.nick, person.name, 'Anonymous') }}<span>

// or 

<span>Welcome {{ person.nick | nvl(person.name, 'Anonymous') }}<span>

Solution 11 - Jinja2

As of Ansible 2.8, you can just use:

{{ p.User['first_name'] }}

See https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.8.html#jinja-undefined-values

Solution 12 - Jinja2

You can simply add "default none" to your variable as the form below mentioned:

{{ your_var | default('NONE', boolean=true) }}

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
QuestionmcdView Question on Stackoverflow
Solution 1 - Jinja2tbicrView Answer on Stackoverflow
Solution 2 - Jinja2TorindoView Answer on Stackoverflow
Solution 3 - Jinja2mitenkaView Answer on Stackoverflow
Solution 4 - Jinja2Ivan BryzzhinView Answer on Stackoverflow
Solution 5 - Jinja2pawel7318View Answer on Stackoverflow
Solution 6 - Jinja2GuoqiangView Answer on Stackoverflow
Solution 7 - Jinja2kiroscView Answer on Stackoverflow
Solution 8 - Jinja2ApolloView Answer on Stackoverflow
Solution 9 - Jinja2tarun kumarView Answer on Stackoverflow
Solution 10 - Jinja2Tamas HegedusView Answer on Stackoverflow
Solution 11 - Jinja2ApolloView Answer on Stackoverflow
Solution 12 - Jinja2yelmirView Answer on Stackoverflow