Handlebars.js Loop for helper

Handlebars.js 26 ก.ย. 2016

If you just wanted to do something n times then:

Handlebars.registerHelper('times', function(n, block) {
    var accum = '';
    for(var i = 0; i < n; ++i)
        accum += block.fn(i);
    return accum;
});

and

{{#times 10}}
    {{this}}
{{/times}}

If you wanted a whole for(;;) loop, then something like this:

Handlebars.registerHelper('for', function(from, to, incr, block) {
    var accum = '';
    for(var i = from; i < to; i += incr)
        accum += block.fn(i);
    return accum;
});

and

{{#for 0 10 2}}
    {{this}}
{{/for}}

source: http://stackoverflow.com/questions/11924452/iterating-over-basic-for-loop-using-handlebars-js

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.