Reputation: 1
Is it possible to call a js function and pass a variable into nunjucks template..?
{{ filter }}
{% set filterParam = orderBy() %}
{{ filter }}
{% for item in items | sort(true, true, filterParam) %}
Upvotes: 0
Views: 606
Reputation: 11
const env = new nunjucks.Environment(/* ...loaders etc... */);
What do you mean by loaders?
Upvotes: 1
Reputation: 5225
To use a function inside a template you should define it as global.
const nunjucks = require('nunjucks');
const env = new nunjucks.Environment(/* ...loaders etc... */); // or nunjucks.configure(...)
env.addGlobal('foo', () => 'OK');
const out = env.renderString(`{{ foo () }}`);
console.log(out);
Upvotes: 0