ForgeTotal.js 5
en

Total.js 5

Loop over arrays and objects in views

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

@{foreach item in expression} repeats the block for every element of an array, closing with @{end}. Inside the loop index is the position of the current element (starting at 0). The same syntax iterates the properties of an object: every element has a key and a value.

views/index.html
@{layout('')}
<ul class="products">
@{foreach product in model.products}
	<li>@{index}: @{product.name} - @{product.price}</li>
@{end}
</ul>
<dl class="fields">
@{foreach field in model.meta}
	<dt>@{field.key}</dt><dd>@{field.value}</dd>
@{end}
</dl>
controllers/default.js
exports.install = function() {
	ROUTE('GET /', page);
};

function page($) {
	$.view('index', {
		products: [{ name: 'Book', price: 12 }, { name: 'Pen', price: 3 }, { name: 'Lamp', price: 40 }],
		meta: { color: 'red', size: 'M' }
	});
}