3

Is there a way to do a conditional "and" or "or" in a Meteor template? What I am trying to do is something like this:

{{#if isInRole 'ADMIN' || isInRole 'INSPECTOR'}}
  ...do some stuff
{{/if}}

using helpers provided by a 3rd party package alanning:roles. This seems to have come up multiple times in my coding with Meteor and I can and have worked around it, usually by duplication of the block code, but it would be really nice if there already existed some way to handle an OR or AND condition without crazy gyrations.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79

1 Answers1

2

Meteor using spacebar which is based on handlebars. In handlebars, there are now direct way for logical operators.

You can do small workaround to handle the or by creating a Template.helper

Template.registerHelper("equals_or", function(param, arr) {
   arr = arr.split(",");
   if (arr.indexOf(param) !== -1) {
      return true;
   } 
   else {
     return false;
   }
});

Then in the HTML you can do

{{#if equals_or isRole "ADMIN,INSPECTOR"}}
  ...do some stuff
{{else}}
   ...do some other stuff
{{/if}}

I might not be a perfect solution, but it is doing the job.

Check this answer too.

Community
  • 1
  • 1
Rajanand02
  • 1,303
  • 13
  • 19
  • Can a helper call another helper like that? isInRole is a global template helper created by the `alanning:roles` package. My other alternative for this particular instance is to write more own `isInAnyRoles` that accepts an array instead of a single role. Or maybe I should just modify the `alanning:roles` and give back to the community. Probably the better solution for everyone. – CodeChimp Nov 15 '14 at 00:10
  • @CodeChimp did you actually update alanning:roles in the end? Cos the comma separated functionality is now in! http://alanning.github.io/meteor-roles/classes/UIHelpers.html – Alveoli Dec 16 '15 at 10:21