---
title: Views
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/views/
updated: 2026-11-18T16:57:29.576Z
---

# Views

> The view commands, the view object, helpers and TEMPLATE().

Views are HTML files in `/views/` with `@{...}` commands, rendered by `controller.view()` and `F.view()`. This page lists every command, the methods of the view object, the helpers and `TEMPLATE()`. The guide [Views](../../guides/views/) explains how they work together, with examples.

## API

### controller.view([name], [model])

Type: `method`

Renders a view and sends it as the HTML response.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no |  | The name of the view without `.html`, for example `products/detail`. |
| `model` | `Object` | no |  | The model, available as `model` in the view. |

Returns: `View` The view object.

The view is read from `/views/{name}.html`, or from `/plugins/` when the name starts with `#`. It is rendered into the layout that was set with `controller.layout()` (or `layout` by default) and sent with `controller.html()`. Without a name the method only returns a new view object, which you render yourself.

Example: Render a view
```js
exports.install = function() {
	ROUTE('GET /products/{id}/', detail);
};

function detail($) {
	var id = $.params.id;
	$.view('products/detail', { id: id });
}
```

Example: Render it yourself
```js
function preview($) {
	var view = $.view();
	view.layout('');
	$.html(view.render('preview', { name: 'Test' }));
}
```

### controller.layout(name)

Type: `method`

Sets the layout for the next `controller.view()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the layout view. |

Call it before `controller.view()`. An empty string keeps the default layout; for a view without a layout use `@{layout('')}` in the view or `view.layout('')`. A view can still change it with `@{layout()}`.

Example: Use another layout
```js
function admin($) {
	$.layout('layout-admin');
	$.view('admin/index');
}
```

### F.view(name, [model], [prepare])

Type: `method`

Renders a view outside of a request and returns the HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the view. |
| `model` | `Object` | no |  | The model. |
| `prepare` | `Function` | no |  | `function(view)` called before the rendering, to set `view.language`, `view.repository` or the layout. |

Returns: `String` The rendered HTML.

The view has no controller, so `controller`, `user`, `query` and the other request values are empty. The view is rendered into its layout as usual; use `@{layout('')}` in the view, or `view.layout('')` in `prepare`, for a fragment without a layout.

Example: Render an e-mail body
```js
var html = F.view('mail/order', { id: 'A100' }, function(view) {
	view.language = 'sk';
	view.layout('');
});
```

### new F.TViewEngine.View([controller])

Type: `class`

The object that renders a view; it is `self` inside the compiled view.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `controller` | `Controller` | no |  | The controller of the request. |

`controller.view()` and `F.view()` create it for you. Its properties are `controller`, `model`, `repository` (`{ layout: 'layout' }` at the start), `language`, `url` and `query`. Its methods are the view commands: `layout()`, `title()`, `description()`, `keywords()`, `meta()`, `json()`, `href()`, `import()`, `section()`, `helper()`, `hostname()` and `view()`.

Example: Set values before rendering
```js
function page($) {
	var view = $.view();
	view.repository.menu = 'products';
	$.html(view.render('products', { items: [] }));
}
```

### view.render(name, [model])

Type: `method`

Renders a view with its layout and returns the HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the view. |
| `model` | `Object` | no |  | The model. |

Returns: `String` The HTML.

The file is `/views/{name}.html` (or `/plugins/{name}.html` for a name with `#`). When `repository.layout` is not empty, the result is rendered into that layout.

Example: Render
```js
var view = new F.TViewEngine.View();
view.layout('');
var html = view.render('fragments/card', { name: 'Pen' });
```

### view.repository

Type: `property`

An object shared by the view, its partial views and the layout.

Returns: `Object`

It holds `layout`, `title`, `description`, `keywords`, the sections (`section_{name}`) and any values you add. In the view it is the variable `repository`. Use it to pass values from a view to its layout.

Example: View to layout
```html
<!-- view -->
@{repository.menu = 'products'}

<!-- layout -->
<nav class="@{repository.menu}">...</nav>
```

### view.language

Type: `property`

The language used for `@(...)` translations.

Returns: `String`

It is the language of the request in `controller.view()` and an empty string in `F.view()`. Set it before rendering; each language has its own compiled copy of the view, and `@{language}` prints it.

Example: Language in F.view()
```js
var html = F.view('mail/welcome', model, function(view) {
	view.language = 'de';
});
```

### @{expression}

Type: `command`

Prints the value of a JavaScript expression, HTML-encoded.

Any expression works: properties, method calls, operators. `null` and `undefined` print as an empty string. An assignment (`=`, `+=`, `++`…) to `model`, `repository`, `config`, `query`, `user`, `session` or `controller` is evaluated and prints nothing. `@{print(...)}` and `@{console.log(...)}` print nothing in the HTML.

Example: Values
```html
<h1>@{model.name}</h1>
<p>@{model.items.length} items</p>
<p>@{model.price.format(2)}</p>
@{repository.count = model.items.length}
```

### @{!expression}

Type: `command`

Prints the value of an expression without HTML encoding.

Use it for HTML you trust, for example content of a CMS stored by your administrators.

Example: Raw HTML
```html
<article>@{!model.body}</article>
```

### @{model}

Type: `command`

The model passed to the view.

It is the second argument of `controller.view()`, `F.view()`, `view.render()` or `@{view()}`. The layout does not receive the model.

Example: Read the model
```html
<h1>@{model.title}</h1>
@{if model.user}<p>@{model.user.name}</p>@{fi}
```

### @{repository}

Type: `command`

The shared object of the view, its partial views and the layout.

See `view.repository`. Assign values with `@{repository.key = value}`; the assignment prints nothing.

Example: Set and read
```html
@{repository.menu = 'contact'}
<li class="@{repository.menu === 'contact' ? 'active' : ''}">Contact</li>
```

### @{config.key} / @{'%key'}

Type: `command`

Prints a configuration value.

`config` is `CONF`, read on every render. `@{'%key'}` reads the value once, when the view is compiled, and puts it into the view as text.

Example: Configuration
```html
<title>@{config.name}</title>
<footer>@{'%name'} @{config.version}</footer>
```

### @{query} / @{body} / @{files} / @{user} / @{session} / @{controller}

Type: `command`

The data of the current request.

`query` is the parsed query string, `body` the parsed body, `files` the uploaded files, `user` and `session` the values of your authorization, and `controller` the controller itself. In `F.view()` they are empty. In a layout, `@{body}` without a property prints the HTML of the view instead.

Example: Request values
```html
@{if user}
	<p>Hello @{user.name}</p>
@{else}
	<a href="/login/">Sign in</a>
@{fi}
<input name="q" value="@{query.q}" />
```

### @{body}

Type: `command`

In a layout, prints the HTML of the rendered view.

The output is not encoded. With a property (`@{body.name}`) it reads the body of the request instead.

Example: Layout
```html
<!DOCTYPE html>
<html>
<head>@{import('meta')}</head>
<body>
	@{body}
</body>
</html>
```

### @{language}

Type: `command`

The language of the view.

The language of the view (`view.language`): the language of the request (`controller.language`) in `controller.view()`, the value you set in the `prepare` function of `F.view()`, and an empty string by default.

Example: Language attribute
```html
<html lang="@{language || 'en'}">
```

Example: In F.view()
```js
var html = F.view('mail/welcome', model, function(view) {
	view.language = 'de';
});
// <html lang="de">
```

### @{url}

Type: `command`

The URL path of the current request.

It is `controller.url`, the path as requested (the letter case is kept) with a trailing slash and without the query string. `url` also works inside expressions and conditions: `@{if url === '/'}`. In `F.view()` there is no request: `@{url}` prints an empty string and `url` in an expression is `undefined`.

Example: Active link
```html
<a href="/products/" class="@{url === '/products/' ? 'active' : ''}">Products</a>
```

### @{hostname([path])}

Type: `command`

Prints the absolute URL of the current page or of another path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | A path that starts with `/`. Without it, the path of the current request. |

Returns: `String` The absolute URL.

`@{hostname()}` prints the protocol, the host and the path of the current request, without the query string (`https://shop.example.com/products/`). `@{hostname('/img/share.png')}` prints the protocol and the host with the given path. It calls `controller.hostname()`, see [Controller](../controller/). Write the parentheses: `@{hostname}` alone does not call the function.

In `F.view()` there is no request, so it prints only `path` (or an empty string).

Example: Canonical link and share image
```html
<link rel="canonical" href="@{hostname()}" />
<meta property="og:image" content="@{hostname('/img/share.png')}" />
```

### @{cookie(name)}

Type: `command`

Prints the value of a cookie of the request, HTML-encoded.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the cookie. |

Without a controller it prints an empty string.

Example: Read a cookie
```html
<body class="theme-@{cookie('theme')}">
```

### @{now}

Type: `command`

A new `Date` with the current time.

Use properties and methods of the date directly: `@{now.getFullYear()}`.

Example: Copyright
```html
<footer>&copy; @{now.getFullYear()} @{config.name}</footer>
```

### @{root}

Type: `command`

Prints `CONF.$root` without its trailing slash.

Use it for links when the application runs under a sub-path. It prints an empty string when `CONF.$root` is not set.

Example: Link under a sub-path
```html
<a href="@{root}/products/">Products</a>
```

### @{if condition} ... @{else if condition} ... @{else} ... @{fi}

Type: `command`

Renders a part of the view only when a condition is true.

The condition is any JavaScript expression. `@{endif}` is the same as `@{fi}`. `@{if(condition)}` with parentheses works as well.

Example: Condition
```html
@{if model.stock > 10}
	<b>In stock</b>
@{else if model.stock > 0}
	<b>Last pieces</b>
@{else}
	<b>Sold out</b>
@{fi}
```

### @{foreach item in source} ... @{end}

Type: `command`

Repeats a part of the view for every item of an array or an object.

For an object, each item is `{ key, value }`. An empty source renders nothing. `@{foreach var item in source}` works as well. Inside the loop, `@{index}` is the zero-based position, and `@{continue}` and `@{break}` skip an item or stop the loop. Loops can be nested.

Example: Array
```html
<ul>
@{foreach item in model.items}
	@{if item.hidden}@{continue}@{fi}
	<li>@{index + 1}. @{item.name}</li>
@{end}
</ul>
```

Example: Object
```html
@{foreach prop in model.settings}
	<dt>@{prop.key}</dt><dd>@{prop.value}</dd>
@{end}
```

### @{index}

Type: `command`

The zero-based position of the current item in `@{foreach}`.

Example: Numbered rows
```html
@{foreach row in model.rows}
	<tr class="@{index % 2 ? 'odd' : 'even'}"><td>@{index + 1}</td><td>@{row.name}</td></tr>
@{end}
```

### @{layout(name)}

Type: `command`

Sets the layout of the view.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the layout view, or an empty string. |

The default layout is `layout` (`/views/layout.html`). An empty string renders the view without a layout. The command prints nothing.

Example: Another layout
```html
@{layout('layout-print')}
<h1>@{model.title}</h1>
```

Example: No layout
```html
@{layout('')}
<div class="card">@{model.name}</div>
```

### @{view(name, [model])}

Type: `command`

Renders another view in place (a partial view).

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the view. |
| `model` | `Object` | no |  | The model of the partial view. |

The partial view gets its own model and no layout. It shares `repository` and the sections with the view that renders it. A name that starts with `#` reads the file from `/plugins/`.

Example: Partial views
```html
@{foreach item in model.items}
	@{view('partials/card', item)}
@{end}
@{view('#blog/sidebar')}
```

### @{section name} ... @{end} / @{section(name)}

Type: `command`

Collects HTML in a view and prints it in another place, usually the layout.

`@{section name}` … `@{end}` adds its HTML to the section; several blocks with the same name are joined. `@{section('name')}` prints the section, or nothing when it is empty. The section must be filled before it is printed, which is the case for a layout.

Example: Scripts for the layout
```html
<!-- view -->
@{section scripts}
	<script src="/js/chart.js"></script>
@{end}

<!-- layout, before </body> -->
@{section('scripts')}
```

### view.section(name, [value], [replace])

Type: `method`

Reads or fills a section from JavaScript.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the section. |
| `value` | `String` | no |  | The HTML to add. |
| `replace` | `Boolean` | no | `false` | Replace the current content. |

Returns: `String|View` The section when reading; the view when writing.

Without `value` it returns the HTML of the section. With `value` it appends the HTML to the section, or replaces it when `replace` is `true`.

Example: Fill a section
```js
function page($) {
	var view = $.view();
	view.section('scripts', '<script src="/js/page.js"></script>');
	$.html(view.render('page'));
}
```

### @{title(value)} / @{title}

Type: `command`

Sets or prints the title of the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The title. |

`@{title('Text')}` stores the text in `repository.title` and prints nothing. `@{title}` prints it HTML-encoded, `@{!title}` without encoding.

Example: Title
```html
@{title('Products / ' + model.category)}
<h1>@{title}</h1>
```

### @{title2(value)}

Type: `command`

Appends text to the title of the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The text to append. |

Adds `value` to the end of `repository.title` (or sets it when there is no title yet) and prints nothing. Like `title()`, the text is stored as it is and encoded once when it is printed. An empty value changes nothing.

Example: Title in two steps
```html
@{title('Products')}
@{if model}@{title2(' / ' + model.name)}@{fi}
<h1>@{title}</h1>  <!-- Products / Pen &amp; paper -->
```

### @{description(value)} / @{description}

Type: `command`

Sets or prints the description of the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The description. |

The value goes to `repository.description`; `@{import('meta')}` prints it as a `<meta name="description">` tag.

Example: Description
```html
@{description('Pens, pencils and paper for the office.')}
```

### @{keywords(value)} / @{keywords}

Type: `command`

Sets or prints the keywords of the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Array` | yes |  | The keywords. |

An array is joined with `, `. The value goes to `repository.keywords`.

Example: Keywords
```html
@{keywords(['pens', 'paper', 'office'])}
```

### @{meta([title], [description], [keywords], [image])} / @{meta}

Type: `command`

Sets the title, description, keywords and image at once, or prints the meta tags.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `title` | `String` | no |  | The title. |
| `description` | `String` | no |  | The description. |
| `keywords` | `String|Array` | no |  | The keywords. |
| `image` | `String` | no |  | An absolute URL or a path of an image. |

With arguments it stores the values in `repository` as they are and prints nothing; they are HTML-encoded once, when the tags are printed. Without arguments it prints the same tags as `@{import('meta')}`: `<title>`, the description and keywords tags, and `og:image` and `twitter:image` for the image. A relative image gets the host name of the request; without a request no image tags are printed. The title gets ` - ` and `CONF.name` appended, except on the URL `/` and when `CONF.$customtitles` is `true`; without a title it is `CONF.name`.

Example: Set and print
```html
<!-- view -->
@{meta(model.name, model.summary, model.tags, model.photo)}

<!-- layout -->
<head>@{meta}</head>
```

### CONF.$customtitles

Type: `config`

Prints the page title without the application name.

Returns: `Boolean` Default `false`.

By default the title tag of `@{meta}` and `@{import('meta')}` is `Title - CONF.name`. With `true` the title is printed as you set it.

Example: /config
```text
$customtitles : true
```

### @{import(name, [name2], ...)}

Type: `command`

Prints the tags for static files, the meta tags or a favicon.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | A file name, `meta` or a favicon name. |

Each argument gives one tag:

- `meta` prints the meta tags (see `@{meta}`).
- `favicon.ico`, `favicon.png` or `favicon.gif` prints a `<link rel="icon">` tag.
- A name with `.css` prints a stylesheet link from `/css/`, other names a script from `/js/`. A path that starts with `/` (or a virtual route) is used as it is.
- An `@` before the name appends `?ts=` with the start time of the application.
- Names joined with `+` are merged into one minified file (see the next entry).

The tags are cached, so the command is cheap.

Example: Head of a layout
```html
<head>
	@{import('meta', 'favicon.ico', '@default.css', 'ui.js', '/vendor/editor.js')}
</head>
```

### @{import('a.css + b.css')}

Type: `command`

Merges several CSS or JavaScript files into one minified file.

The names joined with `+` are merged with `MERGE()` into a file with a generated name, such as `/xkcqdmatno-min.css`, which the framework serves. A name with `.css` makes a stylesheet, otherwise a script. A file name is looked up in `/public/css/` or `/public/js/` by its extension, a path that starts with `/` in `/public/`, and a URL is downloaded. An `@` at the start appends `?ts=`.

Example: One CSS and one JS file
```html
@{import('reset.css + layout.css + forms.css', '@ui.js + app.js')}
<!-- <link rel="stylesheet" href="/wqxjrnefjv-min.css" /> -->
<!-- <script src="/wbxyamtxmw-min.js?ts=mwi48bh6"></script> -->
```

### @{json(value, [id], [beautify], [replacer])}

Type: `command`

Prints a value as JSON, or in a `<script type="application/json">` tag.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value. |
| `id` | `String` | no |  | The id of the script tag. |
| `beautify` | `Boolean` | no | `false` | Indent the JSON. |
| `replacer` | `Function|Boolean` | no |  | A replacer for `JSON.stringify()`. |

With `id` the JSON is wrapped in a script tag with that `id`, which client code reads without a second request. `beautify` indents the JSON with four spaces. `replacer` is a function for `JSON.stringify()`, or `true` for the framework replacer. `@{json(value, true)}` is the same as `@{json(value, null, true)}`.

Example: Data for the client
```html
@{json(model.products, 'products')}
<script>
	var products = JSON.parse(document.getElementById('products').innerHTML);
</script>
```

### @{href([key], [value])}

Type: `command`

Prints the current query string with one parameter changed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String|Object` | no |  | The name of the parameter, or an object with the parameters to set. |
| `value` | `Any` | no |  | The new value. |

Returns: `String` The query string with `?`, or an empty string.

`@{href}` prints the current query string (`?page=2&sort=name`). `@{href('page', 3)}` prints it with `page` set to `3`; without a value the current value is kept. More arguments after the value are names of parameters to remove. An array value writes the parameter once for each item (`tag=a&tag=b`) and an object value is written as JSON. `@{href({ page: 3, sort: 'price' })}` returns the URL of the request with the query string changed by the keys of the object. Use it for paging and sorting links.

Example: Paging
```html
<a href="@{href('page', model.page - 1)}">Previous</a>
<a href="@{href('page', model.page + 1)}">Next</a>
```

Example: Remove a parameter, or change several
```html
<!-- /products/?page=2&sort=name&q=pen -->
<a href="@{href('page', 1, 'sort')}">First page</a>  <!-- ?page=1&q=pen -->
<a href="@{href({ page: 1, sort: 'price' })}">By price</a>  <!-- /products/?page=1&sort=price&q=pen -->
```

### DEF.helpers

Type: `property`

Custom functions you call from every view.

Returns: `Object`

Add functions to the object; inside a helper `this` is the view (`this.model`, `this.repository`, `this.controller`). Call a helper by its name (`@{price(10)}`), with `@{helpers.price(10)}` or with `@{helper('price', 10)}`. The returned value is printed without encoding. Register the helpers before the views are compiled, for example in `/definitions/`.

Example: Register and use
```js
// /definitions/helpers.js
DEF.helpers.price = function(value, currency) {
	return value.format(2) + ' ' + (currency || 'EUR');
};

// in a view: <span>@{price(model.price, 'USD')}</span>
```

### @{helper(name, [arg1], [arg2], ...)}

Type: `command`

Calls a helper from `DEF.helpers` by its name.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the helper. |

The other arguments go to the helper. It prints an empty string when the helper does not exist.

Example: Call a helper
```html
<span>@{helper('price', model.price, 'EUR')}</span>
```

### @(text)

Type: `command`

Translates a text into the language of the view.

The text is replaced by its translation from the resource files when the view is compiled; a text without a translation stays as it is. The [Localization](../../guides/localization/) guide explains the resources.

Example: Translated text
```html
<h1>@(Welcome)</h1>
<button>@(Add to cart)</button>
```

### @{compile [name]} ... @{end}

Type: `command`

Passes a part of the view to `DEF.onViewCompile` on every render.

The HTML between the tags, with the commands already evaluated, is passed to `DEF.onViewCompile(name, html, model)`, and its result is printed. Use it to run Markdown or another processor on a part of the view.

Example: Markdown
```html
@{compile markdown}
# @{model.title}

@{model.text}
@{end}
```

### DEF.onViewCompile = function(name, html, model)

Type: `delegate`

Processes the content of `@{compile}` blocks.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the block, or an empty string. |
| `html` | `String` | yes |  | The content of the block. |
| `model` | `Object` | yes |  | The model of the view. |

Returns: `String` The HTML to print.

`name` is the word after `@{compile`, `html` the rendered content of the block and `model` the model of the view. Return the HTML to print. By default it returns `html` unchanged.

Example: Markdown blocks
```js
DEF.onViewCompile = function(name, html, model) {
	return name === 'markdown' ? html.markdown() : html;
};
```

### ON('$view', function(meta) {})

Type: `event`

Changes the source of a view before it is compiled.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `meta` | `Object` | yes |  | `{ name, body, debug }`. |

`meta.name` is the name of the view, `meta.body` its HTML source and `meta.debug` the debug mode. Change `meta.body` to change the view; the event runs every time a view is compiled.

Example: Replace a placeholder
```js
ON('$view', function(meta) {
	meta.body = meta.body.replace(/\{year\}/g, new Date().getFullYear() + '');
});
```

### @{nocompress html|js|css|all}

Type: `command`

Turns off a compression step for the view.

When a view is compiled, runs of spaces inside its text are collapsed (line breaks and indentation between tags are kept), and the CSS of `<style>` tags and the JavaScript of `<script>` tags are minified. `html` keeps the HTML as it is and also turns off the minification of the response; `js` (or `script`, `javascript`) and `css` (or `style`) keep the inline code; `all` turns off all three. HTML comments are always removed.

Example: Keep whitespace
```html
@{nocompress html}
<pre>
  formatted
    text
</pre>
```

### @{{ text }}

Type: `command`

Prints `@{{ ... }}` as it is, without running it.

Use it to keep `@{{ }}` text for client-side templates.

Example: Literal text
```html
<code>@{{ value }}</code>
```

### TEMPLATE(body, [model], [$])

Type: `method`

Renders a Tangular template and returns a promise with the HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `body` | `String` | yes |  | The template, a URL, a path or a name of a file in `/templates/`. |
| `model` | `Object` | no |  | The model, available as `value`. |
| `$` | `Options` | no |  | The `$` of an action or a controller, for the language and error handling. |

Returns: `Promise<String>` The rendered HTML.

`body` is the template text when it contains `{{`. Otherwise it is a URL (`http://` or `https://`, downloaded), `~` and an absolute path, `#` and a path of a file in `/plugins/` (`.html` is added when missing), or a name of a file in `/templates/` (`.html` is added when missing). In the template the model is `value`.

The template can contain a `<script type="text/json">` block with a default model, used when `model` is not given, and a `<script>` block that adds helpers to `Thelpers`. With `$` (the `$` of a route or an action) the template is translated into `$.language`, and errors go to `$.invalid()` instead of rejecting the promise. Templates read from files are cached in release mode.

Example: Inline template
```js
var html = await TEMPLATE('<p>Hello {{ value.name }}</p>', { name: 'Peter' });
```

Example: A file in a route
```js
exports.install = function() {
	ROUTE('GET /invoice/{id}/', invoice);
};

function invoice($) {
	TEMPLATE('invoice', { number: $.params.id }, $).then(function(html) {
		$.html(html);
	});
}
```

### {{ value }} / {{ if }} / {{ foreach }}

Type: `command`

The commands of Tangular templates used by `TEMPLATE()`.

`{{ value.name }}` prints an HTML-encoded value. `{{ if condition }}` … `{{ else if condition }}` … `{{ else }}` … `{{ fi }}` is a condition. `{{ foreach item in value.items }}` … `{{ end }}` is a loop with `{{ $index }}`, `{{ continue }}` and `{{ break }}`. A value goes through helpers with `|`: `{{ value.html | raw }}`.

Example: Invoice
```html
<h1>Invoice {{ value.number }}</h1>
{{ if value.items.length }}
<table>
	{{ foreach item in value.items }}
	<tr><td>{{ $index + 1 }}</td><td>{{ item.name }}</td><td>{{ item.price | format(2) }}</td></tr>
	{{ end }}
</table>
{{ else }}
<p>No items</p>
{{ fi }}
```

### Thelpers

Type: `property`

The helpers of Tangular templates.

Returns: `Object`

Built-in helpers: `encode` (the default for every value; `encode('json')`, `encode('url')` change the format), `raw` (no encoding), `format(...)` (numbers and dates, like `Number.format()` and `Date.format()`), `def(text)` or `empty(text)` (prints `text`, or `---`, for an empty value) and `pluralize(zero, one, few, other)`. Add your own in `Thelpers`, globally or in the `<script>` block of a template file; `this` is the object that holds the model in `value`.

Example: A template file with a helper and a default model
```html
<script type="text/json">{ "name": "Guest" }</script>
<script>
Thelpers.upper = function(value) {
	return (value || '').toUpperCase();
};
</script>
<h1>{{ value.name | upper }}</h1>
<p>{{ value.count | pluralize('# items', '# item', '# items', '# items') }}</p>
<p>{{ value.date | format('yyyy-MM-dd') }} {{ value.note | def('none') }}</p>
```
