Got expressions that are helpers with arguments working.

This commit is contained in:
Alan Johnson
2010-09-09 20:13:08 -04:00
parent fee8b8c84e
commit 40b1108b2e
2 changed files with 10 additions and 4 deletions
+3 -4
View File
@@ -1,7 +1,6 @@
Handlebars = {
compile: function(string) {
var fnBody = Handlebars.compileFunctionBody(string);
console.log(fnBody);
return new Function("context", "fallback", fnBody);
},
@@ -258,10 +257,10 @@ Handlebars.Compiler.prototype = {
}
},
addExpression: function(mustache) {
addExpression: function(mustache, param) {
var expr = this.lookupFor(mustache);
this.fn += "if (Handlebars.isFunction(" + expr + ")) out = out + Handlebars.filterOutput(" + expr + ".call(Handlebars.buildContext(context, stack)), " + this.escaped + "); ";
this.fn += "if (Handlebars.isFunction(" + expr + ")) out = out + Handlebars.filterOutput(" + expr + ".call(Handlebars.buildContext(context, stack), " + param + "), " + this.escaped + "); ";
this.fn += "else if(typeof " + expr + "!== 'undefined') out = out + Handlebars.filterOutput(" + expr + ", " + this.escaped + ");";
},
@@ -403,7 +402,7 @@ Handlebars.Compiler.prototype = {
this.addBlock(mustache, param, parts)
return;
} else {
return this.addExpression(mustache);
return this.addExpression(mustache, param);
}
this.escaped = true;
+7
View File
@@ -54,6 +54,13 @@ test("functions", function() {
"functions are called and render their output");
});
test("functions with context argument", function() {
shouldCompileTo("{{awesome frank}}",
{awesome: function(context) { return context; },
frank: "Frank"},
"Frank", "functions are called with context arguments");
});
test("nested paths", function() {
shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: "beautiful"}},
"Goodbye beautiful world!", "Nested paths access nested objects");