From 6bfff5f97bc62af9750a39fe3ab9be55ee18d295 Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Thu, 9 Sep 2010 20:29:49 -0400 Subject: [PATCH] Got helper functions to always receive a context as their first param. --- lib/handlebars.js | 4 ++-- test/handlebars.js | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index 3c0fa23f..9225e128 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -326,14 +326,14 @@ Handlebars.Compiler.prototype = { this.fn += "var proxy = Handlebars.buildContext(" + param + ", stack);"; this.fn += "var " + fnId + " = function(context) {" + result + "}; "; this.fn += "lookup = " + this.lookupFor(mustache) + "; "; - this.fn += "if(Handlebars.isFunction(lookup)) out = out + lookup.call(proxy, " + fnId + "); "; + this.fn += "if(Handlebars.isFunction(lookup)) out = out + lookup.call(context, proxy, " + fnId + "); "; this.fn += "else if(typeof lookup !== 'undefined') { out = out + Handlebars.helperMissing.call(proxy, lookup, " + fnId + "); }"; if (compiler.continueInverted) { var invertedCompiler = this.compileToEndOfBlock(mustache); this.fn += "if (Handlebars.isFunction(lookup.not)) {"; this.fn += " var " + fnId + "Not = function(context) { " + invertedCompiler.fn + " };"; - this.fn += " out = out + lookup.not.call(proxy, " + fnId + "Not);"; + this.fn += " out = out + lookup.not.call(context, proxy, " + fnId + "Not);"; this.fn += "}"; } diff --git a/test/handlebars.js b/test/handlebars.js index a6b811d4..c533d442 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -164,7 +164,7 @@ test("block helper", function() { var string = "{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!"; var template = Handlebars.compile(string); - result = template({goodbyes: function(fn) { return fn({text: "GOODBYE"}); }, world: "world"}); + result = template({goodbyes: function(context, fn) { return fn({text: "GOODBYE"}); }, world: "world"}); equal(result, "GOODBYE! cruel world!"); }); @@ -172,7 +172,7 @@ test("block helper staying in the same context", function() { var string = "{{#form}}

{{name}}

{{/form}}" var template = Handlebars.compile(string); - result = template({form: function(fn) { return "
" + fn(this) + "
" }, name: "Yehuda"}); + result = template({form: function(context, fn) { return "
" + fn(this) + "
" }, name: "Yehuda"}); equal(result, "

Yehuda

"); }); @@ -180,7 +180,7 @@ test("block helper passing a new context", function() { var string = "{{#form yehuda}}

{{name}}

{{/form}}" var template = Handlebars.compile(string); - result = template({form: function(fn) { return "
" + fn(this) + "
" }, yehuda: {name: "Yehuda"}}); + result = template({form: function(context, fn) { return "
" + fn(context) + "
" }, yehuda: {name: "Yehuda"}}); equal(result, "

Yehuda

"); }); @@ -188,7 +188,7 @@ test("block helper passing a complex path context", function() { var string = "{{#form yehuda/cat}}

{{name}}

{{/form}}" var template = Handlebars.compile(string); - result = template({form: function(fn) { return "
" + fn(this) + "
" }, yehuda: {name: "Yehuda", cat: {name: "Harold"}}}); + result = template({form: function(context, fn) { return "
" + fn(context) + "
" }, yehuda: {name: "Yehuda", cat: {name: "Harold"}}}); equal(result, "

Harold

"); }); @@ -196,18 +196,18 @@ test("nested block helpers", function() { var string = "{{#form yehuda}}

{{name}}

{{#link}}Hello{{/link}}{{/form}}" var template = Handlebars.compile(string); - result = template({form: function(fn) { return "
" + fn(this) + "
" }, yehuda: {name: "Yehuda", link: function(fn) { return "" + fn(this) + ""; }}}); + result = template({form: function(context, fn) { return "
" + fn(context) + "
" }, yehuda: {name: "Yehuda", link: function(context, fn) { return "" + fn(context) + ""; }}}); equal(result, "

Yehuda

Hello
"); }); test("block inverted sections", function() { var string = "{{#list people}}{{name}}{{^}}Nobody's here{{/list}}" - var list = function(fn) { - if (this.length > 0) { + var list = function(context, fn) { + if (context.length > 0) { var out = ""; @@ -215,8 +215,8 @@ test("block inverted sections", function() { } }; - list.not = function(fn) { - return "

" + fn(this) + "

"; + list.not = function(context, fn) { + return "

" + fn(context, this) + "

"; }; var hash = {list: list, people: [{name: "Alan"}, {name: "Yehuda"}]};