mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 635 forks source link

Add "limit" filter #1366

Closed sdegutis closed 2 months ago

sdegutis commented 2 years ago

Summary

Proposed change:

This seems like an essential FP function that's missing.

Usage:

{{ posts | limit(3) }}

Checklist

I've completed the checklist below to ensure I didn't forget anything. This makes reviewing this PR as easy as possible for the maintainers. And it gets this change released as soon as possible.

ogonkov commented 2 years ago

Thank you for your contribution! This project is aimed to mimic Jinja project in nodejs. From my knowledge it didn't accept filters that is not a part of Jinja.

sdegutis commented 2 years ago

From the perspective of trying to mimic another project's API, that makes sense.

From my perspective as a user of Nunjucks, who is not using either Python or Jinja, I have no need of Nunjucks to restrict its usefulness in order to accomplish a goal of mimicking another project's API.

Having limit is only going to add to the usefulness of Nunjucks, at least for me.

But feel free to close this if it doesn't fit the project's goals, and if the project's goals aren't going to evolve to meet this need. After all, it's a very small utility function that I have already implemented myself locally.

codecov[bot] commented 2 years ago

Codecov Report

Merging #1366 (106fc9e) into master (fd50090) will decrease coverage by 0.02%. The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1366      +/-   ##
==========================================
- Coverage   89.65%   89.63%   -0.03%     
==========================================
  Files          22       22              
  Lines        3046     3048       +2     
==========================================
+ Hits         2731     2732       +1     
- Misses        315      316       +1     
Impacted Files Coverage Δ
nunjucks/src/filters.js 95.94% <50.00%> (-0.32%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update fd50090...106fc9e. Read the comment docs.

webketje commented 1 year ago

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)) }}