Skip to content

Instantly share code, notes, and snippets.

@tomdale
Created December 8, 2012 21:52
Show Gist options
  • Save tomdale/4242136 to your computer and use it in GitHub Desktop.
Save tomdale/4242136 to your computer and use it in GitHub Desktop.
{{#each}} docs

Displaying a List of Items

If you need to enumerate over a list of objects, use Handlebar's {{#each}} helper:

<ul>
  {{#each people}}
    <li>Hello, {{name}}!</li>
  {{/each}}
</ul>

The template inside of the {{#each}} block will be repeated once for each item in the array, with the context of the template set to the current item.

The above example will print a list like this:

<ul>
  <li>Hello, Yehuda!</li>
  <li>Hello, Tom!</li>
  <li>Hello, Trek!</li>
</ul>

Like everything in Handlebars, the {{#each}} helper is bindings-aware. If your application adds a new item to the array, or removes an item, the DOM will be updated without having to write any code.

There is an alternative form of {{#each}} that does not change the scope of its inner template. This is useful for cases where you need to access a property from the outer scope within the loop.

{{name}}'s Friends

<ul>
  {{#each person in people}}
    <li>{{name}}'s friend {{person.name}}
  {{/each}}
<ul>

This would print a list like this:

Trek's Friends

<ul>
  <li>Trek's friend Yehuda</li>
  <li>Trek's friend Tom!</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment