5.5. Working with Lists of Objects

Placeholders are very helpful when designing templates but they only represent single values. They can't represent a list of objects. To generate a complete invoice, you will have to deal with lists of objects like items or taxes in your template.

For instance, if you have a list of items (services or expenses), you will probably want to create a table where each row represents an item from the list of items. If you have a list of taxes, you will probably want to display the total for each one of them at the end of the invoice.

Below is a template that puts the items of an invoice in a table.

<table>
	<tr>
		<th>Name</th>
		<th>Quantity</th>
		<th>Price</th>
		<th>Amount</th>
	</tr>
	
	<#list invoice.items as item>
	<tr>
		<td>${item.name}</td>
		<td>${item.quantity}</td>
		<td>${item.price}</td>
		<td>${item.total}</td>
	</tr>
	</#list>
</table>

The template can be read as follows: Create a table with four columns, one for each property of an item that we want to display. Then (the code shown in bold), create a table row that contains its name, quantity, price and total for each item from the list of items.

Note: If you want to access a property of an item, you must use a placeholder. A list with all the properties of an item can be found here.