Name: nunjucks-setasync
Owner: Namshi
Description: Like set, but for async functions.
Created: 2017-01-18 11:37:56.0
Updated: 2017-05-17 19:16:10.0
Pushed: 2017-03-21 06:17:33.0
Homepage: null
Size: 8
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Like set, but for async functions.
You can use this extension in the following 4 easy steps:
let env = new nunjucks.Environment();
env.addExtension('SetAsyncExtension', new SetAsyncExtension())
{% setAsync 'variable', functionName, [functionArguments] %}
variable
is the name of the variable that will hold the results of functionName
with [functionArguments]
. If the function functionName
does not take any arguments, you need to still pass an empty array {% setAsync 'variable', functionName, [] %}
.{{ variable }}
to render the value of variable
anywhere after the setAsync
block.In this example we are simulating getting the number of astronauts currently in space with an asynchronous function:
index.js
getNumberOfAstronauts = function(cb) {
imeout(function() {
ull, 12);
0);
addGlobal('getNumberOfAstronauts', getNumberOfAstronauts);
addExtension('SetAsyncExtension', new SetAsyncExtension())
template.html
etAsync 'numberofAstronauts', getNumberOfAstronauts, [] %}
There are {{numberofAstronauts}} in space right now! </p>
Or you can also use setAsync
with parens as follow:
etAsync('numberofAstronauts', getNumberOfAstronauts, []) %}
There are {{numberofAstronauts}} in space right now! </p>
render('template.html', function(err, res) {
ole.log(res);