Skip to content

Instantly share code, notes, and snippets.

@tylersticka
Created August 4, 2015 23:42
Show Gist options
  • Save tylersticka/7e1bcc7b355c3a22d9fe to your computer and use it in GitHub Desktop.
Save tylersticka/7e1bcc7b355c3a22d9fe to your computer and use it in GitHub Desktop.
Simple iteration helper for Handlebars
/**
* Iterate over a block a given number of times.
*
* Usage:
* <ul>
* {{#times 5}}<li>Item {{@index}}</li>{{/times}}
* </ul>
*/
var Handlebars = require('handlebars');
Handlebars.registerHelper('times', function (n, options) {
var out = '', data;
for (var i = 0; i < n; ++i) {
data = Handlebars.createFrame(options.data || {});
data.index = i;
out += options.fn(i, { data: data });
}
return out;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment