# Templating

is a decorator which can be used on a controller method (endpoint). This decorator will use the response returned by the method and will use the view to create the output.

WARNING

is available since v5.63.0. For older version use .

# Installation

This example uses EJS and consolidate. To use an other engine, see the documentation of the concerned project.

    TIP

    The configuration engine is exactly the same as Express configuration engine.

    # Usage

    A template engine like EJS or Handlebars can be used to change the response returned by your endpoint. Like Express.js, you need to configure the templating engine so that you can use it later with the decorator.

    Here is an example of a controller using the decorator:

    import {Controller, Get, View} from "@tsed/common";
    
    @Controller("/events")
    export class EventCtrl {
    
      @Get("/:id")
      @View("eventCard.ejs")
      public get(): any {
        return {startDate: new Date(), name: "MyEvent"};
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    And its view:

    <h1><%- name %></h1>
    <div>
        Start: <%- startDate %>
    </div>
    
    1
    2
    3
    4