Adding Logical OR || AND && to Blaze conditionals

Conditionals in template logic often require the logical || or && operators, but by default Blaze does not provide support for this.

With the use of two simple helpers you can easily work around with this solution below.

Helpers:

Template.registerHelper('and', function(a, b) {  
  return a && b;
});

Template.registerHelper('or', function(a, b) {  
  return a || b;
});

Template Syntax:

{{#if or condition1 condition2}}
  Either condition1 or condition2 are true
{{/if}}

{{#if and condition1 condition2}}
  both condition2 and condition2 are true
{{/if}}

Real life example in template:

<div class="options">  
{{#if or $eq owner User._id isInRole 'admin'}}
 {{#afModal title="Update Product"  collection="Products" operation="update" doc=_id formId="updateProduct"}}
  <i class="fa fa-pencil" buttonContent="{{_ "edit_product"}}
{{/afModal}}"></i> {{_ "edit_product"}}
{{/afModal}}
{{/if}}
</div>  

Tom Whitbread

Read more posts by this author.

Subscribe to Meteor University

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!