diff options
author | Ryan Grove <ryan@wonko.com> | 2011-04-20 16:56:37 -0700 |
---|---|---|
committer | Ryan Grove <ryan@wonko.com> | 2011-04-20 16:56:37 -0700 |
commit | dec196b4d913cde7b5e6030ca62fc3f3457ca642 (patch) | |
tree | 4a33eec696b0c96b52fb322ce5d58d172b0ff8c6 | |
parent | 038d9b3feed51bc12db95af0e0ef8b69776b42c9 (diff) | |
download | handlebars.js-dec196b4d913cde7b5e6030ca62fc3f3457ca642.zip handlebars.js-dec196b4d913cde7b5e6030ca62fc3f3457ca642.tar.gz handlebars.js-dec196b4d913cde7b5e6030ca62fc3f3457ca642.tar.bz2 |
The "if" block helper shouldn't treat empty arrays as truthy.
Given the data {foo: []}, the following template previously considered
foo to be truthy when it shouldn't have:
{{#if foo}}
You should not see me!
{{else}}
You should see me!
{{/if}}
-rw-r--r-- | lib/handlebars/base.js | 2 | ||||
-rw-r--r-- | spec/qunit_spec.js | 4 |
2 files changed, 5 insertions, 1 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 8be2634..387ff76 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -86,7 +86,7 @@ Handlebars.registerHelper('each', function(context, fn, inverse) { }); Handlebars.registerHelper('if', function(context, fn, inverse) { - if(!context || context == []) { + if(!context || Handlebars.Utils.isEmpty(context)) { return inverse(this); } else { return fn(this); diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index b8191ff..e144ba2 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -502,6 +502,10 @@ test("if", function() { "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"); + shouldCompileTo(string, {goodbye: ['foo'], world: "world"}, "GOODBYE cruel world!", + "if with non-empty array shows the contents"); + shouldCompileTo(string, {goodbye: [], world: "world"}, "cruel world!", + "if with empty array does not show the contents"); }); test("each", function() { |