Skip to content

Instantly share code, notes, and snippets.

@kdauzickas
Created June 11, 2013 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdauzickas/5756981 to your computer and use it in GitHub Desktop.
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.
// 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