diff options
author | kpdecker <kpdecker@gmail.com> | 2015-08-01 17:54:47 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2015-08-01 17:54:47 -0500 |
commit | 15b55a307b4f95d4a861df8b32c3c1ddb4825414 (patch) | |
tree | 5b5b7fcf8fe8de9aa380d3ef2522be7b93c3bb27 /lib/handlebars/helpers/if.js | |
parent | 231a8d7256d24c1a0287d67d393f06faa32751e8 (diff) | |
download | handlebars.js-15b55a307b4f95d4a861df8b32c3c1ddb4825414.zip handlebars.js-15b55a307b4f95d4a861df8b32c3c1ddb4825414.tar.gz handlebars.js-15b55a307b4f95d4a861df8b32c3c1ddb4825414.tar.bz2 |
Move helpers into separate modules
Diffstat (limited to 'lib/handlebars/helpers/if.js')
-rw-r--r-- | lib/handlebars/helpers/if.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/handlebars/helpers/if.js b/lib/handlebars/helpers/if.js new file mode 100644 index 0000000..11d08df --- /dev/null +++ b/lib/handlebars/helpers/if.js @@ -0,0 +1,20 @@ +import {isEmpty, isFunction} from '../utils'; + +export default function(instance) { + instance.registerHelper('if', function(conditional, options) { + if (isFunction(conditional)) { conditional = conditional.call(this); } + + // 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) || isEmpty(conditional)) { + return options.inverse(this); + } else { + return options.fn(this); + } + }); + + instance.registerHelper('unless', function(conditional, options) { + return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash}); + }); +} |