Template:For loop/doc

Template:For loop 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.

The first unnamed parameter of this template (the "separator") is output between calls to the called template.

Usage
Note that either iterator parameters (option 1) or an explicit list of values (option 2) may be used, but not both.

The separator is the first unnamed parameter, allowing it to start and/or end with newlines and spaces. The separator is output between calls to the template named in. It is not output after the last call to that template. The separator parameter is prior to the value list. If a value in the list contains an equals sign, use (see Help:Template). If you do not use, the list must use named parameters, at least from that point. If you use named parameters, note that the first value is parameter 2 (e.g. ), because parameter 1 is the separator. If the separator contains an equals sign, should also be used. Alternately, the separator can similarly be 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.)

Other examples
using 3x gives

gives

gives:

gives:

begin-><-end gives:

begin-><-end

Legacy code
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 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 was retained in the current version.

Substitution
The current Lua-based template supports substitution. If 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: -> ,  ->

For full substitution Special:ExpandTemplates can also be used.