-
-
Save mikemorris/3800980 to your computer and use it in GitHub Desktop.
each_with_index handlebars helper, adds an {{index}} prop accessible from within the block
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
// <ul data-length="{{records.length}}"> | |
// {{#each_with_index records}} | |
// <li class="legend_item{{this.index}}" z-index="{{this.reverse}}"><span></span>{{this.name}}</li> | |
// {{/each_with_index}} | |
// </ul> | |
Handlebars.registerHelper("each_with_index", function(array, options) { | |
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.reverse = j-i; | |
// show the inside of the block | |
buffer += options.fn(item); | |
} | |
// return the finished buffer | |
return buffer; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment