nunjucks-setasync
v1.0.4
Published
Set Async tag
Readme
nunjucks-set-async
Like set, but for async functions.
Usage
You can use this extension in the following 4 easy steps:
- Create a new nunjucks Environment.
let env = new nunjucks.Environment(); - Add the nunjucks-set-async to the environment.
env.addExtension('SetAsyncExtension', new SetAsyncExtension()) - In your template, call the extension as follow:
{% setAsync 'variable', functionName, [functionArguments] %}variableis the name of the variable that will hold the results offunctionNamewith[functionArguments]. If the functionfunctionNamedoes not take any arguments, you need to still pass an empty array{% setAsync 'variable', functionName, [] %}. - Use
{{ variable }}to render the value ofvariableanywhere after thesetAsyncblock.
Example
In this example we are simulating getting the number of astronauts currently in space with an asynchronous function:
- index.js
let getNumberOfAstronauts = function(cb) {
setTimeout(function() {
cb(null, 12);
}, 10);
}
env.addGlobal('getNumberOfAstronauts', getNumberOfAstronauts);
env.addExtension('SetAsyncExtension', new SetAsyncExtension())- template.html
{% setAsync 'numberofAstronauts', getNumberOfAstronauts, [] %}
<p> There are {{numberofAstronauts}} in space right now! </p>Or you can also use setAsync with parens as follow:
{% setAsync('numberofAstronauts', getNumberOfAstronauts, []) %}
<p> There are {{numberofAstronauts}} in space right now! </p>- index.js
env.render('template.html', function(err, res) {
console.log(res);
});