Check for a value equals to in Ember Handlebar If block helper

If Statementember.jshandlebars.js

If Statement Problem Overview


How do we check for a value equality in ember.js's If-block helper?

{{#if person=="John"}}

How do we perform above in handlebars?

If Statement Solutions


Solution 1 - If Statement

The {{#if}} helper can only test for properties, not arbitrary expressions. The best thing to do in cases like this is therefore to write a property computing whatever conditional you want to test for.

personIsJohn: function() {
  return this.get('person') === 'John';
}.property('person')

Then do {{#if personIsJohn}}.

Note: If you find this too limiting, you can also register your own more powerful if helper.

Solution 2 - If Statement

Use an Ember.Component, thus avoid repetitively defining computed properties in your classes (like personIsJohn above):

// if_equal_component.js script
App.IfEqualComponent = Ember.Component.extend({
  isEqual: function() {
    return this.get('param1') === this.get('param2');
  }.property('param1', 'param2')
});

// if-equal.handlebars template
{{#if isEqual}}
  {{yield}}
{{/if}}

You can define the else part of the comparison, with an App.ElseEqualComponent:

// else_equal_component.js script
App.ElseEqualComponent = App.IfEqualComponent.extend();

// else-equal.handlebars template
{{#unless isEqual}}
  {{yield}}
{{/unless}}

##Usage:

{{#if-equal param1=person param2="John"}}
  Hi John!
{{/if-equal}}
{{#else-equal param1=person param2="John"}}
  Who are you?
{{/else-equal}}

Solution 3 - If Statement

If you're using HTMLBars (Ember version 1.10+), then you can use the Ember Truth Helper addon:

https://github.com/jmurphyau/ember-truth-helpers

Once installed, it'll be as simple as this:

{{#if (eq person "John")}} hello {{/if}}

Solution 4 - If Statement

although this problem can be solved using eq helper by writing

{{#if (eq person "John")}} hello {{/if}}

but for a general solution you can make your own helper which will take three params param[0] and param[2] being operand and param[1] being operator. Below is the helper file.

compare.js

import Ember from 'ember';

export function compare(params) {
  if(params[3]){  //handle case insensitive conditions if 4 param is passed.
    params[0]= params[0].toLowerCase();
    params[2]= params[2].toLowerCase();
  }
  let v1 = params[0];
  let operator = params[1];
  let v2 = params[2];
  switch (operator) {
    case '==':
      return (v1 == v2);
    case '!=':
      return (v1 != v2);
    case '===':
      return (v1 === v2);
    case '<':
      return (v1 < v2);
    case '<=':
      return (v1 <= v2);
    case '>':
      return (v1 > v2);
    case '>=':
      return (v1 >= v2);
    case '&&':
      return !!(v1 && v2);
    case '||':
      return !!(v1 || v2);
    default:
      return false;
  }
}

export default Ember.Helper.helper(compare);

now you can easily use it for multiple purpose.

for equality check.

{{#if (compare person '===' 'John')}} {{/if}}

for greater check.

{{#if (compare money '>' 300)}} {{/if}}

and so on.

Solution 5 - If Statement

Expanding on Jo Liss's answer, you can now do this using a computed property macro for more concise and readable code.

personIsJohn: function() {
  return this.get('person') === 'John';
}.property('person')

becomes

personIsJohn: Ember.computed.equal('person', 'John')

Relavent Docs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1338121View Question on Stackoverflow
Solution 1 - If StatementJo LissView Answer on Stackoverflow
Solution 2 - If StatementPanagiotis PanagiView Answer on Stackoverflow
Solution 3 - If StatementJohnny OshikaView Answer on Stackoverflow
Solution 4 - If StatementNitin9791View Answer on Stackoverflow
Solution 5 - If StatementWillView Answer on Stackoverflow