{decrement}
Takes one off a variable, starting it at -1 when it does not exist yet.
Syntax
{decrement $name}
Parameters
-
$name -
The variable to count down, named rather than read, written with its
$. An application variable, not an engine one (pad*).
Description
{decrement $counter} takes one off the variable and prints nothing. A variable that does not exist yet, or holds an empty text, starts at -1. The variable is a global: the new value outlives the loop or block the tag stands in, so it can count down the condition of an {until} or {while} loop.
It is the short form of {set $counter = $counter - 1} and the mirror image of {increment}. The variable is taken by name - $counter, the $ written - and is never evaluated as an expression.
A value that is no number is an error naming the variable, and so is counting an engine variable such as $padPage.
Examples
Places left
Each booking takes one place off; when none are left the next guests go on the waiting list.
{set $places = 3}
<ul>
{bookings}
{if $places eq 0}<li>{$guest}: waiting list</li>{continue}{/if}
{decrement $places}
<li>{$guest}: booked, {$places} left</li>
{/bookings}
</ul>
<?php
$bookings = [
[ 'guest' => 'Ann' ],
[ 'guest' => 'Bob' ],
[ 'guest' => 'Cleo' ],
[ 'guest' => 'Dirk' ] ];
?>
- Ann: booked, 2 left
- Bob: booked, 1 left
- Cleo: booked, 0 left
- Dirk: waiting list
A counter that did not exist
Decrementing a new variable starts it at -1.
<p>{decrement $balance}{$balance}, {decrement $balance}{$balance}, {decrement $balance}{$balance}</p>
-1, -2, -3
See also
- {increment} Adds one to a variable, starting it at 1 when it does not exist yet.
- {set} Assigns one or more variables, as globals that outlive the level the tag stands in.
- {until} Renders its content again and again until a condition becomes true.
- {while} Renders its content again and again for as long as a condition holds.