-
-
Notifications
You must be signed in to change notification settings - Fork 495
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
allow registering Nunjucks globals #1060
Conversation
btw, fixes #495 |
@lxg are these like Nunjucks macros? |
@nhoizey Not exactly. While they both behave like funktions, Nunjucks macros are defined with Nunjucks syntax, whereas you would write such a global as Javascript and just expose the functionality to Nunjucks. For example: {% macro hello(who) %}
Hello {{ who }}
{% endmacro %} vs. eleventyConfig.addNunjucksGlobal("hello", who => `Hello ${who}`) Both would expose the same functionality in this simple example, for instance:
But obviously native JS is more powerful in regard to syntax and context. Also, and this is my primary reason for this PR, it gives you more power than filters and tags in Nunjucks. In my scenario, there are translation functions that take parameters and are wrapped in |
Ok, I understand the difference now, thanks! It looks like it could fix my issue with tags not getting "external" data, leading to the need to pass information from one to another. For example, I use this macro in my article template: Which calls this other macro, which is called also for other content types: I have to pass data and set default values for different use cases, that's not really easy and clean. Globals look better. |
ping |
@zachleat any opinions regarding this PR? |
I was just looking for let Nunjucks = require('nunjucks')
module.exports = (config) => {
let nunjucksEnvironment = new Nunjucks.Environment()
nunjucksEnvironment.addGlobal('foobar', (str) => `foo ${str} bar`)
config.setLibrary('njk', nunjucksEnvironment)
} |
I do what @spl suggested - making my own environment and passing globals to it. |
Shipping this with 1.0, thank you! |
Any chance to use it with the current version of 11ty? Thanks... |
@gremo You could check out via git branch (but that'll pull in other changes, too). |
Q: Does This seems to work in Nunjucks (see RunKit): const nunjucks = require("nunjucks");
const env = new nunjucks.Environment();
env.addGlobal("cats", 42);
const res = env.renderString('{{ username }} owns {{ cats }} cats!', { username: 'James' });
console.log(res); // Output: "James owns 42 cats!" But I get errors in Eleventy: // .eleventy.js snippet
eleventyConfig.addNunjucksGlobal("cats", 42); {{ cats }} OUTPUT
But then if I try: {{ cats() }} I get a build error with the following:
Workaround seems to be to always wrap the global value in a function, which might be inconsistent w/ Nunjucks behavior: // .eleventy.js snippet
eleventyConfig.addNunjucksGlobal("cats", () => 42); Then use |
I pushed a fix and tests for the issue with using a literal function @pdehaan #1060 (comment) |
This PR allows registering Nunjucks globals through the eleventy config. The purpose of this is mainly to allow creating global template functions where filters and tags aren’t enough.
A simple example:
in .eleventy.js
usage in a template file:
Result: