Extension:Loops

While, do-while and other kinds of loops similar to those used in programming languages.

Usage
For technical reasons, the preprocessor in v1.12alpha and later handles the first parameter differently than the rest, so it's ignored for the #while and #dowhile parser functions in order for them to work. See bug 12842.

#while
{ {#while}} performs a loop (i.e. it repeatedly parses a given wiki markup block statement) so long as the condition mark-up evaluates to non-whitespace.

Examples
Note: The following examples use the VariablesExtension.

The wiki markup:

produces the following:


 * 0
 * 1
 * 2
 * 3
 * 4

{ {#while}} can also be used in a template to simulate a numbered array. If the page "Template:Loops Test" contains

then the wiki-markup

produces


 * zero
 * one
 * two
 * three
 * four

It's important to note that whitespace, including newlines, tabs, and spaces, is stripped from the beginning and end of all the arguments of these parser functions. If this is not desirable, adding any non-whitespace characters (including the HTML encoding for a whitespace character &amp;#32;) will prevent further stripping (hence the &lt;nowiki/&gt; tags in the above examples).

#dowhile
{ {#dowhile}} performs exactly like { {#while}}, with the exception that the block statement is guaranteed to be parsed and displayed (if it results in displayable text) at least once. This is done before the condition text is evaluated.

#loop
{ {#loop}} repeatedly parses and displays &lt;wiki markup> a number of times equal to the absolute value of &lt;number of loops to be performed>. &lt;Starting value> is placed in a variable (accessible by VariablesExtension's { {#var:}} parser function) using the name &lt;variable name>. After each loop, the variable is incremented by one if &lt;number of loops to be performed> is positive, or decremented by one if &lt;number of loops to be performed> is negative.

Note:  From all loop functions,  should have the best performance since there is no condition which has to be expanded and validated for each cycle.

Examples
The following code:

produces


 * This is round 4 and we have 3 more to go
 * This is round 5 and we have 2 more to go
 * This is round 6 and we have 1 more to go
 * This is round 7 and we have 0 more to go

#forargs (Experimental)
{ {#forargs}} is to be used in templates. It takes arguments that are passed to the template and puts them in variables accessible by VariablesExtension's { {#var:}} parser function.

This function iterates through each argument whose name begins with &lt;prefix>. With each iteration it puts the argument name minus &lt;prefix> into &lt;key> as if calling { {#vardefine: &lt;key> }}. It then takes the value of the argument and puts it into &lt;value> in a similar method. The block statement is then expanded. The block statement may contain { {#var: &lt;key> }} and { {#var: &lt;value> }} to access the stored arguments.

Example
If the page "Template:Loops Test" contains

then the wiki markup

produces


 * 1 = val1
 * 5 = val5
 * ument = value

#fornumargs (Experimental)
{ {#fornumargs}} performs similarly to { {#forargs}} with two major differences: It doesn't take a prefix argument, and it only works on numbered arguments whether they're explicitly numbered:

or implicitly numbered:

Mixing these methods in a single template call may cause values to get overwritten, so be careful.

Examples
TBD

Installation
The parser functions #loop, #forargs, and #fornumargs require VariablesExtension to be installed. Loops is installed by adding the following to LocalSettings.php:

Configuration
These configuration variables have to be set in the LocalSettings file after inclusion of Loops.


 * ExtLoops::$maxLoops: This parameter sets the maximum number of loops a page is allowed to perform (default 100). Setting it to  lets the loops run within the limits of phps environment.  This parameter affects neither the { {#forargs:}} nor { {#fornumargs:}}.


 * $egLoopsEnabledFunctions: Configuration variable (array) to define which Loops functions should be enabled. By default, all functions are enabled if the Variables extension is installed as well. If the Variables extension is not installed,,   and   will be disabled since they don't work without. To enable the fornumargs and forargs functions only, one can use: