56

Lets say I have an array like this in ember controller,

selectedUsers: ["Popeye", "Sulley", "Gru"];

Now, how can i render each users in an unordered list using handlebars? Can i use the {{#Each}} helper?

1
  • 1
    What's wrong with each helper? Commented Dec 25, 2013 at 13:53

2 Answers 2

104

Yes, you should use an each loop:

<ul>
{{#each selectedUsers}}
    <li>{{ this }}</li>
{{/each}}
</ul>

From the docs:

You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over.

<ul class="people_list">
  {{#each people}}
    <li>{{this}}</li>
  {{/each}}
</ul>

when used with this context:

{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley"
  ]
}

will result in:

<ul class="people_list">
  <li>Yehuda Katz</li>
  <li>Alan Johnson</li>
  <li>Charles Jolley</li>
</ul>

You can use the this expression in any context to reference the current context.

Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to use a variable name for the item, rather than this? Eg, selectedUser?
4

This also works

<ul>
{{#each this}}
<li>{{ this }}</li>
{{/each}}
</ul>

1 Comment

I get "Unclosed section "each this""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.