diff --git a/lib/handlebars/compiler.js b/lib/handlebars/compiler.js index 5e5e37e2..35bac4bd 100644 --- a/lib/handlebars/compiler.js +++ b/lib/handlebars/compiler.js @@ -91,7 +91,8 @@ Handlebars.registerHelper('each', function(context, fn, inverse) { }); Handlebars.registerHelper('if', function(context, fn, inverse) { - if(context === false || context == null) { + var condition = typeof context === "function" ? context.call(this) : context; + if(condition === false || condition == null) { return inverse(this); } else { return fn(this); diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 44b5ea4b..7b3f9426 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -495,3 +495,27 @@ test("with", function() { shouldCompileTo(string, {person: {first: "Alan", last: "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!", + "if with boolean argument shows the contents when true"); + shouldCompileTo(string, {goodbye: "dummy", world: "world"}, "GOODBYE cruel world!", + "if with string argument shows the contents"); + shouldCompileTo(string, {goodbye: false, world: "world"}, "cruel world!", + "if with boolean argument does not show the contents when false"); + shouldCompileTo(string, {world: "world"}, "cruel world!", + "if with undefined does not show the contents"); +}); + +test("if with function argument", function() { + var string = "{{#if goodbye}}GOODBYE {{/if}}cruel {{world}}!"; + shouldCompileTo(string, {goodbye: function() {return true}, world: "world"}, "GOODBYE cruel world!", + "if with function shows the contents when function returns true"); + shouldCompileTo(string, {goodbye: function() {return this.world}, world: "world"}, "GOODBYE cruel world!", + "if with function shows the contents when function returns string"); + shouldCompileTo(string, {goodbye: function() {return false}, world: "world"}, "cruel world!", + "if with function does not show the contents when returns false"); + shouldCompileTo(string, {goodbye: function() {return this.foo}, world: "world"}, "cruel world!", + "if with function does not show the contents when returns undefined"); +});