Double condition with #if
Asked Answered
C

2

8

How to do a double condition {{#if person && work}} ? The && seems not a valid operator

Chyme answered 4/1, 2013 at 0:21 Comment(0)
H
8

I don't believe handlebars supports multiple items in the {{#if}} statement (related: Logical operator in a handlebars.js {{#if}} conditional).

You could collapse the multiple values in your controller/view into a single computed property and check that single value in the template. This new computed property will update when either of the original values update:

App.ValuesTestController = Ember.Controller.extend({
    value1: false,
    value2: true,
    value1and2: function(){
            return this.get('value1') && this.get('value2');
    }.property('value1', 'value2')
});

Your template would look like this:

<div>{{#if value1 }}value 1 true{{/if}}</div>
<div>{{#if value2 }}value 2 true{{/if}}</div>
<div>{{#if value1and2 }}value 1 and 2 true{{/if}}</div>
Handicap answered 4/1, 2013 at 0:56 Comment(0)
P
3

Even if the @CraigTeegarden's answer is the elegant one, I have found some cases where this workaround has made my life easier, and therefor it has made me happier:

{{#if person}}
  {{#if work}}
    Yeah!
  {{/if}}
{{/if}}
Prisage answered 14/3, 2016 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.