Template:For loop

Da Wikipedia
Jump to navigation Jump to search
Template documentation[view] [edit] [history] [purge]

This template implements a for loop or a foreach loop.

This template calls a user-specified template (the "called template") multiple times: once for each value in either 1) an iterated sequence or 2) an explicit list. Each value in the sequence or list is passed to the same specified parameter of the called template (the "variable parameter"). Optionally, pre- and postfixes can be attached to the passed values.

In addition to the specified variable parameter, other parameters of the called template ("static parameters") can be given a value which is the same in each iteration.

Usage[muda l codesc]

Group Parameter Meaning Default value
Mandatory 1 (unnamed) separator to output between calls (may be blank; whitespace is preserved)
call template to call
pv name (or number) of the variable parameter of the called template 1
Option 1:
iterated sequence
start first numeric value to pass to variable parameter 1
stop maximum numeric value for variable parameter
by iteration step size between values passed to variable parameter 1
Option 2:
explicit values
(unnamed parameters) explicit values, given as separate parameters (whitespace is stripped)
skipBlanks set to "true" to skip empty parameter values false
Other optional
pc[N]n name (or number) of the Nth static parameter of the called template
pc[N]v value for the Nth static parameter of the called template
prefix static prefix prepended to each value passed to the variable parameter
postfix static postfix appended to each value passed to the variable parameter
substall set to "false" to not substitute the called template when {{for loop}} is substituted true

Either option 1 (iteration parameters) or option 2 (an explicit list of values) may be used, but not both.

The first unnamed parameter, prior to any explicit values, is a separator. The separator is a string that is output between calls to the template named in |call=. It is not output after the last call.

Caution[muda l codesc]

  • The separator can be prefixed with "1=", but in that case it cannot contain newlines and spaces at the start and end.
  • If any parameter value contains an equals sign, use {{=}} (see Template Usage hints and workarounds).
  • If you use numbered parameters, note that the first value is parameter 2 (e.g. |2=Your1stValue), because parameter 1 is the separator.
  • If you use numbered parameters, you must not skip any numbers. The loop will terminate after the first absent numbered parameter. (Parameters can be blank, but not absent.)

Substitution[muda l codesc]

The current Lua-based template supports substitution. If |substall=no is not specified, then substituting the template will substitute everything, including the call to the template passed in |call=. If it is specified, then the template substitutes into a sequence of calls to the template specified.

Example: Template:Tlxs -> 01sep 02sep 03sep 04sep 05sep 06sep 07, Template:Tlxs -> {{1x|01}}sep {{1x|02}}sep {{1x|03}}sep {{1x|04}}sep {{1x|05}}sep {{1x|06}}sep {{1x|07}}

For full substitution Special:ExpandTemplates can also be used.

Examples[muda l codesc]

Form Code Explanation Result
{{for loop|&|call = spanbox
  |pv = font size
  |postfix = px
  |pc1n = 1
  |pc1v = A
  |pc2n = background
  |pc2v = yellow
Call the template "spanbox" with values:
  • font size = "[N]px", where N takes values starting with 10 and increasing by 8 while remaining less than or equal to 52
  • 1 (first unnamed parameter) = "A"
  • background = "yellow"

Separating outputs with "&"

Fal Lua: expandTemplate: template "spanbox" does not exist.
Explicit values
{{for loop|, |call=1x
Call the template "1x" with values "1[NN]", where NN = "00" through "99" (given explicitly), separating the outputs with the string ", " Fal Lua: expandTemplate: template "1x" does not exist.

Other examples with explicit values[muda l codesc]

{{for loop|-|a|3||c|g|call=3x}} using Template:Tiw gives

Fal Lua: expandTemplate: template "3x" does not exist.

{{for loop|

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

Fal Lua: expandTemplate: template "3x" does not exist.

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


Fal Lua: expandTemplate: template "3x" does not exist.
{{for loop| |01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19


Fal Lua: expandTemplate: template "1x" does not exist.

begin->{{for loop|{{=}} |01|02|03|04|05|06|07|08|09|10


begin->Fal Lua: expandTemplate: template "1x" does not exist.<-end

Notes about legacy code[muda l codesc]

This template is currently in its third incarnation. It now uses the Lua code at Module:For loop. It was ported to Lua from ParserFunctions. There was also a previous version with a named "sep" parameter to specify the separator value. The template was originally based on Template:For on meta. 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 was retained in the current version.

See also[muda l codesc]