From cde93ca0c7f77ae8aa7626cc217eb586040732f8 Mon Sep 17 00:00:00 2001 From: Spencer Jones Date: Sun, 2 Aug 2020 15:09:35 -0400 Subject: [PATCH 1/2] docs(techniques/mvc): introduce @WithAlias() decorator Documents the @WithAlias() decorator and getUrl helper introduced in PR nestjs/nest#5117 --- content/techniques/mvc.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/content/techniques/mvc.md b/content/techniques/mvc.md index 3b2d213661..5509b07b10 100644 --- a/content/techniques/mvc.md +++ b/content/techniques/mvc.md @@ -94,6 +94,42 @@ In this code, we are specifying the template to use in the `@Render()` decorator While the application is running, open your browser and navigate to `http://localhost:3000`. You should see the `Hello world!` message. +#### Aliasing Routes + +You can alias routes with the `@WithAlias()` decorator in your controller, and then reference the alias inside your view. + +Suppose in your user controller, you have some routes to a list of users or to a specific user: +```typescript +@@filename(users.controller) +import { Get, Controller, Render, WithAlias } from '@nestjs/common'; +import { User } from './user.entity.ts'; + +@Controller('users') +export class UserController { + @Get() + @Render('users-list') + @WithAlias('hello') + root(): User[] { + return [ /* list of users */ ]; + } + + @Get('/:user') + @Render('user') + @WithAlias('hello') + getUser(): User { + return { /* user */ } + } +} +``` +Once you have registered route aliases, you can access them via the `getUrl(routeAlias: string, routeParams?: object): string` method that is injected into the template render context. + +```njk +Users +User With Specific ID +``` + +> warn **Note** Some templating engines (like handlebars) do not support evaluation of functions inside your template. + #### Dynamic template rendering If the application logic must dynamically decide which template to render, then we should use the `@Res()` decorator, and supply the view name in our route handler, rather than in the `@Render()` decorator: From 0c713f4629944e7295f0e2028625ab0b17bf4d68 Mon Sep 17 00:00:00 2001 From: Spencer Jones Date: Sun, 2 Aug 2020 16:38:30 -0400 Subject: [PATCH 2/2] docs(techniques/mvc): correct alias typo Correct route aliases to proper values reflecting the route --- content/techniques/mvc.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/techniques/mvc.md b/content/techniques/mvc.md index 5509b07b10..3dad4aca0f 100644 --- a/content/techniques/mvc.md +++ b/content/techniques/mvc.md @@ -108,14 +108,14 @@ import { User } from './user.entity.ts'; export class UserController { @Get() @Render('users-list') - @WithAlias('hello') + @WithAlias('users') root(): User[] { return [ /* list of users */ ]; } @Get('/:user') @Render('user') - @WithAlias('hello') + @WithAlias('user') getUser(): User { return { /* user */ } } @@ -124,7 +124,7 @@ export class UserController { Once you have registered route aliases, you can access them via the `getUrl(routeAlias: string, routeParams?: object): string` method that is injected into the template render context. ```njk -Users +Users User With Specific ID ```