Extension:LoopFunctions/cs

Rozšíření LoopFunctions poskytuje funkce, které umožňují pracovat s wikitextem v opakovaných cyklech (smyčkách), kdy postupně nahrazují proměnnou pořadovým číslem aktuálního cyklu. Tahle proměnná se může vyskytovat jak v běžném textu, tak v odkazu. Výsledek zpracování lze použít jako parametry jiného výrazu, například pro výpočet faktoriálu. Protože se kód smyčky generuje dřív, než dojde k nahrazení proměnné, není možné udělat výpočet již během zpracování v rámci jednoho cyklu.

#for
Kde n je počet cyklů a text řetězec, se kterým se bude pracovat.

If, after expansion of templates, parser functions, and variables, the text contains the text  or what is defined by the third parameter, that will be replaced by the current iteration, starting with 1.

Using in templates
Calling the template, if template foo containing: , will result in the wikitext:

Using for calculations
Sum of squares 1^2 through 5^2: gives 55

Factorial 6 (6!): gives 720

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


 * Parameter of a parser function depending on the index variable :

Code gives  ; the result is in this case the same as when the repetition was done first, and then the evaluation of each item.

Code gives  ; the result is in this case not the same, because   is changed to , 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 so this text will be repeated:


 * Template name depending on the index variable :

Code gives:
 * if the template with the literal name  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  ).

if the template with the literal name  does exist: the result of, i.e., the content is repeated unchanged.


 * Limitation in combination with : 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
Function only for template use.

The mask is a string in the format prefixsuffix where  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 prefixsuffix is a template parameter defined in the template call.

If the text contains the text  or what is defined by the third parameter, that will be replaced by the current value of.

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.

Example
Calling the template using the template foo, containing:  will result in the expanded wikitext   and hence the output:
 * foo1_bar = b
 * foo2_bar = a

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

Using without prefix or suffix
The code call Template:Showpars containing  and gives:
 * 1 = abc
 * 2 = de
 * 3 = ijk

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