Created
August 4, 2015 23:42
-
-
Save tylersticka/7e1bcc7b355c3a22d9fe to your computer and use it in GitHub Desktop.
Simple iteration helper for Handlebars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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