{while}
Renders its content again and again for as long as a condition holds.
Syntax
{while condition} ... {/while}
{while} ... {/while condition}
Parameters
-
condition - An expression, evaluated again before every pass; the loop ends when it is false. Written on the closing tag instead, it is tested after each pass, so the content renders at least once.
Description
{while condition} tests its condition and, while it holds, renders its content and tests again. Something inside the content has to change what the condition reads - an {increment}, a {set} - or the loop never ends. The variables it counts with are ordinary globals, so their last value is still there after the loop.
Written on the closing tag - {while} ... {/while $i lt 10} - the condition is tested after each pass instead of before it: a do-while, whose content renders once even when the condition is false from the start.
{until} is the same loop with the test turned round: it runs while the condition is false. {break} leaves the loop at once, {cease} lets the current pass finish and asks for no further one, and {continue} skips the rest of the current pass.
A loop whose condition never turns is stopped after 10000 passes (the sequences' ceiling, $padSeqDefaultTries) instead of running until PHP's time limit; under the strict syntax check that is an error naming the loop. A {while} without a condition or without its closing tag is an error as well. To repeat over data or a fixed range, a data tag or {sequence '1..10'} is simpler.
Examples
Counting up
The content repeats while the condition holds; {increment} moves it on.
{set $step = 1}
<p>{while $step le 5}{$step} {increment $step}{/while}</p>
<p>After the loop $step is {$step}.</p>
1 2 3 4 5
After the loop $step is 6.
The condition on the closing tag
Tested after each pass: the content renders once even though the condition is false from the start.
{set $tries = 7}
<p>Before: {while $tries lt 3}[{$tries}]{increment $tries}{/while} - nothing.</p>
<p>After: {while}[{$tries}]{increment $tries}{/while $tries lt 3} - one pass.</p>
Before: - nothing.
After: [7] - one pass.
See also
- {until} Renders its content again and again until a condition becomes true.
- {increment} Adds one to a variable, starting it at 1 when it does not exist yet.
- {break} Leaves a loop at once: the rest of the current pass is dropped and no further row follows.
- {continue} Skips the rest of the current pass of a loop and goes on with its next row.
- {cease} Ends a loop gracefully: the current pass finishes, and no further row follows.