tal:repeat
This attribute handles iterable objects like arrays, associative arrays, and objects implementing the PHP5 Iterator
class.
The repeat
attribute repeats its element and its content until the end of the specified resource.
<tr tal:repeat="item some/result">
<td tal:content="item">text replaced by item</td>
</tr>
Within a loop, you can access the current loop information (and that of its parent for nested loops) using specific repeat/
paths.*
In the above example:
repeat/item/key
: returns the item's key if some/result is an associative resource (index otherwise)
repeat/item/index
: returns the item index (0 to count-1)
repeat/item/number
: returns the item number (1 to count)
repeat/item/even
: returns true if item index is even
repeat/item/odd
: returns true if item index is odd
repeat/item/start
: returns true if item is the first one
repeat/item/end
: returns true if item is the last one
repeat/item/length
: returns the number of elements in some/result
"item
" depends on the receiver variable defined in tal:repeat
expression.
The most common usage of tal:repeat
is in using some SQL database result. The following code will work if playersRanking
contains object that implements PHP's Iterator
interface:
<table>
<thead>
<tr>
<th>Position</th>
<th>Player</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<tr tal:repeat="ranking playersRanking">
<td tal:content="ranking/position"/>
<td tal:content="ranking/player"/>
<td tal:content="ranking/score"/>
</tr>
</tbody>
</table>