Skip to content

Instantly share code, notes, and snippets.

@victorbstan
Last active Dec 31, 2021
Embed
What would you like to do?
Multi argument 'if' & 'unless' conditionals in Spacebars / Handlebars for Meteor.js
<!--
Usage examples
-->
<!-- IF ANY -->
{{#if any truthyOne truthyTwo falsyThree}}
You will see this.
{{/if}}
{{#if any falsyOne falsyTwo falsyThree}}
You won't see this.
{{/if}}
<!-- UNLESS ANY -->
{{#unless any falsyOne falsyTwo falsyThree}}
You will see this.
{{/unless}}
{{#unless any falsyOne falsyTwo truthyThree}}
You won't see this.
{{/unless}}
// dependency: https://lodash.com
Template.registerHelper('any', function () {
var any = false;
var cleanArgs = lodash.filter(arguments, function ( arg ) {
if ( lodash.isArray( arg ) || arg === true )
return true;
});
for ( var i = 0; i < cleanArgs.length; i++ ) {
if ( ! lodash.isEmpty( cleanArgs[i] ) || cleanArgs[i] === true )
any = true;
};
return any;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment