This commit is contained in:
Alan Johnson
2011-01-22 10:30:44 -05:00
2 changed files with 26 additions and 1 deletions
+2 -1
View File
@@ -86,7 +86,8 @@ Handlebars.registerHelper('each', function(context, fn, inverse) {
});
Handlebars.registerHelper('if', function(context, fn, inverse) {
if(!context || context === []) {
var condition = typeof context === "function" ? context.call(this) : context;
if(!condition || condition == []) {
return inverse(this);
} else {
return fn(this);
+24
View File
@@ -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");
});