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

# Views

> HTML templates in views/: expressions, conditions, loops, layouts, partials, sections, helpers and localization.

A view is an HTML file in `views/` with commands in `@{...}`. Total.js compiles it into a JavaScript function, runs it with a model, wraps the result in a layout and sends it as the response.

## Where views live

Views are `.html` files in `views/`. You name them without the extension.

```text
views/
	layout.html
	index.html
	contact.html
	users/
		index.html
		detail.html
```

A controller looks first in the folder with its own name. In `controllers/users.js`, `this.view('detail')` renders `views/users/detail.html`. When that file does not exist, Total.js tries `views/detail.html`. The controller `default.js` uses `views/` directly.

A prefix changes where the view is looked up:

| Name | File |
| --- | --- |
| `index` | `views/<controller>/index.html`, then `views/index.html` |
| `~index` | `views/index.html`, always from the root of `views/` |
| `=admin/index` | `themes/admin/views/index.html` |
| `@index` | the view of a package |
| `#plugin/index` | the view of a plugin |
| `.path/to/file` | an absolute path, without `.html` |

## Render a view

The simplest route has the name of a view instead of an action:

```js
exports.install = function() {
	ROUTE('GET /', 'index');
	ROUTE('GET /contact/', 'contact');
};
```

From an action, call `this.view()` with the name and a model:

```js
exports.install = function() {
	ROUTE('GET /products/', products);
};

function products() {
	var model = { name: 'Shop', items: [{ name: 'Apple', price: 1.2 }, { name: 'Pear', price: 0.9 }] };
	this.view('products', model);
}
```

Without a name, `this.view(model)` renders the view named after the last part of the URL (`index` for `/`).

A schema operation can render its result as a view. The result becomes the model:

```js
ROUTE('GET /products/ *Products --> list', 'products');
```

## The model and the repository

The model is the second argument of `this.view()`. In the view it is `model` (or `M`):

```html
<h1>@{model.name}</h1>
<p>@{model.items.length} products</p>
```

The **repository** is an object shared by the view, its partial views and the layout during one request. Use it to pass values that are not part of the model, like a page class. It is `controller.repository` in JavaScript and `repository` (or `R`) in the view:

```js
function products() {
	this.repository.page = 'products';
	this.view('products', model);
}
```

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

A view can also assign values. The command prints nothing:

```html
@{repository.page = 'products'}
```

Other variables you can use in a view: `user`, `session`, `query`, `body` (request body, see the layout section for `@{body}` alone), `config`, `language`, `url`, `controller`.

## Expressions and escaping

`@{expression}` prints the value of any JavaScript expression, **HTML-encoded**. Put `!` in front to print it **raw**:

```html
<p>@{model.name}</p>
<p>@{model.price.format(2)}</p>
<div>@{!model.html}</div>
```

With `model.name = '<b>Peter</b>'`, `@{model.name}` prints `&lt;b&gt;Peter&lt;/b&gt;`, and `@{!model.name}` prints the bold tag. `null` and `undefined` print nothing.

Use the raw form only for HTML you trust. The output of helpers, `@{json()}` and the form commands is not encoded again.

Text that starts with `@{{` is not a command; it is left in the page as it is.

## Conditions

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

The condition is JavaScript. Close it with `@{fi}` or `@{endif}`.

## Loops

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

`index` is the position of the item, from `0`. An empty array prints nothing. When you loop over an object, each item has `key` and `value`:

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

`@{continue}` and `@{break}` work inside the loop, usually in an `@{if}`.

## Layouts

Every view is rendered inside a layout. The default layout is `views/layout.html` (`default_layout` in the config). `@{body}` prints the rendered view:

```html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	@{import('meta', 'head', 'default.css', 'default.js')}
</head>
<body>
	@{body}
</body>
</html>
```

If the layout file does not exist, the request ends with an error 500. Create it, or turn the layout off.

Change the layout in the controller or in the view:

```js
function admin() {
	this.layout('admin');     // views/admin.html
	this.view('dashboard');
}

function plain() {
	this.layout('');          // no layout
	this.view('plain');
}
```

```html
@{layout('admin')}
```

The layout gets the same model and repository as the view.

## Partial views

`@{view(name, [model])}` renders another view in place, without a layout. Pass the model if the partial needs it:

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

The name is resolved like any view, so `partials/product` is `views/partials/product.html` (or inside the folder of the controller). Add an expiration and a key to cache the output: `@{view('menu', model, '5 minutes', 'menu')}`.

## Sections

A section is HTML written in the view and printed in the layout, for example scripts of one page:

```html
@{section scripts}
	<script src="/js/chart.js"></script>
@{end}

<h1>Statistics</h1>
```

```html
	@{body}
	@{section('scripts')}
</body>
```

Several blocks with the same name are joined. From a controller use `this.section('scripts', '<script src="/js/chart.js"></script>')`.

## Helpers

Total.js has view helpers for common HTML: `@{import()}`, `@{options()}`, `@{checked()}`, `@{text()}`, `@{json()}` and more. They are listed in the [Views reference](../../reference/views/).

Define your own helpers in `DEF.helpers`, usually in a file in `definitions/`. In a helper `this` is the controller:

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

Call it by its name:

```html
<span>@{price(item.price, 'USD')}</span>
```

The output of a helper is not encoded; encode the values yourself when they come from users. `@{helper('price', item.price)}` does the same call.

A helper used by only one view can be written inside it:

```html
@{helper badge(text)}
	<span class="badge">@{text}</span>
@{end}

@{badge('New')}
```

## Localization

Text in `@(...)` is translated when the view is compiled, with the language of the request (`controller.language`, set by `DEF.onLocale`):

```html
<h1>@(Products)</h1>
<button>@(Add to cart)</button>
```

The translation comes from the resource file of the language. When there is none, the text stays as it is. `@(#key)` reads the key `key` from the resource directly. `@{resource('key')}` prints a key of `default.resource`, and `@{resource('en', 'key')}` a key of `en.resource`.

## CSS, JavaScript and images

`@{import()}` writes the tags for static files. Relative names go to the static folders (`/css/`, `/js/`, `/img/`):

```html
@{import('meta', 'head', 'default.css', 'ui.css', 'default.js', 'favicon.ico')}
```

`meta` prints the title and meta tags, `head` the tags added with `@{head()}`. Join files with `+` to merge them into one: `@{import('default.css + ui.css')}`.

## Title and meta tags

Set the title and the description in the controller or in the view, and print them with `@{meta}` (or `@{import('meta')}`) in the layout:

```js
function detail() {
	this.title('Apple');
	this.description('Fresh apples from our farm');
	this.view('detail', model);
}
```

```html
@{title(model.name)}
@{description(model.summary)}
```

The title gets ` - ` and the name of the app (`CONF.name`) at the end, unless `allow_custom_titles` is `true` in the config.

## Compilation and caching

The first time a view is used, Total.js reads it, removes HTML comments, minifies the HTML with its inline CSS and JavaScript, translates `@(...)` and compiles it. In **release** mode the compiled view is kept in memory, once per language. In **debug** mode it is compiled on every request, so your changes appear without a restart.

Put `@{nocompress html}` at the top of a view to keep its HTML as written (`css`, `js` and `all` also work).

## Render a view to a string

`VIEW(name, [model], [layout], [repository], [language])` renders a view anywhere and returns HTML, without a layout unless you name one:

```js
var html = VIEW('emails/order', order);
```
