DMS
DMS

Reputation: 1

Call a Javascript function to update a filter in Nunjucks template?

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

Answers (2)

badCoder
badCoder

Reputation: 11

const env = new nunjucks.Environment(/* ...loaders etc... */);

What do you mean by loaders?

Upvotes: 1

Aikon Mogwai
Aikon Mogwai

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

Related Questions