Last active
December 16, 2015 13:18
-
-
Save jordantomax/5440241 to your computer and use it in GitHub Desktop.
Handlebars foreach helper with index, first, last and support for string primitives
This file contains hidden or 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
| 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