Search code examples
ember.jshandlebars.js

How to insert extra item to template each


I have such template

<tbody>
  {{#each topics as |topic|}}
    {{topic-list-item topic=topic}}
  {{/each}}
</tbody>

I want to insert extra item into this rendering template list, something like this:

<tbody>
  {{#each topics as |topic|}}
    {{#if someCondition}}
      {{my-list-item}}
    {{else}}
      {{topic-list-item topic=topic}}
    {{/if}}
  {{/each}}
</tbody>

But the problem is this solution will skip some topic when someCondition is true, ie it will note insert but replace.

Is there a solution to add an item to template render list?

UPDATE: I want to insert {{my-list-item}} only once into render list if someCondition is true


Solution

  • If you want to dislay topic-list-item all the time irrespective of condition then, you can do it by removing else.

    <tbody>
          {{#each topics as |topic|}}
            {{#if someCondition}}
              {{my-list-item}}
            {{/if}}
            {{topic-list-item topic=topic}}        
          {{/each}}
        </tbody>