Extension:LoopFunctions

The LoopFunctions extension will enable limited looping functionality in the wikitext, where a variable ($n$ by default) is replaced by the current iteration number of the loop. The variable can be used in a plain text or link. Also the result of the whole loop can be put in an expression, e.g. to compute a factorial. However, the loop body is evaluated before replacing the variable, so e.g. a computation inside the loop body, depending on the value of the variable, is not possible.

Per standard the number of iterations is limited to 100 iterations per session.

#for
where n is the count of iteration and the text is the text to be repeated. If, after expansion of templates, parser functions, and variables, the text contains the text $n$ or what is defined by the third parameter, that will be replaced by the current iteration, starting with 1.

Examples
calling the template using the template foo, containing: will result in the text a b c d


 * Sum of squares 1^2 through 5^2: gives 55.
 * Factorial 6 (6!): gives 720.

Compare, using and :  gives 55.

For a list of squares it seems that we can only use "for", not "#for".

Limitation
In the loop body, templates, parser functions, and variables are expanded before replacing the index variable by its values.

Examples:

Parameter of a parser function depending on the index variable:
 * gives ab1ab2ab3; the result is in this case the same as when the repetition was done first, and then the evaluation of each item.
 * gives AB$N$AB$N$AB$N$; the result is in this case not the same, because $n$ is changed to $N$, and therefore not treated as index variable.
 * Applying #expr to an expression containing the index variable does not work: expansion of #expr in e.g. gives the text "Expression error: Unrecognised punctuation character "$"", so this text will be repeated.

Template name depending on the index variable:
 * gives:
 * if the template with the literal name Template:a$n$ does not exist: Template:a1 Template:a2 Template:a3 (the loop body is evaluated as Template:a$n$, after which the loop function repeats that, substituting $n$).
 * if the template with the literal name Template:a$n$ does exist: the result of, i.e., the content is repeated unchanged.

Limitation in combination with Extension:Variables
Within the loop a variable can be assigned a value depending on the loop variable, and the value of this variable can be retrieved.

However, it seems that within the loop the variable cannot be assigned a new value depending on its own current value. Instead the value on entering the loop is taken.

#foreach
The mask is a string in the format prefix$n$suffix where $n$ can be changed via the replacement parameter. The function #foreach, called inside a template, will produce the text for $n$ = 1, 2, 3,... as long as prefix$n$suffix is a template parameter defined in the template call. If the text contains the text $n$ or what is defined by the third parameter, that will be replaced by the current value of $n$.

It seems that the whole call of #foreach is ignored if the loop body contains a template call or a parser function call, or if the loop body does not contain.

Examples
Calling the template using the template foo, containing: will result in the expanded wikitext foo1_bar = b foo2_bar = a

and hence the output

foo1_bar = b foo2_bar = a

Since foo3_bar is not defined, foo4_bar is not listed either; neither are a and 1, the parameter names which exist but do not match the pattern.

Example without prefix or suffix:

The call of Template:Showpars containing  gives:

1 = abc 2 = de 3 = ijk

Content of a template that links to the pages given by the values of the unnamed parameters:

Installation
This version is for MediaWiki 1.12+

You can download a tarball here, or copy the code below to the right files.

''Note : This work has not been checked by the author of LoopFunctions. Please tell me if you notice any regression. ''

Please note also that the text is now left untrimmed in the case of, in order to allow printing a list : now displays : If you want to keep the old behaviour, check the code of the function  (LoopFunctions.php, line 65) : just a line to uncoment.
 * foo 1
 * foo 2
 * foo 3

''And finaly, please excuse my poor English ! :-)''

--Xiloynaha 14:37, 13 August 2008 (UTC)