---
title: Views
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/views/
updated: 2026-11-16T15:41:00.817Z
---

# Views

> Every command of the view engine and the controller methods that render views.

Views are `.html` files in `views/` with commands in `@{...}` and translated text in `@(...)`. The guide [Views](../../guides/views/) explains how they work together; this page lists each command and the controller methods for views.

## API

### @{expression}

Type: `command`

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

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `expression` | `Any` | yes |  | A JavaScript expression. |

Returns: `String` The encoded value.

Any expression works: a property of the model, a method call, arithmetic. `null` and `undefined` print an empty string. The characters `<`, `>`, `&`, `"` and `'` are encoded. To print the value without encoding, use `@{!expression}`.

Text that starts with `@{{` is not a command and stays in the page as written.

Example: Values of the model
```html
<h1>@{model.name}</h1>
<p>@{model.price.format(2)} EUR</p>
<p>@{model.items.length} items</p>
```

### @{!expression}

Type: `command`

Prints the value of an expression without HTML encoding.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `expression` | `Any` | yes |  | A JavaScript expression. |

Returns: `String` The raw value.

Use it for HTML that you trust, for example content you have already sanitized. Never use it for text typed by users.

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

### @{model.property}

Type: `command`

The model passed to the view by `controller.view(name, model)`.

`M` is a shorter name for the same object. The layout gets the same model as the view. A command that assigns a value, like `@{model.count = 0}`, prints nothing.

Example: Controller and view
```js
function detail(id) {
	this.view('detail', { id: id, name: 'Apple' });
}
```

Example: In the view
```html
<h1>@{model.name}</h1>
<small>@{M.id}</small>
```

### @{repository.property}

Type: `command`

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

It is `controller.repository` in JavaScript. `R` is a shorter name. Values set in the view are visible in the layout, because the view is rendered first.

Example: Set in the view, read in the layout
```html
@{repository.page = 'products'}

<!-- layout.html -->
<body class="@{repository.page}">
```

### @{user} @{session} @{query} @{body.field} @{config.key} @{language} @{url} @{controller}

Type: `command`

Values of the current request that every view can use.

| Variable | Value |
| --- | --- |
| `user` | The signed-in user (`controller.user`) |
| `session` | The session (`controller.session`) |
| `query` | The query string as an object |
| `body` | The request body; `@{body}` alone is the rendered view, in a layout |
| `config`, `CONF` | The configuration |
| `language` | The language of the request |
| `url` | The path of the request |
| `files` | Uploaded files |
| `mobile` | `true` for a mobile device |
| `controller` | The controller |

They are encoded like any expression; put `!` in front for the raw value.

Example: Request values
```html
@{if user}
	<p>Hello @{user.name}</p>
@{fi}
<input type="search" name="q" value="@{query.q}" />
```

### @{'%key'}

Type: `command`

Prints a value of the configuration, HTML-encoded.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | A key of the configuration. |

The value is read when the view is compiled. `@{config.key}` and `@{CONF.key}` print the same value. A quoted path without `%`, like `@{'/css/default.css'}`, prints the URL of that static file.

Example: App name
```html
<footer>@{'%name'}</footer>
<footer>@{CONF.name}</footer>
```

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

Type: `command`

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

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `condition` | `Any` | yes |  | A JavaScript expression. |

The condition is JavaScript. `@{else if}` and `@{else}` are optional. Close the block with `@{fi}` or `@{endif}`.

Example: Condition
```html
@{if model.items.length}
	<p>@{model.items.length} products</p>
@{else if model.loading}
	<p>Loading…</p>
@{else}
	<p>No products.</p>
@{fi}
```

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

Type: `command`

Renders a part of the view for each item of an array or object.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `item` | `String` | yes |  | The name of the variable for the current item. |
| `source` | `Array|Object` | yes |  | The expression to loop over. |

`index` is the position of the current item, from `0`. An empty source prints nothing. When the source is an object, it becomes an array of `{ key, value }`. `@{foreach var item in source}` also works. `@{continue}` and `@{break}` skip an item or stop the loop.

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

Example: Object
```html
@{foreach item in model.prices}
	<li>@{item.key}: @{item.value}</li>
@{end}
```

Example: Skip items
```html
@{foreach item in model.items}
	@{if item.hidden}
		@{continue}
	@{fi}
	<li>@{item.name}</li>
@{end}
```

### @{body}

Type: `command`

Prints the rendered view inside the layout.

Use it in the layout. With a property, `@{body.name}` reads the request body instead.

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

### @{layout(name)}

Type: `command`

Changes the layout of the current view.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the layout, or `''` for no layout. |

Returns: `String` An empty string.

The default layout is `layout` (`default_layout` in the config). An empty string renders the view without a layout. Layouts are always looked up from the root of `views/`.

Example: Another layout
```html
@{layout('admin')}

<h1>Dashboard</h1>
```

Example: No layout
```html
@{layout('')}
```

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

Type: `command`

Renders a partial view in place, without a layout.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the view. |
| `model` | `Object` | no |  | The model of the partial view. |
| `expire` | `String` | no |  | How long to cache the output, e.g. `'5 minutes'`. |
| `key` | `String` | no |  | A key that makes the cache entry unique, e.g. the language. |

Returns: `String` The rendered HTML.

The name is resolved like in `controller.view()`. The partial does not get the model of the parent automatically; pass it. With `expire` the output is cached for that time under the name and the `key`.

Example: Partial per item
```html
@{foreach item in model.items}
	@{view('partials/product', item)}
@{end}
```

Example: Cached menu
```html
@{view('menu', model.menu, '10 minutes', language)}
```

### @{view_compile(body, [model], [key])}

Type: `command`

Compiles an HTML string as a view and renders it in place.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `body` | `String` | yes |  | The template, with the same syntax as a view. |
| `model` | `Object` | no |  | The model. |
| `key` | `String` | no |  | A cache key for the compiled template. |

Returns: `String` The rendered HTML.

Useful for templates stored in a database. The result has no layout. With `key`, the compiled template is kept in memory in release mode.

Example: Template from the model
```html
@{view_compile(model.template, model.data)}
```

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

Type: `command`

Writes HTML in the view and prints it somewhere else, usually in the layout.

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

`@{section name}` … `@{end}` stores its content and prints nothing; blocks with the same name are joined. `@{section('name')}` prints the stored content, or nothing. The content is kept in the repository, so print it after the view is rendered: in the layout.

Example: Scripts of one page
```html
@{section scripts}
	<script src="/js/chart.js"></script>
@{end}

<h1>Statistics</h1>
```

Example: In the layout
```html
@{body}
	@{section('scripts')}
</body>
```

### @{helper name(arguments)} ... @{end}

Type: `command`

Defines a helper inside the view.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the helper. |
| `arguments` | `String` | yes |  | Names of the arguments, as in a JavaScript function. |

The helper exists only in this view. Call it by its name; its output is not encoded again. For helpers shared by all views use `DEF.helpers`.

Example: Inline helper
```html
@{helper badge(text, color)}
	<span class="badge badge-@{color}">@{text}</span>
@{end}

@{badge('New', 'green')}
@{badge('Sale', 'red')}
```

### DEF.helpers.name = function(...args) {}

Type: `property`

Defines view helpers available in all views.

Add the functions before the views are compiled, for example in `definitions/`. In a view, call the helper by its name: `@{name(arguments)}`; `@{helpers.name(arguments)}` and `@{helper('name', arguments)}` work too. Inside the function `this` is the controller. The returned value is printed without encoding.

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

DEF.helpers.active = function(path) {
	return this.url === path ? ' class="active"' : '';
};
```

Example: Use
```html
<span>@{price(item.price, 'USD')}</span>
<a href="/products/"@{active('/products/')}>Products</a>
```

### @(Text)

Type: `command`

Marks text for translation; it is replaced when the view is compiled.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `Text` | `String` | yes |  | The text in the default language, or `#key`. |

The text is translated with the language of the controller (`controller.language`, which comes from `DEF.onLocale`). The translation is read from the resource file of the language under a key made from the hash of the text; when there is none, the original text stays. `@(#key)` reads `key` from the resource directly. Inside quotes, quotes in the translation are escaped. Each language gets its own compiled view.

Example: Translated text
```html
<h1>@(Products)</h1>
<button title="@(Add to cart)">@(Add)</button>
<p>@(#footer_note)</p>
```

### @{resource([name], key)}

Type: `command`

Prints a value from a resource file, HTML-encoded.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no | `default` | The resource file without extension. |
| `key` | `String` | yes |  | The key. |

Returns: `String` The value, or an empty string.

Without `name` the value comes from `default.resource`. When a key is missing in another file, the value from `default.resource` is used. `@{!resource(...)}` prints it raw.

Example: Resource values
```html
<p>@{resource('welcome')}</p>
<p>@{resource('en', 'welcome')}</p>
```

### @{import(file1, [file2], ...)}

Type: `command`

Writes the HTML tags for static files and for the head of the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `file` | `String` | yes |  | A file name or one of the keywords. As many as you need. |

Returns: `String` HTML tags.

Each argument is one of:

| Argument | Output |
| --- | --- |
| `'meta'` | Title and meta tags, see `@{meta}` |
| `'head'` | The author meta tag and the tags added with `@{head()}` |
| `'file.css'` | `<link rel="stylesheet">` from `/css/` |
| `'file.js'` | `<script>` from `/js/` |
| `'favicon.ico'`, `'favicon.png'` | The icon link |
| an image or video | Its URL |
| `'a.css + b.css'` | One merged file with all of them |
| `'manifest'` | `<link rel="manifest">` |
| `'livereload'` | The live reload script, in debug mode only |

A name that starts with `/` or a full URL is used as it is. `!` in front of a file prints only its URL. The output is cached per file.

Example: Head of a layout
```html
<head>
	<meta charset="utf-8" />
	@{import('meta', 'head', 'default.css', 'default.js', 'favicon.ico')}
</head>
```

Example: Merged files
```html
@{import('ui.css + default.css', 'ui.js + default.js')}
```

### @{css(file1, [file2], ...)}  /  @{js(file1, [file2], ...)}

Type: `command`

Write `<link rel="stylesheet">` tags for CSS files, or `<script>` tags for JavaScript files.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `file` | `String` | yes |  | A file. As many as you need. |

Returns: `String` HTML tags.

Relative names are taken from `/css/` (`static_url_style`) and `/js/` (`static_url_script`). In `@{js()}`, start a name with `async ` to add the `async` attribute.

Example: Styles and scripts
```html
@{css('default.css', 'print.css')}
@{js('default.js', 'async stats.js')}
```

### @{public_js(name)} @{public_css(name)} @{public_image(name)} @{public_video(name)} @{public_font(name)} @{public_download(name)} @{public(name)}

Type: `command`

Prints the URL of a static file in its folder, without a tag.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The file name. |

Returns: `String` The URL.

Each command uses its folder from the config: `/js/`, `/css/`, `/img/`, `/videos/`, `/fonts/`, `/download/`, and `public` the root. A name that starts with `/` is used from the root. The URL respects the theme and `default_root`.

Example: URLs
```html
<img src="@{public_image('logo.png')}" alt="Logo" />
<script src="@{public_js('app.js')}"></script>
```

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

Type: `command`

Prints the title and meta tags of the page, or sets them.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `title` | `String` | no |  | The title. |
| `description` | `String` | no |  | The description. |
| `keywords` | `String|Array` | no |  | Keywords. |
| `image` | `String` | no |  | An image for social networks, an absolute URL or a file in `/img/`. |

Returns: `String` HTML tags, or an empty string when setting.

Without arguments it prints `<title>`, the description and keywords meta tags, and `og:image` and `twitter:image` when an image is set. With arguments it sets the values and prints nothing. The title gets ` - ` and `CONF.name` at the end unless `allow_custom_titles` is `true`. To change the output, replace `DEF.onMeta`.

Example: Set in the view, print in the layout
```html
@{meta(model.name, model.summary, model.tags, model.picture)}

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

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

Type: `command`

Prints or sets the title of the page.

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

Returns: `String` The title, or an empty string when setting.

`@{title(value)}` sets the title and prints nothing; `@{title2(value)}` appends to the current title. `@{title}` prints the title alone. The value is HTML-encoded when it is set, so `@{!title}` prints it without encoding it twice. To print the whole `<title>` tag, use `@{meta}`.

Example: Title
```html
@{title(model.name)}

<h1>@{!title}</h1>
```

### @{description(value)} @{keywords(value)} @{author(value)}

Type: `command`

Sets the description, keywords or author of the page.

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

Returns: `String` The value, or an empty string when setting.

With an argument they set the value and print nothing. `@{description}`, `@{keywords}` and `@{author}` print the value. `@{meta}` prints the description and keywords; `@{import('head')}` prints the author (or `CONF.author`). Keywords can be an array.

Example: Meta values
```html
@{description('Fresh apples from our farm')}
@{keywords(['apples', 'fruit'])}
@{author('Peter')}
```

### @{head}  /  @{head(value1, [value2], ...)}

Type: `command`

Adds CSS, JavaScript or HTML to the head of the page, or prints it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | no |  | A file name, a URL or HTML. |

Returns: `String` HTML, or an empty string when adding.

With arguments it adds tags and prints nothing: `.css` files become `<link>`, `.js` files `<script>`, and a value that starts with `<` is added as HTML. Each value is added once. `@{head}` (or `@{import('head')}`) prints the author meta tag and everything added.

Example: Add from a view
```html
@{head('chart.css', 'chart.js', '<meta name="robots" content="noindex" />')}
```

Example: Print in the layout
```html
<head>
	@{import('meta', 'head')}
</head>
```

### @{place(name)}  /  @{place(name, value1, [value2], ...)}

Type: `command`

Adds content to a named place, or prints it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the place. |
| `value` | `String` | no |  | A file path or HTML. |

Returns: `String` The content, or an empty string when adding.

With values it appends them and prints nothing: `.js` and `.mjs` files become `<script>`, `.css` files `<link>`, anything else is added as it is. With the name only it prints the content. Values are not resolved to the static folders, so write full paths.

Example: Place for scripts
```html
@{place('scripts', '/js/chart.js')}

<!-- layout.html -->
@{place('scripts')}
```

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

Type: `command`

Serializes a value to JSON.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Any` | yes |  | The value to serialize. |
| `id` | `String` | no |  | The id of a `<script>` element. |
| `beautify` | `Boolean` | no | `false` | Indents the JSON. |

Returns: `String` JSON, or a `<script>` element.

With `id` the JSON is wrapped in `<script type="application/json" id="...">`, so client-side code can read it. The output is not HTML-encoded.

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

### @{options(items, [selected], [name], [value], [disabled])}

Type: `command`

Writes `<option>` elements for a `<select>`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `items` | `Array|Object` | yes |  | The options. |
| `selected` | `Any` | no |  | The value of the selected option. |
| `name` | `String` | no | `name` | The property with the text. |
| `value` | `String` | no | `value` | The property with the value. |
| `disabled` | `String` | no | `disabled` | The property that disables an option. |

Returns: `String` HTML.

For an array of objects, `name` is the property of the text (default `name`) and `value` the property of the value (default: the same as `name` when you give a name, otherwise `value`). The property `disabled` disables an option. For an array of strings or numbers, each item is both text and value. For an object, keys are values and values are texts. The option equal to `selected` gets `selected="selected"`. Texts and values are encoded.

Example: Select
```html
<select name="country">
	@{options(model.countries, model.country, 'name', 'id')}
</select>
```

Example: Strings
```html
<select name="size">
	@{options(['S', 'M', 'L'], 'M')}
</select>
```

### @{checked(condition)} @{selected(condition)} @{disabled(condition)} @{readonly(condition)}

Type: `command`

Prints an HTML attribute when a condition is true.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `condition` | `Any` | yes |  | When truthy, the attribute is printed. |

Returns: `String` The attribute or an empty string.

They print ` checked="checked"`, ` selected="selected"`, ` disabled="disabled"` or ` readonly="readonly"`, with a space in front, or nothing.

Example: Attributes
```html
<input type="checkbox" name="newsletter"@{checked(model.newsletter)} />
<button@{disabled(!model.items.length)}>Order</button>
```

### @{text(name, [attributes])} @{password(name, [attributes])} @{hidden(name, [attributes])}

Type: `command`

Writes an `<input>` for a field of the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the field. |
| `attributes` | `Object` | no |  | Extra attributes, e.g. `{ placeholder: 'Name', required: true }`. |

Returns: `String` An `<input>` element.

The model of the view is added automatically: the value of the input is `model[name]`, encoded. The attributes are added to the element; `required`, `disabled`, `readonly` and `autofocus` are written as boolean attributes, and `autocomplete: true` writes `autocomplete="on"` (any other truthy value writes `off`). `value` in the attributes is used when the model has no such field.

Example: Form fields
```html
<form method="POST">
	@{text('name', { placeholder: 'Your name', required: true })}
	@{password('password')}
	@{hidden('id')}
</form>
```

### @{input(type, name, [attributes])}

Type: `command` · Since: `0.0.53`

Writes an `<input>` of any type for a field of the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The type of the input. |
| `name` | `String` | yes |  | The name of the field. |
| `attributes` | `Object` | no |  | Extra attributes. |

Returns: `String` An `<input>` element.

Works like `@{text()}` with the type you choose, for example `email`, `number` or `date`.

Example: Email
```html
@{input('email', 'email', { placeholder: 'you@example.com' })}
```

### @{checkbox(name, [label|attributes])} @{radio(name, value, [label|attributes])}

Type: `command`

Writes a checkbox or a radio button for a field of the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the field. |
| `value` | `String` | yes |  | Radio only: the value of this button. |
| `label` | `String|Object` | no |  | A label, or extra attributes (`label` is one of them). |

Returns: `String` An `<input>`, wrapped in `<label>` when there is a label.

A checkbox is checked when `model[name]` is `true`, `1`, `'true'` or `'on'`; its value is `1`. A radio button is checked when `model[name]` equals `value`. A string as the last argument is a label: the input is wrapped in `<label>` with the text in a `<span>`. `@{radio()}` needs this last argument, a label or an object.

Example: Checkbox and radio
```html
@{checkbox('newsletter', 'Send me news')}
@{radio('size', 'S', 'Small')}
@{radio('size', 'L', 'Large')}
```

### @{textarea(name, [attributes])}

Type: `command`

Writes a `<textarea>` with the value of a field of the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the field. |
| `attributes` | `Object` | no |  | Extra attributes, e.g. `{ rows: 5 }`. |

Returns: `String` A `<textarea>` element.

The content is `model[name]`, or `value` from the attributes. The content is not encoded, so encode values typed by users first.

Example: Textarea
```html
@{textarea('note', { rows: 5, placeholder: 'Note' })}
```

### @{image(name, [width], [height], [alt], [class])}

Type: `command`

Writes an `<img>` for a file in `/img/`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The image file. |
| `width` | `Number|Object` | no |  | The width, or an object with all attributes. |
| `height` | `Number` | no |  | The height. |
| `alt` | `String` | no |  | The alternative text. |
| `class` | `String` | no |  | CSS classes. |

Returns: `String` An `<img>` element.

Instead of the arguments you can pass an object: `{ width, height, alt, class, style }`. `alt` is encoded.

Example: Image
```html
@{image('logo.png', 120, 40, 'Logo')}
@{image('photo.jpg', { alt: model.name, class: 'photo' })}
```

### @{csrf}

Type: `command` · Since: `0.0.37`

Prints a CSRF token for the current request.

Returns: `String` The token.

A route with the `csrf` flag checks the token in the `X-CSRF-Token` header or in the `csrf` query parameter. It needs `secret_csrf` in the config. The token is made by `DEF.onCSRFcreate(req)` and checked by `DEF.onCSRFcheck(req)`.

Example: Form and AJAX
```html
<form method="POST" action="/contact/?csrf=@{csrf}">
	...
</form>

<meta name="csrf-token" content="@{csrf}" />
```

### @{href()}  /  @{href(key, value)}

Type: `command`

Prints the current query string, optionally with one key changed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | no |  | The parameter to set. |
| `value` | `Any` | no |  | Its new value. |

Returns: `String` A query string.

The result starts with `?`, or is empty. Use it for paging and filters that keep the other parameters.

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

### @{url}  /  @{url(true)}

Type: `command`

Prints the path of the current request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `host` | `Boolean` | no | `false` | Adds the protocol and the host. |

Returns: `String` The URL.

`@{url(true)}` prints the full address with the protocol and the host.

Example: Canonical link
```html
<link rel="canonical" href="@{url(true)}" />
```

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

Type: `command`

Passes a rendered block of the view to `DEF.onCompileView()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | A name that tells your function what to do. |

The function gets the name, the HTML of the block and the model, and returns the HTML to print. By default it returns the HTML unchanged; replace it to run another template language or a Markdown parser. The block is not minified.

Example: A hook
```js
DEF.onCompileView = function(name, html, model) {
	return name === 'markdown' ? html.markdown() : html;
};
```

Example: In a view
```html
@{compile markdown}
# Title
Some **text**.
@{end}
```

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

Type: `command`

Turns off the minification of a view.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | `html`, `css`, `js` or `all`. |

By default Total.js minifies the HTML of a view and its inline CSS and JavaScript. Put this command anywhere in the view to keep one part as written: `html`, `css` (or `style`), `js` (or `script`, `javascript`) or `all`. HTML comments are removed in any case, except conditional comments.

Example: Keep the HTML as written
```html
@{nocompress html}
<pre>
	Indented   text
</pre>
```

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

Type: `method`

Renders a view with its layout and sends it as the response.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no | `from the URL` | The name of the view without `.html`. |
| `model` | `Object` | no |  | The model of the view. |
| `headers` | `Object` | no |  | Extra HTTP headers. |
| `partial` | `Boolean` | no | `false` | `true` returns the HTML instead of sending it. |

Returns: `Controller|String` The controller, or the HTML when `partial` is `true`.

Without `name` the view is named after the last part of the URL (`index` for `/`). The name is looked up in `views/<controller>/` first, then in `views/`; prefixes `~`, `=theme/`, `@`, `#plugin/` and `.` change that (see the guide). With `partial` set to `true`, the method returns the HTML instead of sending it; the layout is still applied unless you set `controller.layout('')`. When the view does not exist, the response is an error 500.

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

function products() {
	this.view('products', { items: [] });
}
```

Example: Get the HTML
```js
function preview() {
	this.layout('');
	var html = this.view('card', model, true);
	this.json({ html: html });
}
```

### controller.view_compile(body, [model], [headers], [partial], [key])

Type: `method`

Compiles an HTML string as a view, renders it and sends it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `body` | `String` | yes |  | The template. |
| `model` | `Object` | no |  | The model. |
| `headers` | `Object` | no |  | Extra HTTP headers. |
| `partial` | `Boolean` | no | `false` | `true` returns the HTML instead of sending it. |
| `key` | `String` | no |  | A cache key for the compiled template. |

Returns: `Controller|String` The controller, or the HTML when `partial` is `true`.

The string uses the same syntax as a view file. With `key` the compiled template is kept in memory in release mode.

Example: Template from a database
```js
function page() {
	this.layout('');
	this.view_compile('<h1>@{model.title}</h1>', { title: 'Hello' });
}
```

### controller.layout(name)

Type: `method`

Sets the layout for the next view.

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

Returns: `Controller` The controller.

The default is `default_layout` from the config (`layout`). An empty string renders without a layout. The view can change it again with `@{layout()}`.

Example: Admin layout
```js
function dashboard() {
	this.layout('admin');
	this.view('dashboard');
}
```

### controller.meta(title, [description], [keywords], [image])

Type: `method`

Sets the title and meta tags that `@{meta}` prints.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `title` | `String` | yes |  | The title. |
| `description` | `String` | no |  | The description. |
| `keywords` | `String|Array` | no |  | Keywords. |
| `image` | `String` | no |  | An image for social networks. |

Returns: `Controller` The controller.

`controller.title()`, `controller.description()`, `controller.keywords()` and `controller.author()` set one value each.

Example: Meta tags
```js
function detail() {
	this.meta('Apple', 'Fresh apples', ['apple', 'fruit'], 'apple.jpg');
	this.view('detail');
}
```

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

Type: `method`

Adds content to a section of the view, or reads it.

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

Returns: `Controller|String` The controller, or the content when reading.

Without `value` it returns the content. Otherwise it appends the value, or replaces the content when `replace` is `true`. Print the section with `@{section('name')}`.

Example: Section from a controller
```js
function stats() {
	this.section('scripts', '<script src="/js/chart.js"></script>');
	this.view('stats');
}
```

### controller.repository

Type: `property`

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

In the view it is `repository` or `R`.

Example: Pass a value to the layout
```js
function products() {
	this.repository.page = 'products';
	this.view('products');
}
```

### VIEW(name, [model], [layout], [repository], [language])

Type: `method`

Renders a view outside a request and returns the HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the view. |
| `model` | `Object` | no |  | The model. |
| `layout` | `String` | no | `''` | A layout. |
| `repository` | `Object` | no |  | The repository. |
| `language` | `String` | no |  | The language for `@(...)`. |

Returns: `String` The HTML.

There is no layout unless you name one. Use it for emails, files or responses built by hand. A name like `=theme/view` reads the view from a theme.

Example: Render to a string
```js
var html = VIEW('emails/order', order, null, null, 'en');
```

### VIEWCOMPILE(body, [model], [layout], [repository], [language])

Type: `method`

Compiles an HTML string as a view, outside a request, and returns the HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `body` | `String` | yes |  | The template. |
| `model` | `Object` | no |  | The model. |
| `layout` | `String` | no | `''` | A layout. |
| `repository` | `Object` | no |  | The repository. |
| `language` | `String` | no |  | The language for `@(...)`. |

Returns: `String` The HTML.

It is `VIEW()` for a template in a string, for example from a database. The string uses the same syntax as a view file. There is no layout unless you name one, and `layout` and `repository` can switch places. The template is compiled on every call.

Example: Template from a string
```js
var html = VIEWCOMPILE('<h1>@{model.title}</h1>', { title: 'Hello' });
```
