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

Augment 'first' & 'last' filter with param n - Jinja/Twig feature parity #1463

Open
webketje opened this issue Apr 11, 2024 · 0 comments
Open

Comments

@webketje
Copy link

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:

constenv = new nunjucks.Environment()
env.addFilter('limit', function(str, n) {
  return arr.slice(0, n)
});

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:

{{ myarr|first(10)) }}
{{ myarr|last(5)) }}

Originally posted by @webketje in #1366 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant