diff options
-rw-r--r-- | lib/handlebars/base.js | 6 | ||||
-rw-r--r-- | spec/qunit_spec.js | 28 |
2 files changed, 31 insertions, 3 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 7d6c1ce..776ba50 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -72,7 +72,8 @@ Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) { return fn(context); }); -Handlebars.registerHelper('each', function(context, fn, inverse) { +Handlebars.registerHelper('each', function(ctx, fn, inverse) { + var context = typeof ctx == "function" ? ctx.call(this) : ctx; var ret = ""; if(context && context.length > 0) { @@ -98,7 +99,8 @@ Handlebars.registerHelper('unless', function(context, fn, inverse) { Handlebars.helpers['if'].call(this, context, inverse, fn); }); -Handlebars.registerHelper('with', function(context, fn) { +Handlebars.registerHelper('with', function(ctx, fn) { + var context = typeof ctx == "function" ? ctx.call(this) : ctx; return fn(context); }); diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 1453189..8bbade1 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -490,12 +490,19 @@ test("if a context is not found, helperMissing is used", function() { module("built-in helpers"); -test("with", function() { +test("with non-function argument", function() { var string = "{{#with person}}{{first}} {{last}}{{/with}}"; shouldCompileTo(string, {person: {first: "Alan", last: "Johnson"}}, "Alan Johnson"); }); +test("with function argument", function() { + var string = "{{#with person}}{{first}} {{last}}{{/with}}"; + + shouldCompileTo(string, {person: function() {return {first: this.firsName, last: this.lastName};}, + firsName: "Alan", lastName: "Johnson"}, "Alan Johnson"); +}); + test("if with non-function argument", function() { var string = "{{#if goodbye}}GOODBYE {{/if}}cruel {{world}}!"; shouldCompileTo(string, {goodbye: true, world: "world"}, "GOODBYE cruel world!", @@ -519,3 +526,22 @@ test("if with function argument", function() { shouldCompileTo(string, {goodbye: function() {return this.foo}, world: "world"}, "cruel world!", "if with function does not show the contents when returns undefined"); }); + +test("each with non-function argument", function() { + var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!" + var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; + shouldCompileTo(string, hash, "goodbye! Goodbye! GOODBYE! cruel world!", + "each with array argument iterates over the contents when not empty"); + shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!", + "each with array argument ignores the contents when empty"); +}); + +test("each with function argument", function() { + var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!" + var hash = {goodbyes: function() {return this.texts;}, + texts: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; + shouldCompileTo(string, hash, "goodbye! Goodbye! GOODBYE! cruel world!", + "each with function argument returning array iterates over the contents when not empty"); + shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!", + "each with function argument returning array ignores the contents when empty"); +}); |