Make the function passed to a block helper have an identical signature to top-level template methods

This commit is contained in:
tomhuda
2011-02-14 16:05:01 -08:00
parent d7f93c09e7
commit 2b319bef2f
2 changed files with 37 additions and 3 deletions
+6 -3
View File
@@ -627,13 +627,16 @@ Handlebars.JavaScriptCompiler = function() {};
Handlebars.VM = {
programWithDepth: function(fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function(context) {
return function(context, helpers, partials, data) {
args[0] = helpers || args[0];
args[1] = partials || args[1];
args[2] = data || args[2];
return fn.apply(this, [context].concat(args));
};
},
program: function(fn, helpers, partials, data) {
return function(context) {
return fn(context, helpers, partials, data);
return function(context, h2, p2, d2) {
return fn(context, h2 || helpers, p2 || partials, d2 || data);
};
},
noop: function() { return ""; },
+31
View File
@@ -599,5 +599,36 @@ test("passing in data to a compiled function that expects data - works with bloc
equals("#win happy world?", result);
});
test("you can override inherited data when invoking a helper", function() {
var template = Handlebars.compile("{{#hello}}{{world zomg}}{{/hello}}", true);
var helpers = {
hello: function(fn) {
return fn({exclaim: "?", zomg: "world"}, null, null, {adjective: "sad"});
},
world: function(thing, data) {
return data.adjective + " " + thing + (this.exclaim || "");
}
};
var result = template({exclaim: true, zomg: "planet"}, helpers, null, {adjective: "happy"});
equals("sad world?", result);
});
test("you can override inherited data when invoking a helper with depth", function() {
var template = Handlebars.compile("{{#hello}}{{world ../zomg}}{{/hello}}", true);
var helpers = {
hello: function(fn) {
return fn({exclaim: "?"}, null, null, {adjective: "sad"});
},
world: function(thing, data) {
return data.adjective + " " + thing + (this.exclaim || "");
}
};
var result = template({exclaim: true, zomg: "world"}, helpers, null, {adjective: "happy"});
equals("sad world?", result);
});