sudha
sudha

Reputation: 11

How to use and condition in if statement on meteor template?

I want to use the and condition in if statement like

{{#if requiredExp equals 0 and tile equals "SSE"}}
Expierince cannot be  equal to 0
{{/if}}

How to write this condition using and in meteor template?

Upvotes: 1

Views: 1387

Answers (1)

Tomas Hromnik
Tomas Hromnik

Reputation: 2200

Meteor is designed in a way that you can't put logic in your templates. You can create a simple helper like equals, but you cannot use logic operators like && and || in templates because this logic does not belong to templates.

You could create a global template helper equals for this:

Template.registerHelper('equals', function(param1, param2) {
  return param1 === param2;
});

So you can use it in your templates:

{{#if equals requiredExp 0}}
  {{#if equals tile "SSE"}} 
    Expierince cannot be equal to 0 
  {{/if}}
{{/if}}

Or, the better option is to create a helper that handles the logic you want:

Template.yourTemplateName.helpers({
  showExperienceNotification: function() {
    return this.requiredExp === 0 && this.tile === 'SSE';
  }
});

And use it in your template:

<template name="yourTemplateName">
  {{#if showExperienceNotification}}
    Experience cannot be equal to 0
  {{/if}}
</template>

You can access template data via keyword this in your helper: this.requiredExp and this.tile. Read more about Meteor templates and data context: https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/

Upvotes: 2

Related Questions