52

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?

Marco Prins
  • 7,189
  • 11
  • 41
  • 76
user1338121
  • 785
  • 1
  • 8
  • 13
  • There's also an answer for this question in [Logical operator in a handlebars.js {{#if}} conditional](http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional) – sumid May 03 '13 at 23:14

5 Answers5

77

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.

Community
  • 1
  • 1
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
  • @JoLiss Can you pass an arbitrary expression into the computed property from the template? – Sam Selikoff Jul 28 '13 at 02:33
  • No, computed properties cannot take arguments. So you'll need to make a separate CP for every value you want to test for: `personIsJohn`, `personIsMike`, ... Or register a new `if`-like helper -- see my edit above. – Jo Liss Jul 30 '13 at 00:07
  • Nice answer. They really need to put this in handlebars. – Edward Aug 22 '13 at 16:47
  • Thanks @JoLiss - this is exactly what I needed today to filter out all but one organisation among many. – Dave Everitt Oct 06 '14 at 15:46
  • Hi Jo, I am trying similar stuff but not successful.sectionfeedIsEqual: (function() { {{debugger}} return this.get('param1') === this.get('param2'); }.property('param1', 'param2') ) But I dont get values in param1 and param2 – plzdontkillme Nov 30 '14 at 07:48
26

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}}
Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
23

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}}
doublejosh
  • 5,548
  • 4
  • 39
  • 45
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
10

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.

Evgeniy Tkachenko
  • 1,733
  • 1
  • 17
  • 23
Nitin9791
  • 1,124
  • 1
  • 14
  • 17
4

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.

Will
  • 733
  • 8
  • 23