Jinja: map filter: can't use a false, '', None, [], {} as default (or anything that is automatically cast to false)

Created on 4 Jan 2021  路  2Comments  路  Source: pallets/jinja

https://github.com/pallets/jinja/blob/e39c87c4d474e36dde3b3576a674980e6f4699e1/src/jinja2/filters.py#L70

So this will work:

{{ users|map(attribute="username", default="Anonymous")|join(", ") }}

but not this:

{{ users|map(attribute="username", default="")|join(", ") }}

Most helpful comment

@davidism I am currently working on this Issue. Notified here.

All 2 comments

For anyone else that happens to stumble over this: You may be able to use something like this as a workaround:

{{ users | map(attribute="username") | select("defined") | join(", ") }}

This works at least in Ansible (where the missing values resolve to AnsibleUndefined), I haven't tested with pure Jinja2 though.

@davidism I am currently working on this Issue. Notified here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AMDmi3 picture AMDmi3  路  4Comments

dwt picture dwt  路  3Comments

mitsuhiko picture mitsuhiko  路  3Comments

jp-costa picture jp-costa  路  5Comments

delroth picture delroth  路  4Comments