7

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

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

Thanks,

Juanitos

Juan Jardim
  • 2,232
  • 6
  • 28
  • 46

4 Answers4

10

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.

Anonymous
  • 6,181
  • 7
  • 45
  • 72
  • 5
    @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). http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional#comment-11152800 http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers – Anonymous May 16 '13 at 14:26
  • 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 – Ian Steffy Jun 25 '14 at 13:50
  • 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. – Kiruna Jun 24 '21 at 08:40
6

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.

Max Wallace
  • 3,609
  • 31
  • 42
1

For me, this worked:

Ember.computed.and('firstComputedProperty', 'secondComputedProperty')
Max Wallace
  • 3,609
  • 31
  • 42
spirito_libero
  • 1,206
  • 2
  • 13
  • 21
1

If you are using ember-template-helpers You can use evaluate in your template directly with:

{{#if (and ClientController.Client.number PhoneController.hasLinesToInstall)}}
...
{{/if}}}
Kiruna
  • 151
  • 9