# HTML Views
Deepkit HTTP comes with a built-in HTML view rendering system. It's based on JSX and allows you to write your views in TypeScript. It's not a template engine with its own syntax, but a full-fledged TypeScript/JSX renderer provided by the package `@deepkit/template`.
It's meant for server-side rendering only and is not compatible with React. Components can be asynchronous and have access to the dependency injection container, so they can load their own data.
## Setup
Install `@deepkit/template` and enable JSX in your `tsconfig.json`:
```sh
npm install @deepkit/template
```
```json
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "@deepkit/template"
}
}
```
Files that contain JSX need the `.tsx` extension.
## JSX
JSX is a syntax extension to JavaScript and comes with TypeScript support out of the box. It allows you to write HTML in TypeScript.
When a route returns a JSX element, the HTTP module renders it to a string and sends it with the content type `text/html; charset=utf-8`.
```tsx app=app.ts
import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { http, HttpRouterRegistry } from '@deepkit/http';
export function View() {
return
```
## Components
### Function Components
A function component receives its attributes as the first argument (`props`) and the rendered children as the second argument. If a component accepts children, add `children?: any` to the props type so that TypeScript allows passing them.
```tsx
function Card(props: { title: string, children?: any }, children: any) {
return
{props.title}
{children}
;
}
router.get('/', () =>
Content of the card
);
```
Function components can be `async`. The renderer waits for them, so you can load data directly in the component.
```tsx
async function Weather(props: { city: string }) {
const data = await loadWeather(props.city);
return
{props.city}: {data.temperature}°C
;
}
```
### Dependency Injection
All parameters starting with the third one are resolved from the dependency injection container. This works for every provider registered in your app or modules.
```tsx
class UserRepository {
async findAll(): Promise<{ username: string }[]> {
return [{ username: 'Peter' }];
}
}
async function UserList(props: {}, children: any, users: UserRepository) {
const list = await users.findAll();
return
{list.map(user =>
{user.username}
)}
;
}
const app = new App({
providers: [UserRepository],
imports: [new FrameworkModule()],
});
app.get(HttpRouterRegistry).get('/users', () => );
```
Components are resolved from the root injector, not from the HTTP request scope. Providers with `scope: 'http'` like `HttpRequest` or `HttpResponse` can therefore not be injected into components. Read the values you need in the route and pass them as props instead.
```tsx
function Greeting(props: { name: string }) {
return
Hello {props.name}
;
}
router.get('/hello', (name: HttpQuery) => );
```
### Class Components
A class component is a class with a `render` method. Its constructor receives `props` and `children` as the first two arguments, all further arguments are injected from the dependency injection container. `render` can be `async`.
```tsx
class UserList {
constructor(
protected props: { limit: number },
protected children: any,
protected users: UserRepository,
) {
}
async render() {
const list = (await this.users.findAll()).slice(0, this.props.limit);
return
{list.map(user =>
{user.username}
)}
;
}
}
router.get('/users', () => );
```
## Layouts
There is no special layout system. A layout is a regular component that renders its children inside the page frame. Use `html()` to output the doctype, since it can't be written as JSX.
```tsx
import { html } from '@deepkit/template';
function Layout(props: { title: string, children?: any }, children: any) {
return <>
{html('')}
{props.title}{children}
>;
}
router.get('/', () =>
Welcome
);
```
## Escaping
Dynamic values are escaped automatically. In text content `&`, `<` and `>` are converted to `&`, `<` and `>`, so a value like `' is rendered as text
return