From 40b1108b2e359e0dd99917995b0f057c33b9ee78 Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Thu, 9 Sep 2010 20:13:08 -0400 Subject: [PATCH] Got expressions that are helpers with arguments working. --- lib/handlebars.js | 7 +++---- test/handlebars.js | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index 94edac0c..3c0fa23f 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -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; diff --git a/test/handlebars.js b/test/handlebars.js index eeaa692e..a6b811d4 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -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");