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.

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.

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:

{{#for:3|{{lc:Ab$n$}} }} gives ab1ab2ab3; the result is in this case the same as when the repetition was done first, and then the evaluation of each item.

{{#for:3|{{uc:Ab$n$}} }} 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. {{#expr:2*$n$}} gives the text "Expression error: Unrecognised punctuation character "$"", so this text will be repeated.

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 {{{$n$}}}.

Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

To users running MediaWiki 1.30 or earlier:

The instructions above describe the new way of installing this extension using wfLoadExtension(). If you need to install this extension on these earlier versions (MediaWiki 1.30 and earlier), instead of wfLoadExtension('LoopFunctions');, you need to use: