Last active
December 18, 2015 08:49
-
-
Save kdauzickas/5756878 to your computer and use it in GitHub Desktop.
Handlebars {{#or}} helper. When you need variable amount of values to OR together.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// var context = {foo: true} | |
// {{#or foo bar baz}}Yup{{else}}Nope{{/or}} -> Yup | |
// {{#or bar baz}}Yup{{else}}Nope{{/or}} -> Nope | |
Handlebars.registerHelper('or', function() { | |
var fncType = toString.call(function(){}); | |
for (var i = 0; i < arguments.length - 1; i++) { | |
var arg = arguments[i]; | |
if (toString.call(arg) === fncType) { | |
arg = arg.call(this); | |
} | |
if (arg && !Handlebars.Utils.isEmpty(arg)) return arguments[arguments.length - 1].fn(this); | |
} | |
return arguments[arguments.length - 1].inverse(this); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment