Home > Blockchain >  Call a Javascript function to update a filter in Nunjucks template?
Call a Javascript function to update a filter in Nunjucks template?

Time:07-01

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) %}

CodePudding user response:

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);

CodePudding user response:

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

What do you mean by loaders?

  • Related