home
  • Blog
1.12
  • Getting Started
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
    • Introduction
    • Defining Your Routes
    • Generated Objects
    • Specifying a Route's Model
    • Setting Up a Controller
    • Rendering a Template
    • Redirecting
    • Specifying the URL Type
    • Query Parameters
    • Asynchronous Routing
    • Loading / Error Substates
    • Preventing and Retrying Transitions
  • Components
  • Controllers
  • Models
  • Views
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Ember Inspector
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.12.0.
Go to v5.0.0

Rendering a Template

Edit pencil

One of the most important jobs of a route handler is rendering the appropriate template to the screen.

By default, a route handler will render the template into the closest parent with a template.

app/router.js
Router.map(function() {
  this.route('posts');
});
app/routes/posts.js
export default Ember.Route.extend();

If you want to render a template other than the one associated with the route handler, implement the renderTemplate hook:

app/routes/post.js
export default Ember.Route.extend({
  renderTemplate: function() {
    this.render('favoritePost');
  }
});

If you want to use a different controller than the route handler's controller, pass the controller's name in the controller option:

app/routes/posts.js
export default Ember.Route.extend({
  renderTemplate: function() {
    this.render({ controller: 'favoritePost' });
  }
});

Ember allows you to name your outlets. For instance, this code allows you to specify two outlets with distinct names:

app/templates/application.hbs
<div class="toolbar">{{outlet "toolbar"}}</div>
<div class="sidebar">{{outlet "sidebar"}}</div>

So, if you want to render your posts into the sidebar outlet, use code like this:

app/routes/posts.js
export default Ember.Route.extend({
  renderTemplate: function() {
    this.render({ outlet: 'sidebar' });
  }
});

All of the options described above can be used together in whatever combination you'd like:

app/routes/posts.js
export default Ember.Route.extend({
  renderTemplate: function() {
    var controller = this.controllerFor('favoritePost');

    // Render the `favoritePost` template into
    // the outlet `posts`, and use the `favoritePost`
    // controller.
    this.render('favoritePost', {
      outlet: 'posts',
      controller: controller
    });
  }
});

If you want to render two different templates into outlets of two different rendered templates of a route:

app/routes/post.js
export default Ember.Route.extend({
  renderTemplate: function() {
    this.render('favoritePost', {   // the template to render
      into: 'posts',                // the template to render into
      outlet: 'posts',              // the name of the outlet in that template
      controller: 'blogPost'        // the controller to use for the template
    });
    this.render('comments', {
      into: 'favoritePost',
      outlet: 'comment',
      controller: 'blogPost'
    });
  }
});
left arrow
Setting Up a Controller
Redirecting
right arrow
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