Chr*_*ois 3 javascript templating nunjucks
So I've found this in the Nunjucks docs:
Function Calls
If you have passed a javascript method to your template, you can call it like normal.
{{ foo(1, 2, 3) }}
Run Code Online (Sandbox Code Playgroud)
But I can't seem to make in work, I've tried putting my function on the html page in <script> tags but its not working.
I've also tried passing it with the data to the render function:
{
stuff: function (string, length) {
while (string < length) {
string = "0" + string
}
}
}
Run Code Online (Sandbox Code Playgroud)
And I get: unable to call data["stuff"], which is undefined of falsey
您可以使用addGlobal(参见g)或传递一个函数来呈现(参见f)。
var nunjucks = require('nunjucks');
var env = nunjucks.configure();
function f (s) {
return s + s;
}
function g (s) {
return s + s + s;
}
env.addGlobal('g', g);
var res = nunjucks.renderString('{{f("OK")}} {{g("ok")}}', {f: f});
console.log(res);
//Output: OKOK okokok
Run Code Online (Sandbox Code Playgroud)