Views
HTML views with @{} commands: rendering, layouts, conditions, loops, sections, meta tags, imports, helpers and templates.
On this page
A view is an HTML file in the /views/ folder with commands between @{ and }. The framework compiles each view into a JavaScript function and runs it with your model. In debug mode a view is compiled again on every render, so you see changes at once; in release mode the compiled function is cached by name and language.
<!-- /views/index.html -->
@{title('Products')}
<h1>@{model.name}</h1>
<ul>
@{foreach item in model.items}
<li>@{item.name} - @{item.price}</li>
@{end}
</ul>Rendering a view
In a route, call $.view(name, [model]). The name is the path of the file in /views/ without .html; a name that starts with # reads the file from the /plugins/ folder. The view is rendered into /views/layout.html, so create that file first or start the view with @{layout('')} (see Layouts).
exports.install = function() {
ROUTE('GET /', index);
ROUTE('GET /contact/'); // renders /views/contact.html
ROUTE('GET /products/', 'products'); // renders /views/products.html
};
function index($) {
$.view('index', { name: 'Shop', items: [{ name: 'Pen', price: 5 }] });
}A route without an action renders the view named by the first part of its URL (index for /), and a route whose action is a string renders the view of that name. The Routing guide has the details.
$.view() without arguments returns a new view object that you render yourself, and F.view(name, [model], [prepare]) renders a view outside of a request and returns the HTML string. F.view() is what you use for e-mails, files or caches.
var html = F.view('mail/order', { id: 'A100' }, function(view) {
view.language = 'sk';
});Layouts
Every view is rendered into a layout. The default layout is /views/layout.html; the layout prints the HTML of the view with @{body}. If the file does not exist, the request fails with an ENOENT error and the client receives a 503 response.
<!-- /views/layout.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
@{import('meta', 'default.css', 'default.js')}
</head>
<body>
@{body}
</body>
</html>Change the layout in the view with @{layout('name')} or in the route with $.layout('name'). An empty name (@{layout('')}) renders the view without a layout. The view runs first and the layout after it, so the layout can read the title, the sections and the repository values that the view set. The layout does not receive the model: pass the values it needs through repository.
Output and escaping
@{expression} prints the value of any JavaScript expression, HTML-encoded. @{!expression} prints it without encoding. null and undefined print as an empty string. A route without an action renders its view without a model: model is then undefined, so @{model.name} throws an error and the client receives a 503 response. Pass a model with $.view(name, model), even an empty object.
<p>@{model.name}</p> <!-- <b>Peter</b> -->
<div>@{!model.html}</div> <!-- the raw HTML -->
<span>@{model.items.length}</span>
@{repository.count = 5} <!-- an assignment prints nothing -->These variables are available in every view:
| Variable | What it contains |
|---|---|
model | The model passed to the view |
repository | An object shared by the view, its partial views and the layout |
config | The configuration (CONF) |
controller | The controller of the request (undefined in F.view()) |
query, body, files | The query string, the parsed body and the uploaded files of the request |
user, session | The user and the session of the request |
language | The language of the view: of the request, or view.language set for F.view() (an empty string by default) |
url | The path of the request, without the query string (undefined in F.view()) |
helpers | The custom helpers (DEF.helpers) |
Global objects such as MAIN, REPO, FUNC, CONF and MODS work as well. @{'%name'} prints a configuration value that is read once, when the view is compiled.
@{hostname()} prints the absolute URL of the current page and @{hostname('/path/')} the absolute URL of another path, for example for canonical links and share images:
<link rel="canonical" href="@{hostname()}" />
<a href="/docs/products/" class="@{url === '/products/' ? 'active' : ''}">Products</a>Conditions and loops
@{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 model.items}
@{if item.hidden}@{continue}@{fi}
<li>@{index + 1}. @{item.name}</li>
@{end}@{endif} is the same as @{fi}. Inside @{foreach}, @{index} is the zero-based position, and @{continue} and @{break} work as in JavaScript. A loop over an object (not an array) gives items with key and value.
Partial views and sections
@{view('name', [model])} renders another view in place, with its own model and without a layout. A section collects HTML in the view and prints it elsewhere, usually in the layout:
<!-- in the view -->
@{section scripts}
<script src="/docs/js/chart.js"></script>
@{end}
<!-- in the layout -->
@{section('scripts')}Title, meta tags and imports
@{title('Text')}, @{description('Text')} and @{keywords('a, b')} store the values in repository; @{title2('Text')} appends text to the title, and @{meta(title, description, keywords, image)} sets them all at once. The values are stored as they are and HTML-encoded once, when they are printed. The layout prints them with @{import('meta')} (or @{meta}): a <title> tag, the description and keywords meta tags and the og:image tags. The title gets - and CONF.name appended, except on the home page and when CONF.$customtitles is true.
@{import()} creates the tags for your static files. A .css name is looked up in /css/ and other names in /js/; a path that starts with / is used as it is. Several names joined with + are merged into one minified file, which the framework serves itself. An @ before a name appends ?ts= with the start time of the application, so browsers load the new file after each restart.
@{import('meta', 'favicon.ico', 'default.css', '@app.js')}
@{import('reset.css + layout.css + forms.css')}
@{import('/vendor/editor.js')}Helpers
A helper is a function you can call from every view. Register it in DEF.helpers; inside it this is the view, so this.model and this.repository are available. Call it by its name, with @{helpers.name()}, or with @{helper('name', ...)}. A helper returns the HTML it prints, without encoding.
// /definitions/helpers.js
DEF.helpers.price = function(value) {
return value.format(2) + ' EUR';
};<span>@{price(model.price)}</span>Localization
Text in @(...) is translated into the language of the view when the view is compiled, so every language has its own compiled copy. The language comes from the request; set view.language for F.view(). The Localization guide explains the resource files.
<h1>@(Welcome to our shop)</h1>Compression
When a view is compiled, HTML comments are removed, the CSS of <style> tags and the JavaScript of <script> tags are minified, and runs of spaces inside the text are collapsed; line breaks and indentation between tags are kept. @{nocompress html} (or js, css, all) in the view turns off one of these steps. Commands work inside <style> too, also several in one block: <style>.a{color:@{model.color}}.b{background:@{model.background}}</style>.
Templates
TEMPLATE(body, [model], [$]) renders a template with the Tangular engine and returns a promise with the HTML. Templates are not views: they use {{ }} commands and the model is value. The body is the template itself, a URL, a path of a file in /templates/ (.html is added when missing), a path that starts with # for a file in /plugins/ (.html is added when missing), or ~ and an absolute path.
var html = await TEMPLATE('invoice', { number: 'F001', items: items });<!-- /templates/invoice.html -->
<h1>Invoice {{ value.number }}</h1>
{{ foreach item in value.items }}
<p>{{ item.name }}: {{ item.price | format(2) }}</p>
{{ end }}