I am using NodeJS/Express3 and dusts-linkedin and would like to do the equivalent of:
for(var i = locals.first;i < (locals.first + locals.pages); $i++) {
<div>i</div>
}
I assume I need to create a helper function but am unclear on how to do so
Ok. In my case I was trying to create a pager but I'm going to simplify the answer a bit so that one can get the general idea. There may be a cleaner way to do this.
My helper will be called 'pager'. I have some express variables in locals that I will want to pass. These are locals.start and locals.end - each is a number representing my pager start and end. I will also pass a url to use for the pager.
In my template I call the helper along with all the parameters I may need:
{@pager start="{locals.start}" end="{locals.end}" url="/articles?page=%page%"/}
Then in my app.'s I will add this helper. I need to make sure my dust module is present:
cons = require('consolidate'),
dust = require('dustjs-linkedin'),
Then create the helper:
dust.helpers['pager'] = function(chunk, context, bodies, params) {
//tapping these to get the actual value (since we passed vsaraibles) and casting as a number with '+'
first = +dust.helpers.tap(params.first, chunk, context);
last = +dust.helpers.tap(params.last, chunk, context);
//this one is all ready since we just passed a string
url = params.url;
//build our html - do whatever you need here - this is an example
var html = '<ul>';
for(var i = first; i <= last; $i++) {
//I used a replace and placeholder - prob other methods you can use
html += '<li><a href="' + url.replace('%page%', i) + '">' + i '</a></li>';
}
html += '</ul>';
//write this back to the template
chunk.write(html);
//return it
return chunk;
};
Hope this helps someone