Skip to content

Instantly share code, notes, and snippets.

@gabrielstuff
Forked from burin/each_with_index.coffee
Created March 3, 2012 16:00
Show Gist options
  • Save gabrielstuff/1966803 to your computer and use it in GitHub Desktop.
Save gabrielstuff/1966803 to your computer and use it in GitHub Desktop.
each_with_index handlebars helper, adds an {{index}} prop accessible from within the block
// {{#each_with_index records}}
// <li class="legend_item{{index}}"><span></span>{{Name}}</li>
// {{/each_with_index}}
Handlebars.registerHelper("each_with_index", function(array, fn) {
var total = array.length;
var buffer = "";
for (var i = 0, j = array.length; i < j; i++) {
var item = array[i];
// stick an index property onto the item, starting with 1, may make configurable later
item.index = i+1;
item.total = total;
// show the inside of the block
buffer += fn(item);
}
// return the finished buffer
return buffer;
});
@gabrielstuff
Copy link
Author

Love it just add access to the total number of el.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment