User:Polaris999/for loop/doc

Template:For loop and User:Polaris999/for loop implement a foreach loop. 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 use Template:For loop, which is implemented as a Lua module.

This template calls a user specified template once for each of a list of values. A named parameter is passed with the value from the list of the current iteration with a possible prefix and/or a postfix attached. In addition to the variable parameter, fixed parameters can be specified to be passed with each iteration. The first unnamed parameter is output between calls to the user specified template.

Usage
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 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.)

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

using 3x gives

gives

gives:

gives:

This demonstrates the limitation to 150 items. Use the new version of the template.

begin-><-end gives:

begin-><-end

Legacy code
This template is currently in its third incarnation. It is now uses the Lua code at Module:For loop. It was ported to Lua from ParserFunctions, and the old code is available at User:Polaris999/for loop. 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 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 User:Polaris999/for loop. 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 "User:Polaris999/for loop" - substitute the call of User:Polaris999/for loop
 * substf=subst: - substitute the parser functions in User:Polaris999/for loop
 * 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   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: No conversion is necessary for invocations written for the second version of the template, unless they use fine-grained substitution as mentioned in the section above. If you need fine-grained substitution you should use User:Polaris999/for loop instead of for loop. — Mr. Stradivarius  ♪ talk ♪ 09:44, 19 July 2013 (UTC)
 * replace  with , etc.
 * use an unnamed parameter for the separator; if empty, just enter an extra "|"