diff options
author | kpdecker <kpdecker@gmail.com> | 2013-10-14 21:36:46 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2013-10-14 21:38:19 -0500 |
commit | 84049bf0c5576727e9d6a0880abb123454d28861 (patch) | |
tree | 20700bf7f047fe822a76363cd125599e18f4b228 /lib/handlebars/base.js | |
parent | 2cd0995f2158371dc83590281839af9edca2c90a (diff) | |
download | handlebars.js-84049bf0c5576727e9d6a0880abb123454d28861.zip handlebars.js-84049bf0c5576727e9d6a0880abb123454d28861.tar.gz handlebars.js-84049bf0c5576727e9d6a0880abb123454d28861.tar.bz2 |
Add includeZero flag to if conditional
Allows for users who desire non-falsy handling of numbers to utilize if while maintaining the legacy if behavior.
Fixes #608
Diffstat (limited to 'lib/handlebars/base.js')
-rw-r--r-- | lib/handlebars/base.js | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index a1b3461..dcf446b 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -133,7 +133,10 @@ function registerDefaultHelpers(instance) { instance.registerHelper('if', function(conditional, options) { if (isFunction(conditional)) { conditional = conditional.call(this); } - if (Utils.isEmpty(conditional)) { + // Default behavior is to render the positive path if the value is truthy and not empty. + // The `includeZero` option may be set to treat the condtional as purely not empty based on the + // behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative. + if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) { return options.inverse(this); } else { return options.fn(this); @@ -141,7 +144,7 @@ function registerDefaultHelpers(instance) { }); instance.registerHelper('unless', function(conditional, options) { - return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn}); + return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash}); }); instance.registerHelper('with', function(context, options) { |