Permalink
Browse files

Add support for @index in #each

  • Loading branch information...
1 parent 72e05d6 commit 5e8be14d78f87e73878e4d29013482f7daff9d8a @wycats committed Jul 6, 2012
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/handlebars/base.js
@@ -56,13 +56,27 @@ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
}
});
+Handlebars.K = function() {};
+
+Handlebars.createFrame = Object.create || function(object) {
+ Handlebars.K.prototype = object;
+ var obj = new Handlebars.K();
+ Handlebars.K.prototype = null;
+ return obj;
+};
+
Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
- var ret = "";
+ var ret = "", data;
+
+ if (options.data) {
+ data = Handlebars.createFrame(options.data);
+ }
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
- ret = ret + fn(context[i]);
+ if (data) { data.index = i; }
+ ret = ret + fn(context[i], { data: data });
}
} else {
ret = inverse(this);

0 comments on commit 5e8be14

Please sign in to comment.