You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/release/routing/defining-your-routes.md
+79-45Lines changed: 79 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ To define a route, run
8
8
ember generate route route-name
9
9
```
10
10
11
-
This creates a route file at `app/routes/route-name.js`, a template for the route at `app/templates/route-name.hbs`,
11
+
This creates a route file at `app/routes/route-name.js`, a template for the route at `app/templates/route-name.gjs`,
12
12
and a unit test file at `tests/unit/routes/route-name-test.js`.
13
13
It also adds the route to the router.
14
14
@@ -42,15 +42,19 @@ Router.map(function() {
42
42
Inside your templates, you can use [`<LinkTo />`](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/LinkTo?anchor=LinkTo) to navigate between
43
43
routes, using the name that you provided to the `route` method.
44
44
45
-
```handlebars
46
-
<LinkTo @route="index">
47
-
<img class="logo">
48
-
</LinkTo>
45
+
```gjs
46
+
import { LinkTo } from '@ember/routing';
49
47
50
-
<nav>
51
-
<LinkTo @route="about">About</LinkTo>
52
-
<LinkTo @route="favorites">Favorites</LinkTo>
53
-
</nav>
48
+
<template>
49
+
<LinkTo @route="index">
50
+
<img class="logo">
51
+
</LinkTo>
52
+
53
+
<nav>
54
+
<LinkTo @route="about">About</LinkTo>
55
+
<LinkTo @route="favorites">Favorites</LinkTo>
56
+
</nav>
57
+
</template>
54
58
```
55
59
56
60
The `<LinkTo />` component will also add an `active` class to the link that
@@ -65,7 +69,7 @@ Router.map(function() {
65
69
```
66
70
67
71
The route defined above will by default use the `blog-post.js` route handler,
68
-
the `blog-post.hbs` template, and be referred to as `blog-post` in any
72
+
the `blog-post.gjs` template, and be referred to as `blog-post` in any
69
73
`<LinkTo />` components.
70
74
71
75
Multi-word route names that break this convention, such as:
@@ -77,7 +81,7 @@ Router.map(function() {
77
81
```
78
82
79
83
will still by default use the `blog-post.js` route handler and the
80
-
`blog-post.hbs` template, but will be referred to as `blog_post` in any
84
+
`blog-post.gjs` template, but will be referred to as `blog_post` in any
81
85
`<LinkTo />` components.
82
86
83
87
## Nested Routes
@@ -109,17 +113,22 @@ ember generate route posts/new
109
113
And then add the `{{outlet}}` helper to your template where you want the nested
110
114
template to display. You can also add a page title with the current page name (using [page-title helper](../../accessibility/page-template-considerations/#toc_page-title)), this will help users with assistive technology know where they are in the website.
111
115
112
-
```handlebars {data-filename=templates/posts.hbs}
113
-
{{page-title "Posts - Site Title"}}
114
-
<h1>Posts</h1>
115
-
{{!-- Display posts and other content --}}
116
-
{{outlet}}
116
+
```gjs {data-filename=templates/posts.gjs}
117
+
import { pageTitle } from 'ember-page-title'
118
+
119
+
<template>
120
+
{{pageTitle "Posts - Site Title"}}
121
+
122
+
<h1>Posts</h1>
123
+
{{!-- Display posts and other content --}}
124
+
{{outlet}}
125
+
</template>
117
126
```
118
127
119
128
This generates a route for `/posts` and for `/posts/new`. When a user
120
-
visits `/posts`, they'll simply see the `posts.hbs` template. (Below, [index
129
+
visits `/posts`, they'll simply see the `posts.gjs` template. (Below, [index
121
130
routes](#toc_index-routes) explains an important addition to this.) When the
122
-
user visits `posts/new`, they'll see the `posts/new.hbs` template rendered into
131
+
user visits `posts/new`, they'll see the `posts/new.gjs` template rendered into
123
132
the `{{outlet}}` of the `posts` template.
124
133
125
134
A nested route name includes the names of its ancestors.
@@ -134,7 +143,7 @@ routes, it will load a template with the same name (`application` in
134
143
this case) by default.
135
144
You should put your header, footer, and any other decorative content
136
145
here. All other routes will render
137
-
their templates into the `application.hbs` template's `{{outlet}}`.
146
+
their templates into the `application.gjs` template's `{{outlet}}`.
138
147
139
148
This route is part of every application, so you don't need to
140
149
specify it in your `app/router.js`.
@@ -200,38 +209,51 @@ replace the `{{outlet}}` in the `posts` template with the
200
209
201
210
The following scenarios may help with understanding the `index` route:
202
211
203
-
- The top-level index route is analogous to `index.html`. For example, when someone visits `https://some-ember-app.com`, the contents of the `template/index.hbs` file will be rendered. There is no need to add an entry `this.route('index', { path: '/' });` in `app/router.js` file. The `index` route is implicitly included in order to help reduce verbose declarations in the `app/router.js`. The `app/router.js` file could be empty, and the `index` would still be shown:
212
+
- The top-level index route is analogous to `index.html`. For example, when someone visits `https://some-ember-app.com`, the contents of the `template/index.gjs` file will be rendered. There is no need to add an entry `this.route('index', { path: '/' });` in `app/router.js` file. The `index` route is implicitly included in order to help reduce verbose declarations in the `app/router.js`. The `app/router.js` file could be empty, and the `index` would still be shown:
204
213
205
214
```javascript {data-filename=app/router.js}
206
215
Router.map(function() {
207
216
});
208
217
```
209
-
- When a user navigates to `/posts`, the contents of `index.hbs` will be rendered. This is similar to a user navigating to the child route of `/posts`. `/posts/index` is a child route like `/posts/comments` or `/posts/likes`.
218
+
- When a user navigates to `/posts`, the contents of `index.gjs` will be rendered. This is similar to a user navigating to the child route of `/posts`. `/posts/index` is a child route like `/posts/comments` or `/posts/likes`.
210
219
211
220
### When to use an index route
212
221
213
222
The index route is most helpful for rendering a view when the route has [dynamic segments](#toc_dynamic-segments) defined in it or there are nested routes. In other words, an `index` template is used to show content that should not be present on sibling and child routes. For example, a blog app might have an `index` route that shows a list of all posts, but if a user clicks on a post, they should only see the content for the individual post. Here is how that looks in practice:
214
223
215
-
A `templates/posts.hbs` file has the following:
224
+
A `templates/posts.gjs` file has the following:
225
+
226
+
```gjs {data-filename=templates/posts.gjs}
227
+
228
+
import { pageTitle } from 'ember-page-title'
216
229
217
-
```handlebars {data-filename=templates/posts.hbs}
218
-
{{page-title "Posts"}}
219
-
<h1>This is the posts template, containing headers to show on all child routes</h1>
220
-
{{outlet}}
230
+
<template>
231
+
{{pageTitle "Posts"}}
232
+
<h1>This is the posts template, containing headers to show on all child routes</h1>
233
+
{{outlet}}
234
+
</template>
221
235
```
222
236
223
-
The `templates/posts/index.hbs` file has the following:
237
+
The `templates/posts/index.gjs` file has the following:
Behind the scenes, what is happening is that the [route's controller](https://api.emberjs.com/ember/release/classes/Route/methods/setupController?anchor=setupController) receives the results of the model hook, and Ember makes the model hook results available to the template. Your app may not have a controller file for the route, but the behavior is the same regardless.
If you decide to pass the entire model, be sure to cover this behavior in your [application tests](../../testing/testing-application/).
241
249
242
250
If a route you are trying to link to has multiple dynamic segments, like `/photos/4/comments/18`, be sure to specify all the necessary information for each segment:
0 commit comments