Search code examples
ember.js

How to use multiple conditions using #if while forming a Table in ember JS?


I am forming a Table dynamically using Ember

How can i use nested if while forming the table

Something like this

{{#if ismorethanone && model.hasSize}}
                            <td> Hai  </td>
                            {{else}}
                                <td> Bye </td>
                        {{/if}}

My code

<script type="text/x-handlebars" data-template-name="index">
   <table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th>Participant</th>
        </tr>
    </thead>
    <tbody id="ParticipantList">
        {{#each person in model}}
            <tr>
                <td> {{person.name}}</td>
                <td> {{person.city}}</td>
                {{#if ismorethanone}}
                     <td><img src="image.jpg" alt="Remove" {{action "removeUser" person.name}}></td>
                {{/if}}
            </tr>
        {{/each}}
    </tbody>
   </table>
</script>

http://jsfiddle.net/6Evrq/1937/


Solution

  • Either use ember-truth-helpers:

    {{#if (and ismorethanone && model.hasSize)}}
      foo
    {{else}}
      bar
    {{/if}
    

    or just nest the ifs:

    {{#if foo}}
      {{#if bar}}
        one
      {{else}}
        two
      {{/if}}
    {{/if}}