Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for @index in #each

  • Loading branch information...
commit 5e8be14d78f87e73878e4d29013482f7daff9d8a 1 parent 72e05d6
@wycats authored
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/handlebars/base.js
View
18 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);
Please sign in to comment.
Something went wrong with that request. Please try again.