Created
June 11, 2013 13:45
-
-
Save kdauzickas/5756981 to your computer and use it in GitHub Desktop.
Handlebars {{#and}} helper. When you need variable amount of values to AND 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, bar: false, baz: true} | |
// {{#and foo baz}}Yup{{else}}Nope{{/and}} -> Yup | |
// {{#and bar baz qux}}Yup{{else}}Nope{{/and}} -> Nope | |
Handlebars.registerHelper('and', 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].inverse(this); | |
} | |
return arguments[arguments.length - 1].fn(this); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment