Skip to content

Iterators

Craig edited this page Nov 13, 2017 · 7 revisions

A loop is a statement, or set of statements, that are repeated for a specified number of times or until some condition is met.

For Each Loops

For each loops are supported with any property or variable that is of type IEnumerable. The syntax for using a for each loop is:

{{each $.RoleAssignments}}
	Role: {{$.RoleName}}
{{/each}}

Which would generate something like:

Role: Admin
Role: Customer

Nested loops are also supported:

{{each $.Users}}
	User: {{$.UserName}}

	{{each $.RoleAssignments}}
		Role: {{$.RoleName}}
	{{/each}}
{{/each}}

The body of an each loop can contain any Nettle code or content. Every time an each statement is rendered, the body is given a new model based on the item in the loop.

All unconflicting properties or variables from the parent model are inherited by the new model (unless the DisableModelInheritance flag is set, see template flags documentation for more information).

While Loops

The while loop loops through a block of code as long as a specified condition is true.

Syntax

{{while condition}}
	code block to be executed
{{/while}}

Example

In the following example, the code in the loop will run, over and over again, as long as a variable (counter) is less than 10:

{{var counter = 1}}

{{while counter < 10}}
	Counter: {{counter}}
	{{counter++}}
{{/while}}

The example above will generate the following output:

Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Clone this wiki locally