You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Tera manual says this about the filters first, last and nth:
If the array is empty, returns empty string.
The Jinja 2.11.x template designer documentation doesn't seem to specify what happens for an empty sequence in first and last, but according to the following experiment in Python, it's undefined and not an empty string. Undefined does make more sense in some cases and is more general:
from jinja2 import Template
template = Template("hello, ?{{ items | first | default('world') }}?")
output = template.render(items=[])
print(output)
This prints hello, ?world?. The same in Tera 1.3.1 would be
let input = "hello, ?{{ items | first | default(value='world') }}?";
let mut context = Context::new();
context.insert("items", &Vec::<&str>::new());
let output = Tera::one_off(input, &context, false).unwrap();
println!("{}", output);
which prints hello, ?? as expected because the value from first is defined, so default does not trigger.
Printing the value from first for an empty sequence is still well defined in Jinja2. From the template designer docs (this seems to already happen in Tera as well for undefined values, such as printing items[1] for empty items):
If a variable or attribute does not exist, you will get back an undefined value. What you can do with that kind of value depends on the application configuration: the default behavior is to evaluate to an empty string if printed or iterated over, and to fail for every other operation.
Consider changing the behaviour of these three filters to prefer undefined (null) over empty string.
The text was updated successfully, but these errors were encountered:
Instead of empty string, return null value that's considered undefined
(and can be piped to the default filter) if the array is empty for the
filters first, last and nth. For nth, also change the out-of-bounds
value from empty string to null.
ClosesKeats#534, and is a breaking change.
Instead of empty string, return null value that's considered undefined
(and can be piped to the default filter) if the array is empty for the
filters first, last and nth. For nth, also change the out-of-bounds
value from empty string to null.
ClosesKeats#534, and is a breaking change.
The Tera manual says this about the filters
first
,last
andnth
:The Jinja 2.11.x template designer documentation doesn't seem to specify what happens for an empty sequence in
first
andlast
, but according to the following experiment in Python, it's undefined and not an empty string. Undefined does make more sense in some cases and is more general:This prints
hello, ?world?
. The same in Tera 1.3.1 would bewhich prints
hello, ??
as expected because the value fromfirst
is defined, sodefault
does not trigger.Printing the value from
first
for an empty sequence is still well defined in Jinja2. From the template designer docs (this seems to already happen in Tera as well for undefined values, such as printing items[1] for empty items):Consider changing the behaviour of these three filters to prefer undefined (null) over empty string.
The text was updated successfully, but these errors were encountered: