Skip to content

Instantly share code, notes, and snippets.

@victorbstan
Last active December 31, 2021 11:19
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 victorbstan/3462664de456146e87db to your computer and use it in GitHub Desktop.
Save victorbstan/3462664de456146e87db to your computer and use it in GitHub Desktop.
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