Skip to content

Instantly share code, notes, and snippets.

@jordantomax
Last active December 16, 2015 13:18
Show Gist options
  • Save jordantomax/5440241 to your computer and use it in GitHub Desktop.
Save jordantomax/5440241 to your computer and use it in GitHub Desktop.
Handlebars foreach helper with index, first, last and support for string primitives
Handlebars.registerHelper("foreach",function(arr,options) {
if (options.inverse && !arr.length) {
return options.inverse(this);
}
return arr.map(function(item,index) {
if (typeof item === 'string') {
var item = new String(item);
}
item.$index = index;
item.$first = index === 0;
item.$last = index === arr.length-1;
return options.fn(item);
}).join('');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment