77

I’m new to Handlebars.js and just started using it. Most of the examples are based on iterating over an object. I wanted to know how to use handlebars in basic for loop.

Example.

for(i=0 ; i<100 ; i++) {
   create li's with i as the value
}

How can this be achieved?

Jan Johansen
  • 1,829
  • 5
  • 29
  • 42
user1184100
  • 6,322
  • 27
  • 74
  • 116

5 Answers5

179

There's nothing in Handlebars for this but you can add your own helpers easily enough.

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}}
    <span>{{this}}</span>
{{/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}}
    <span>{{this}}</span>
{{/for}}

Demo: http://jsfiddle.net/ambiguous/WNbrL/

mu is too short
  • 396,305
  • 64
  • 779
  • 743
24

Top answer here is good, if you want to use last / first / index though you could use the following

Handlebars.registerHelper('times', function(n, block) {
    var accum = '';
    for(var i = 0; i < n; ++i) {
        block.data.index = i;
        block.data.first = i === 0;
        block.data.last = i === (n - 1);
        accum += block.fn(this);
    }
    return accum;
});

and

{{#times 10}}
    <span> {{@first}} {{@index}} {{@last}}</span>
{{/times}}
Kannan J
  • 368
  • 4
  • 11
Mike Mellor
  • 1,164
  • 12
  • 20
  • 2
    This helper seems to not allow to use @../index or @../last when coming to nested uses of the Helper. Is this right or maybe I did something wrong? – madtyn Jun 13 '17 at 10:43
8

If you like CoffeeScript

Handlebars.registerHelper "times", (n, block) ->
  (block.fn(i) for i in [0...n]).join("")

and

{{#times 10}}
  <span>{{this}}</span>
{{/times}}
Manuel
  • 193
  • 1
  • 6
8

This snippet will take care of else block in case n comes as dynamic value, and provide @index optional context variable, it will keep the outer context of the execution as well.

/*
* Repeat given markup with given times
* provides @index for the repeated iteraction
*/
Handlebars.registerHelper("repeat", function (times, opts) {
    var out = "";
    var i;
    var data = {};

    if ( times ) {
        for ( i = 0; i < times; i += 1 ) {
            data.index = i;
            out += opts.fn(this, {
                data: data
            });
        }
    } else {

        out = opts.inverse(this);
    }

    return out;
});
dmi3y
  • 3,152
  • 2
  • 19
  • 31
  • 1
    I like this approach better because you have the @index variable at hand. Also you can easily add other variables depending on specific needs. – ChrisRich Jan 29 '16 at 10:15
0

Couple of years late, but there's now each available in Handlebars which allows you to iterate pretty easily over an array of items.

https://handlebarsjs.com/guide/builtin-helpers.html#each

Dharman
  • 21,838
  • 18
  • 57
  • 107