简体   繁体   中英

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) %}
const env = new nunjucks.Environment(/* ...loaders etc... */);

装载机是什么意思?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM