home
  • Blog
1.13
  • Getting Started
  • The Object Model
  • Templates
    • Handlebars Basics
    • The Application Template
    • Conditionals
    • Displaying a List of Items
    • Binding Element Attributes
    • Binding Element Class Names
    • Links
    • Actions
    • Input Helpers
    • Development Helpers
    • Rendering with Helpers
    • Writing Helpers
  • Routing
  • Components
  • Controllers
  • Models
  • Testing
  • Ember Inspector
  • Configuring Ember.js
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.13.0.
Go to v5.0.0

Displaying a List of Items

Edit pencil

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

<ul>
  {{#each people key="id" as |person|}}
    <li>Hello, {{person.name}}!</li>
  {{/each}}
</ul>

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

The above example will print a list like this:

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

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. Note that a [].push() will not update the helper. Adding items need to be done with [].pushObject, and related Ember Mutable Array methods so that Ember can observe the change.

Specifying Keys

The key option is used to tell Ember how to determine if the array being iterated over with {{#each}} has changed between renders. By helping Ember detect that some elements in the array are the same, DOM elements can be re-used, significantly improving rendering speed and preventing unexpected results. For example, here's the {{#each}} helper with its key set to id:

  {{#each people key="id" as |person|}}
  {{/each}}

When this {{#each}} re-renders, Ember will match up the previously rendered items (and reorder the generated DOM elements) based on each item's id property. Make sure the value you pass to key is unique!

There are a few special values for key: * @index - The index of the item in the array. * @item - The item in the array itself. This can only be used for arrays of strings or numbers. * @guid - Generate a unique identifier for each object (uses Ember.guidFor).

Empty Lists

The {{#each}} helper can have a matching {{else}}. The contents of this block will render if the collection is empty:

{{#each people key="id" as |person|}}
  Hello, {{person.name}}!
{{else}}
  Sorry, nobody is here.
{{/each}}
left arrow
Conditionals
Binding Element Attributes
right arrow
On this page

  • Specifying Keys
  • Empty Lists
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue