Skip to content

Conversation

@EtiennePelletier
Copy link
Contributor

@EtiennePelletier EtiennePelletier commented May 7, 2019

Allows using default values with chains of items or attributes that may
contain undefined values without raising a jinja2.exceptions.UndefinedError.

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

Fixes #977

Allows using default values with chains of items or attributes that may
contain undefined values without raising a jinja2.exceptions.UndefinedError.

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

webknjaz commented May 7, 2019

@webknjaz

@sivel
Copy link
Contributor

sivel commented May 8, 2019

As this was brought up in the Ansible sprint, I wanted to share feedback here that Ansible has implemented a class that performs this same duty:

https://github.com/ansible/ansible/blob/11279a909dc441dcef07e4104fd1320c4ba4fb22/lib/ansible/template/__init__.py#L195-L209

We will still have the need to use ours for the foreseeable future, as we deal with many OSes and users that use the OS provided packages, which means they are likely running very old versions of jinja.

@davidism davidism merged commit 19133d4 into pallets:master May 8, 2019
@davidism davidism added this to the 2.11 milestone May 9, 2019
@EtiennePelletier EtiennePelletier deleted the jinja2-977 branch September 21, 2019 02:24
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] default filter on dictionnary and undefined parent key

4 participants