Evaluate two conditions in handlebar using ember

14,208

Solution 1

I don't think it's possible to chain conditions like that in handlebars like that - I can't find anything about it in the documentation.

You could nest them though, like this:

{{#if ClientController.Client.number}}
    {{#if PhoneController.hasLinesToInstall}}
        ...
    {{/if}}
{{/if}}

That would achieve the same outcome.

Solution 2

It's not supported out-of-the-box, but you can use the addon https://github.com/jmurphyau/ember-truth-helpers:

ember install ember-truth-helpers

Then, in your template:

{{#if (and ClientController.Client.number PhoneController.hasLinesToInstall)}}
  ...
{{/if}}}

Previously, the community's understanding was that templates should be largely free of logic. Overtime, our viewpoint has shifted towards putting more declarative logic in templates-- along with ember-truth-helpers, ember-composable-helpers is a great example of this.

Solution 3

For me, this worked:

Ember.computed.and('firstComputedProperty', 'secondComputedProperty')
Share:
14,208
Juan Jardim
Author by

Juan Jardim

I'm a software engineer

Updated on June 04, 2022

Comments

  • Juan Jardim
    Juan Jardim over 1 year

    I was wondering if its possible to do something like this:

    {{#if ClientController.Client.number && PhoneController.hasLinesToInstall}}
    ...
    {{/if}}}
    

    Thanks,

    Juanitos

  • Anonymous
    Anonymous over 10 years
    @Juanito - I've been doing some more reading around and I've found a few people saying explicity that it isn't possible and that it's a design feature of handlebars (forces you to keep your templates clean and free of logic that belongs elsewhere). stackoverflow.com/questions/8853396/… blog.teamtreehouse.com/…
  • Ian Steffy
    Ian Steffy over 9 years
    Why do we use handlebars if they are so limited? They're a great idea for sure, but i'm a little sad that I have to use so many {{#if}} instead of {{elsif}} and {{#ifCond}} is valid but {{#unlessCond}} isn't
  • Kiruna
    Kiruna over 2 years
    All of that has been taken care of over the years, Never had an issue that handlebars couldn't do something i needed them to do in a nice way.