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

Add nunjucks filters to external lib file #133

Closed
paulmsmith opened this issue Jan 21, 2016 · 2 comments
Closed

Add nunjucks filters to external lib file #133

paulmsmith opened this issue Jan 21, 2016 · 2 comments

Comments

@paulmsmith
Copy link
Contributor

Hi,

I've created a fork of the kit and will soon be at the stage to contribute something potentially useful but as suggested in the Guide lines for contributing I'm explaining the enhancement here before asking somebody to spend time code reviewing a pull request.

In short:

  • Will keep filters neatly in their place and make them automatically available to nunjucks
  • Provide a suite of functional filters for manipulating data, strings, arrays, objects, etc.

At length:
Currently out-of-the-box (unless I've missed something) adding a new filter to Nunjucks express means doing the following in server.js:

nunjucks.ready(function(env) {
    // First custom filter.
    env.addFilter('sayHi', function(name) {
            return 'Ayup ' + name '!';
    });
    // Second custom filter.
    env.addFilter('sayBye', function(name) {
            return 'Ta-ta ' + name '!';
    });
    // etc.
    // and so on.
});

As a start I've created lib/filters.js and in there you would simply do:

fi.sayHi =  function(name) {
  return 'Ayup ' + name '!';
}

"fi" meaning "filter item". (You'll see why that name in the PR).

This keeps a potentially growing list of filters in their own place (with the possibility for further organisation/tidying if required).

Along with that, I can add a collection of useful filters with huge credit and thanks going to @TheFuzzball who wrote a lot them and has kindly given permission for their use. Allowing manipulating of strings, objects, arrays, etc. Essentially a thin layer between Nunjucks and lodash.

This could give us a basis for defining pattern schemas as discussed with @timpaul, @htmlandbacon, @gemmaleigh and others recently. I'll stop here and we could go into more detail about that another time.

If I do a PR, would it be preferable for two branches/PRs. One for simply making it easier to add custom nunjucks filters in their own file. One for adding the suite of useful filters (reduce, merge, etc)?

@timpaul
Copy link
Contributor

timpaul commented Jan 21, 2016

Wow - good stuff! Yes, keeping things like filters out of routes.js is a good idea and will make it easier to upgrade the kit over time. I think 2 PRs is a good idea too.

@joelanman
Copy link
Contributor

closed by #200

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

3 participants