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

Feature request: Expose Nunjucks' addGlobal #495

Closed
robdodson opened this issue Apr 15, 2019 · 4 comments
Closed

Feature request: Expose Nunjucks' addGlobal #495

robdodson opened this issue Apr 15, 2019 · 4 comments
Labels
enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.

Comments

@robdodson
Copy link

I was curious if addGlobal might be something eleventy could expose.

In my instance I wanted to call a function that takes two arguments. It doesn't feel like I could do this correctly with a filter. I could use a shortcode to do this, but then I can't pipe the results into a filter.

Essentially I'm trying to do:

{{ findItemInCollection(collection, slug) | dump }}
@Ryuno-Ki
Copy link
Contributor

Would a syntax like {{ collection | findItem(slug) | dump }} work for you?

(I believe I implemented something related to that but haven't packaged it up, yet)

@robdodson
Copy link
Author

I think that might work but it's sort of arcane syntax. Kind of related to this comment from the nunjucks maintainer that filters are somewhat awkward syntax on top of function calls.

@edwardhorsford
Copy link
Contributor

@robdodson Elventy might not directly support it, but you can add them manually (I discovered this week) by using your own Nunjucks environment. I'm already doing similar to be able to set some specific Nunjucks options.

Inside your eleventy.js:

  let nunjucksEnvironment = new Nunjucks.Environment(
    new Nunjucks.FileSystemLoader(["src/site/_templates"]), // we need to pass in our includes dir here
    { 
      lstripBlocks: true,
      trimBlocks: true,
      autoescape: false
     });

  nunjucksEnvironment.addGlobal(name, nunjucksGlobals[name]);

  eleventyConfig.setLibrary("njk", nunjucksEnvironment);

FWIW I've made a similar filter. I'm pleased to see the syntax matches @Ryuno-Ki's suggested. I also accept an optional arg to do exact matching or not.

I've also just found out that filters (at least for Nunjucks) have access to the context. So I'm tempted to rework my filter to not require collections at all - just take a slug and return the item / array of items / false.

@zachleat zachleat added the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Jan 17, 2020
@zachleat
Copy link
Member

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.
Projects
None yet
Development

No branches or pull requests

4 participants