Repeating sections of markup
The TAL repeat attribute repeats the element upon which it appears once for each item in a collection. A parallel in programming/scripting languages would be a 'for each' loop.
Syntax
tal:repeat="NAME EXPRESSION"
The variable name
Permitted variable names (NAME
) are one or more non-whitespace characters. They are case
sensitive and mandatory. This creates a new repeat variable within
the TALES context, which acts much like a local variable, with some additional functionality.
Note that the TALES csharp:
expressions plugin is installed by
default.
If this plugin is installed then tal:repeat
variable names
must also be valid C♯ variable names.
The expression
The expression (EXPRESSION
) is
evaluated as a TALES expression. It is then handled according
to the value of the result, using the following logic:
-
If the result of the expression cancels the action, or is
null
then the element is left unchanged and no repeat variable is defined. -
If the result of the expression does not implement
System.Collections.IEnumerable
then an error is raised. - The expression result is enumerated and the element (and all of its children) is duplicated once for each item in the resultant collection. The order in which the repeated elements appear in the DOM respects the order in which the collection is enumerated.
Note that if the collection exposed by the expression is empty, then the element and all of its children will be repeated zero times. This means that they would be removed from the DOM; further TAL processing upon the element and its children will be aborted.
Example
Here is an example of the tal:repeat
syntax in operation:
<table>
<tbody>
<tr tal:repeat="item CurrentUser/ShoppingCart">
<td tal:content="repeat/item/index">1</td>
<td tal:content="item/Name">Item name</td>
</tr>
</tbody>
</table>
The first column uses the TALES repeat variable in order to display the numeric index of the repetition, the second column makes use of the local variable to render information from the current iteration's object model.