Nunjucks' goal is to have full Jinja2 compatibility, and eventual Twig compatibility.
The Twig slice filter behaves a lot like Javascript's arr.slice(start, end), however Jinja's slice creates an array of arrays.
In effect, it can be argued that Nunjucks is missing a feature to get n items from the start/end of a string/array because in Python Jinja2, this can be achieved using the square bracket notation source_list[:N]. Given that Nunjucks does not allow this, a better proposal IMO would be to "augment" the first and last filters so they can take an additional signature with an argument, like:
The following is salvaged from a meaningful comment on a now-deleted PR that is a good candidate for increased feature parity
This is really easy to add in user-code if desired:
Nunjucks' goal is to have full Jinja2 compatibility, and eventual Twig compatibility. The Twig slice filter behaves a lot like Javascript's
arr.slice(start, end)
, however Jinja's slice creates an array of arrays.In effect, it can be argued that Nunjucks is missing a feature to get n items from the start/end of a string/array because in Python Jinja2, this can be achieved using the square bracket notation
source_list[:N]
. Given that Nunjucks does not allow this, a better proposal IMO would be to "augment" thefirst
andlast
filters so they can take an additional signature with an argument, like:Originally posted by @webketje in https://github.com/mozilla/nunjucks/issues/1366#issuecomment-1290549739