Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty string for empty array is inconsistent with Jinja2 #534

Open
sooda opened this issue Jul 21, 2020 · 2 comments · May be fixed by #536
Open

Empty string for empty array is inconsistent with Jinja2 #534

sooda opened this issue Jul 21, 2020 · 2 comments · May be fixed by #536

Comments

@sooda
Copy link

sooda commented Jul 21, 2020

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.

@Keats
Copy link
Owner

Keats commented Jul 22, 2020

Good idea but that's a breaking change, so for the next major version

sooda added a commit to sooda/tera that referenced this issue Jul 22, 2020
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.

Closes Keats#534, and is a breaking change.
@sooda sooda linked a pull request Jul 22, 2020 that will close this issue
@sooda
Copy link
Author

sooda commented Jul 22, 2020

Sure, thanks. Opened a PR for completeness; looks like the change would be small patch-wise at the moment.

sooda added a commit to sooda/tera that referenced this issue Aug 11, 2020
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.

Closes Keats#534, and is a breaking change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants