Skip to content

Instantly share code, notes, and snippets.

@wycats
Forked from paul/gist:1185483
Created September 1, 2011 04:59
Show Gist options
  • Save wycats/1185487 to your computer and use it in GitHub Desktop.
Save wycats/1185487 to your computer and use it in GitHub Desktop.
arr = [1,2,3]
{{#each }}
{{this}},
{{/each}}
produces:
1,2,3,
Can I do something to produce:
1,2,3
instead?
{{#each arr}}
{{item}}{{#unless last}},{{/unless}}
{{/each}}
Handlebars.registerHelper("eachWithIndex", function(context, options) {
var idx = 0, len = context.length, lastIdx = len - 1, last = false;
var buf = "", currentContext;
context.forEach(function(item) {
last = idx === lastIdx;
currentContext = { item: item, idx: idx, last: last }
buf += options.fn(currentContext);
idx++;
});
return buf;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment