ForgeTotal.js 5
en

Total.js 5

Use conditions and escaping in views

Total.js 5 · total5@0.0.19 viewstemplates Executed against Total.js 5

Views are HTML with @{...} blocks. A condition is @{if expression}, optionally @{else}, and always closed with @{fi}. The expression is plain JavaScript and can use model (the data passed to $.view()), repository (values shared with the layout), query, user, config and language.

Output is escaped by default: @{model.text} turns < into <, which protects you from injection. Put a ! after @{ to send the raw value (@{!model.html}) and do it only with content you trust.

views/index.html
@{layout('')}
@{if model.admin}
	<p class="role">Administrator</p>
@{else}
	<p class="role">Guest</p>
@{fi}
@{if model.items.length > 2}
	<p class="many">many items</p>
@{fi}
<div class="escaped">@{model.text}</div>
<div class="raw">@{!model.text}</div>
controllers/default.js
exports.install = function() {
	ROUTE('GET /{role}/', page);
};

function page($) {
	$.view('index', { admin: $.params.role === 'admin', items: [1, 2, 3], text: 'Hello <b>World</b>' });
}