{increment}
Adds one to a variable, starting it at 1 when it does not exist yet.
Syntax
{increment $name}
Parameters
-
$name -
The variable to count with, named rather than read, written with its
$. An application variable, not an engine one (pad*).
Description
{increment $counter} adds one to the variable and prints nothing. A variable that does not exist yet, or holds an empty text (an empty request value, a {set $n = ''}), starts at 1. The variable is a global, like one made by {set}: its new value outlives the loop or block the tag stands in, which is what makes it the step of a {while} loop.
It is the short form of {set $counter = $counter + 1}, without the error the longer form gives when the variable does not exist yet. {decrement} is its mirror image. The tag takes the variable by name - $counter, the $ written - so it never evaluates it.
A value that is no number cannot be counted: {increment} on a word or a list is an error naming the variable, and so is counting an engine variable such as $padPage.
Examples
Counting matches
A counter that did not exist starts at 1 and keeps its value after the loop.
{sequence '1..20', name='number'}
{if $number % 3 eq 0}{increment $threes}{/if}
{/sequence}
<p>Between 1 and 20, {$threes} numbers are divisible by 3.</p>
Between 1 and 20, 6 numbers are divisible by 3.
Numbering across loops
One counter numbers the rows of two separate lists in a single run.
{set $line = 100}
{data 'incrementFruit'}["apple", "pear"]{/data}
{data 'incrementVeg'}["leek", "kale", "beet"]{/data}
<p>{incrementFruit}{increment $line}{$line}: {$incrementFruit}<br>{/incrementFruit}</p>
<p>{incrementVeg}{increment $line}{$line}: {$incrementVeg}<br>{/incrementVeg}</p>
101: apple
102: pear
103: leek
104: kale
105: beet
See also
- {decrement} Takes one off 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.
- {while} Renders its content again and again for as long as a condition holds.
- {until} Renders its content again and again until a condition becomes true.