ForgeTotal.js 5
en

Total.js 5

Render a view with a layout

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

$.view(name, model) renders views/<name>.html and wraps it in views/layout.html. Inside a view the data is available as model; use @{...} for expressions, @{foreach ... in ...} to loop and @{body} in the layout to place the view.

The layout is rendered without the model. To give it values (a page title, for example) use repository: the object returned by $.view() exposes it.

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';
}