# Loop over arrays and objects in views

> Repeat markup with @{foreach} for arrays and objects, and use the index of the current item.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: adab60f6b0585cd9ac35414a22c15ca854687debd2d504904f69cd77d07e4aff
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`@{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`.

```html 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>
```

```js 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' }
	});
}
```
