Chapter 16: Using Counters

You can increase or decrease named numerical counters with counterUp() and counterDown(), respectively. The counter starts at 0 and is incremented by 1. Use the general-purpose service format() to format the number of digits in the output integers.

Example

In this example, the counter named i is used to number the rows of a table. The counter output is formatted to be a two-digit number.

NumberNameDescription

{m:for req | requirements}

{m:'i'.counterUp.format('00')}{m:req.name}{m:req.description}

{m:endfor}

This outputs the following table:

01requirement1Description of requirement1.
02requirement2Description of requirement2.
03requirement3Description of requirement3.

To increase or decrease numerical counters with an integer other than 1, use the counterAdd() method. To count down instead of up, use a negative integer. To initialize or reset the counter, use counterSet(). You can initialize the counter with any integer. As before, use format() to format the number of digits in the output integers.

Example

In this example, the counter starts at 30 and is incremented by -5. The counter output is formatted to be a three-digit number.

NumberNameDescription

{m:let c='i'.counterSet(30)}

{m:for req | requirements}

{m:'i'.counterAdd(-5).format('000')}{m:req.name}{m:req.description}

{m:endfor}

{m:endlet}

This outputs the following table:

025requirement1Description of requirement1.
020requirement2Description of requirement2.
015requirement3Description of requirement3.