tera
tera copied to clipboard
[Feature request] Add a `falsy` option to the `default` filter
Currently, the default filter only return the given value if the variable evaluated is not present in the context.
It would be great if this behavior could be extended so that it would also return the default value when the variable exists but evaluates to false (false, empty string, empty array, ...)
For example, I have a use case with a variable that is an array and that I would like to transform into a string using the join filter.
I currently have to use an if statement, which could be made more concise with the following (for example):
{{ my_arr | default(value=[]) | join(sep=" - ") | default(value="N/A", falsy=true) }}
If you're sure the value is defined, you can use or which works on falsy values.
Hi @Keats,
Thanks for your answer and being so quick 👍
What would such a thing look like ?
I've made some testings with or but couldn't get anything else than false or true.
For example:
{{ my_arr | default(value=[]) | join(sep=" - ") or "N/A" }}
renders as true which make sense, but isn't what I want :-)