Sjabloon:For loop/doc

Uit informatiestandaarden
Ga naar: navigatie, zoeken

This template produces a for loop. Or more accurately, it is a quasi-for-loop, as the value for each iteration must be explicitly specified. This is a legacy template, and exists for reasons of backwards compatibility. If you need to use a for loop, it is almost always better to implement it as a Lua module.

The template calls a specified template a set number of times, where one parameter takes each of a list of values; in addition to the variable parameter, fixed parameters can be specified for each iteration.


{{for loop|<!-- separator, whitespace is preserved -->|call = template to call
  | pc1n = name of the first fixed parameter 
  | pc1v = value of the first fixed parameter
  | pc2n = name of the second fixed parameter 
  | pc2v = value of the second fixed parameter
  | pc3n = name of the third fixed parameter 
  | pc3v = value of the third fixed parameter
<!-- More fixed parameters can be inserted as needed -->
  | pv = name of the parameter that the variable values will be passed to
  | prefix = fixed prefix for each of the variable values
  | postfix= fixed postfix for each of the variable values
  | list of variable values with separator "|", whitespace is stripped
     (empty parameter values work in the regular way, they are ''not'' ignored like undefined ones)

The separator is unnamed, allowing it to start and/or end with newlines and spaces. The separator should be put before the variable list. If a variable in the list contains an equals sign, the list should use named parameters, at least from that point. If you use named parameters, note that e.g. the third item in the list is parameter 4, because parameter 1 is the separator. If the separator contains an equals sign it is similarly prefixed with "1=", but in that case it cannot contain newlines and spaces at the start and end. Also, if you use named parameters, you must not skip any numbers. The loop will terminate after the first absent numbered parameter. (Note that parameters can be blank but not absent; blank parameters are read as normal.)


Code Result
1{{for loop|, 1|00|01|02|03|04|05|06|07|08|09

Note that the last part of the separator is used as a prefix; the first prefix needs to be put separately.

{{for loop|-|a|3||c|g|call=3x}} using Template:3x (backlinks, edit) gives


{{for loop|

|a|3||c|g|call=3x}} gives


{|class="wikitable sortable"
| {{for loop|
{{!}} |a|b|c|d|e|call=3x}}


{{for loop| |01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19



Legacy code

This template is currently in its third incarnation. It is now based on the Lua code at Module:ForLoop. It was ported to Lua from ParserFunctions, and the old code is available for use at {{for loop/old}}. There was also a previous version with a named "sep" parameter to specify the separator value. The template was originally based on m:Template:For. The template name was changed because there was already a Template:For on Wikipedia.

The old versions were limited to 150 variable values and four fixed parameters. There are no such limits in the current version. Also, in the first version the "sep" parameter didn't allow whitespace in the separator value. This was fixed with the second version, and this behaviour was retained in the current version.


The current Lua-based template supports substitution of the whole template call. To substitute everything, use {{subst:for loop}}.

The previous ParserFunctions-based template supported varying levels of substitution. If you wish to have fine-grained control over substitution, you need to use {{for loop/old}}. For successive levels of substitution use one or more or the following (the first, the first two, the first three, the first four, or all five).

  • "subst:" in front of "for loop/old" - substitute the call of {{for loop/old}}
  • substf=subst: - substitute the parser functions in {{for loop/old}}
  • substfa=subst: - substitute the call of {{ForLoop/aux}}, a helper subtemplate
  • substa=subst: - substitute the parser functions in {{ForLoop/aux}} (reduces the number of items from 150 to the actual number)
  • substc=subst: - substitute the calls of the user template

If at least the first four are applied, and the separator contains {{!}}, one can replace it with {{subst:!}} to substitute that too.

For full substitution Special:ExpandTemplates can also be used.

Converting legacy template invocations

If you need to convert a template invocation that was written for the original version of {{for loop}}, do the following:

  • replace pc1=X=Y with pc1n=X | pc1v=Y, etc.
  • use an unnamed parameter for the separator; if empty, just enter an extra "|"

No conversion is necessary for invocations written for the second version of the template. The Lua-based template is fully backwards-compatible with the second ParserFunctions-based template.

See also