Last active
December 31, 2021 11:19
-
-
Save victorbstan/3462664de456146e87db to your computer and use it in GitHub Desktop.
Multi argument 'if' & 'unless' conditionals in Spacebars / Handlebars for Meteor.js
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
<!-- | |
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}} |
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
// 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