Skip to content

Instantly share code, notes, and snippets.

@kdauzickas kdauzickas/helper.js
Created Jun 11, 2013

Embed
What would you like to do?
Handlebars {{#and}} helper. When you need variable amount of values to AND together.
// 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
You can’t perform that action at this time.