ForgeTotal.js 5
it

Total.js 5

Renderizzare una vista con un layout

Total.js 5 · total5@0.0.19 viewslayouttemplates Eseguita con Total.js 5

$.view(name, model) renderizza views/<name>.html e lo avvolge in views/layout.html. In una vista i dati sono disponibili come model; usa @{...} per le espressioni, @{foreach ... in ...} per i cicli e @{body} nel layout per posizionare la vista.

Il layout viene renderizzato senza il modello. Per dargli dei valori (ad esempio un titolo di pagina) usa repository: lo espone l'oggetto restituito da $.view().

views/layout.html
<!DOCTYPE html>
<html>
<head><title>@{repository.title}</title></head>
<body>
@{body}
</body>
</html>
views/index.html
<h1>@{model.name}</h1>
<ul>
@{foreach item in model.items}
	<li>@{item}</li>
@{end}
</ul>
controllers/default.js
exports.install = function() {
	ROUTE('GET /', home);
};

function home($) {
	var view = $.view('index', { name: 'Forge', items: ['routes', 'actions'] });
	view.repository.title = 'Home';
}