Welcome to the DFO World Wiki. With many major updates since the release of DFO, many items are missing. Visit Item Database Project to learn more.
Please remember to click "show preview" before saving the page.
Thanks for the updated logo snafuPop!

Difference between revisions of "Template:Horizontal scale loop/doc"

From DFO World Wiki
Jump to: navigation, search
m (Dfoplayer moved page Template:For loop/doc to Template:For/doc without leaving a redirect: migrate to a non lua)
Line 1: Line 1:
 +
{{documentation subpage}}
 +
Uses [[Template:For/aux]].
  
<!-- PLEASE ADD CATEGORIES AND INTERWIKIS AT THE BOTTOM OF THIS PAGE. -->
+
== Description ==
[[Template:For loop]] implements a [[for loop]] or a [[foreach loop]].
+
The template calls a specified template up to 150 times, where one parameter takes each of a list of values; in addition to the variable parameter, for up to 4 parameters a fixed value can be specified.
 
 
This template calls a user-specified template (the "called template") once for each value in either 1) an iterated sequence or 2) an explicit list. A specified parameter of the called template is passed each value in the sequence or list (with optional pre- and postfixes attached). The first unnamed parameter of this template (the "separator") is output between calls to the called template. In addition to the one variable-valued parameter, other "static" parameters of the called template can be passed, with the same value in each iteration.
 
  
 
== Usage ==
 
== Usage ==
{| class="wikitable"
+
(Note the change for the constant values.)
! Parameter !! Value !! Notes
 
|-
 
| '''1 (unnamed)''' || separator to output between calls (whitespace is preserved) || '''MANDATORY''' (may be blank)
 
|-
 
| '''call''' || template to call || '''MANDATORY'''
 
|-
 
| '''pv''' || name of the variable parameter to pass to the called template || '''MANDATORY''' unless it is the first unnamed parameter of the called template (use integers for other unnamed parameters)
 
|-
 
| '''pc[N]n''' || name (or number) of the Nth static parameter to pass to the called template ||
 
|-
 
| '''pc[N]v''' || value of the Nth static parameter to pass to the called template ||
 
|-
 
| '''prefix''' || static prefix prepended to each of the variable values ||
 
|-
 
| '''prefix''' || static postfix appended to each of the variable values ||
 
|-
 
| '''substall''' || false to not substitute the called template when {{tl|for loop}} is substituted (defaults to true) ||
 
|- style="border-top: 3px solid grey;"
 
| '''start''' || first value to pass to variable parameter (defaults to 1) || rowspan=3 | '''Option 1: iterator'''
 
|-
 
| '''stop''' || maximum value for variable parameter
 
|-
 
| '''by''' || iteration step size between values passed to variable parameter (defaults to 1)
 
|- style="border-top: 3px solid grey;"
 
| '''(unnamed)''' || | explicit list of variable values, given as separate parameters (whitespace is stripped) || rowspan=2 | '''Option 2: explicit values'''
 
|-
 
| '''skipBlanks''' || true to skip empty parameter values (defaults to false)
 
|}
 
  
Note that ''either'' iterator parameters (option 1) ''or'' an explicit list of values (option 2) may be used, but not both.
 
<!--
 
 
<pre>
 
<pre>
{{for loop|<!-- separator, whitespace is preserved --><!--|
+
{{for
    call   = template to call
+
  |call=template to call (default: 1x)
   | pc1n   = name of the 1st static parameter passed to the called template
+
  |what to separate the entries with (unnamed parameter; required, but can be the empty string)
   | pc1v   = value of the 1st static parameter passed to the called template
+
   |pc1n=name of first parameter with constant value (default: the value of parameter u)
   | pc2n   = name of the 2nd static parameter passed to the called template
+
   |pc1v=first constant value (default: empty)
   | pc2v   = value of the 2nd static parameter passed to the called template
+
   |pc2n=name of second parameter with constant value (default: the value of parameter u)
   | pc3n   = name of the 3rd static parameter passed to the called template
+
   |pc2v=second constant value (default: empty)
   | pc3v   = value of the 3rd static parameter passed to the called template
+
   |pc3n=name of third parameter with constant value (default: the value of parameter u)
<!-- More fixed parameters passed to the called template can be inserted as needed -->
+
   |pc3v=third constant value (default: empty)
<!--
+
   |pc4n=name of fourth parameter with constant value (default: the value of parameter u)
   | pv      = name of the parameter passed to the called template, to which is
+
   |pc4v=fourth constant value (default: empty)
                assigned the variable values (with optional pre- and postfixes attached).
+
   |pc5n=name of fifth parameter with constant value (default: the value of parameter u)
   | prefix  = static prefix prepended to each of the variable values
+
   |pc5v=fifth constant value (default: empty)
   | postfix = static postfix appended to each of the variable values
+
   |pv=name of variable parameter (default: 1)
   | skipBlanks = whether to include skip empty parameter values
+
   |prefix=constant first part of the value of pv (default: empty)
<!-- EITHER: -->
+
   |postfix=constant last part of the value of pv (default: empty)
<!--
+
   |list of 0 to 150 parameter values (without prefixes and postfixes), with separator "|"
   | start = first variable value to pass (defaults to 1)
+
     (empty parameter values work in the regular way, they are ''not'' ignored like undefined ones)
   | stop = maximum value for variable value
+
  |u=name of a parameter that is not used in template ''call'' (default: empty; see also below)
   | by = iteration step size between values (defaults to 1)
+
 
<!-- OR: -->
 
<!--
 
   | explicit list of variable values with separator "|" (whitespace is stripped)
 
     (unless skipBlanks is specified and true, empty parameter values work in the regular way, they are ''not'' ignored like undefined ones)
 
 
}}
 
}}
 
</pre>
 
</pre>
-->
 
  
The separator is the first unnamed parameter, allowing it to start and/or end with [[Help:newlines and spaces|newlines and spaces]]. The separator is output ''between'' calls to the template named in <code>|call=</code>. 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 {{tlx|{{=}}}} (see [[Help:Template#Usage hints and workarounds|Help:Template]]).  If you do not use {{tlx|{{=}}}}, 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. <code>|2=Your1stValue</code>), because parameter 1 is the separator. If the separator contains an equals sign, {{tlx|{{=}}}} 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.)
+
The separator is unnamed, allowing it to start and/or end with [[Help:newlines and spaces|newlines and spaces]]. This parameter should be put before the list. If a parameter value in the list contains an equals sign the list should use named parameters, at least from that point, where it should be noted 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 then represent it with <nowiki>{{=}}</nowiki>, see {{tim|1==}}.
  
 
==Examples==
 
==Examples==
 
{| class="wikitable", border=1
 
{| class="wikitable", border=1
 
!Code
 
!Code
!Explanation
 
 
!Result
 
!Result
 
|-
 
|-
 
|
 
|
<pre>{{for loop|&|call = spanbox
+
<pre>1{{for|, 1|00|01|02|03|04|05|06|07|08|09
  |pc1n = 1
 
  |pc1v = A
 
  |pc2n = background
 
  |pc2v = yellow
 
  |pv = font size
 
  |postfix = px
 
  |start=10|stop=46|by=8
 
}}</pre>
 
| Outputting "&" between calls, call the template "spanbox" with values:
 
*1 (first unnamed parameter) = "A"
 
*background = "yellow"
 
*font size = "[N]px", where N takes values starting with 10 and increasing by 8 while remaining less than 46
 
|{{for loop|&|call = spanbox
 
  |pc1n = 1
 
  |pc1v = A
 
  |pc2n = background
 
  |pc2v = yellow
 
  |pv = font size
 
  |postfix = px
 
  |start=10|stop=46|by=8
 
}}
 
|-
 
|<pre>1{for loop|, |call=1x
 
|prefix=1
 
|00|01|02|03|04|05|06|07|08|09
 
 
|10|11|12|13|14|15|16|17|18|19
 
|10|11|12|13|14|15|16|17|18|19
 
|20|21|22|23|24|25|26|27|28|29
 
|20|21|22|23|24|25|26|27|28|29
Line 111: Line 50:
 
|80|81|82|83|84|85|86|87|88|89
 
|80|81|82|83|84|85|86|87|88|89
 
|90|91|92|93|94|95|96|97|98|99
 
|90|91|92|93|94|95|96|97|98|99
 +
  |call=1x
 
}}</pre>
 
}}</pre>
| Call the template "1x" with values "1[NN]", where NN = "00" through "99" (given explicitly), separating the outputs with the string ", "
+
|1{{for|, 1|00|01|02|03|04|05|06|07|08|09
|{{for loop|, |call=1x
 
|prefix=1
 
|00|01|02|03|04|05|06|07|08|09
 
 
|10|11|12|13|14|15|16|17|18|19
 
|10|11|12|13|14|15|16|17|18|19
 
|20|21|22|23|24|25|26|27|28|29
 
|20|21|22|23|24|25|26|27|28|29
Line 125: Line 62:
 
|80|81|82|83|84|85|86|87|88|89
 
|80|81|82|83|84|85|86|87|88|89
 
|90|91|92|93|94|95|96|97|98|99
 
|90|91|92|93|94|95|96|97|98|99
 +
  |call=1x
 
}}
 
}}
 
|}
 
|}
  
===Other examples===
+
Note that the last part of the separator is used as a prefix; the first prefix needs to be put separately.
 +
 
 +
----
 +
 
 +
<nowiki>{{for|-|a|3||c|g}}</nowiki> gives
 +
 
 +
{{for|-|a|3||c|g}}
  
<nowiki>{{for loop|-|a|3||c|g|call=3x}}</nowiki> using {{tiw|3x}} gives
+
<nowiki>{{for|-|a|3||c|g|call=3x}}</nowiki> using {{tim|3x}} gives
  
{{for loop|-|a|3||c|g|call=3x}}
+
{{for|-|a|3||c|g|call=3x}}
  
<nowiki>{{for loop|</nowiki>
+
<nowiki>{{for|</nowiki>
  
 
<nowiki>|a|3||c|g|call=3x}}</nowiki> gives
 
<nowiki>|a|3||c|g|call=3x}}</nowiki> gives
  
{{for loop|
+
{{for|
  
 
|a|3||c|g|call=3x}}
 
|a|3||c|g|call=3x}}
Line 147: Line 91:
 
!Test
 
!Test
 
|-
 
|-
| {{for loop|
+
| {{for|
 
{{!}}-
 
{{!}}-
 
{{!}} |a|b|c|d|e|call=3x}}
 
{{!}} |a|b|c|d|e|call=3x}}
Line 159: Line 103:
 
!Test
 
!Test
 
|-
 
|-
| {{for loop|
+
| {{for|
 
{{!}}-
 
{{!}}-
 
{{!}} |a|b|c|d|e|call=3x}}
 
{{!}} |a|b|c|d|e|call=3x}}
 
|}
 
|}
 +
 +
Using {{tim|table row example}},
  
 
<pre>
 
<pre>
{{for loop| |01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19
+
{| class="wikitable sortable"
|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39
+
|-
|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59
+
!number!!reciprocal
|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79
+
|-
|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99
+
| {{for|
|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119
+
{{!}}-
|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139
+
{{!}} |call=table row example|3|1|5|3|2}}
|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|call=1x}}
+
|}
 
</pre>
 
</pre>
gives:
 
  
{{for loop| |01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19
+
gives
|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39
+
 
|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59
+
{| class="wikitable sortable"
|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79
+
|-
|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99
+
!number!!reciprocal
|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119
+
|-
|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139
+
| {{for|
|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|call=1x}}
+
{{!}}-
 +
{{!}} |call=table row example|3|1|5|3|2}}
 +
|}
 +
 
 +
----
 +
 
 +
<nowiki>{{for| / |call=short DOW|3|1|5|3|2}}</nowiki> using {{tim|short DOW}} gives
 +
:{{for| / |call=short DOW|3|1|5|3|2}}
 +
 
 +
----
 +
 
 +
<nowiki>{{for|; |call=t2|pc1n=2|pc1v=constant|abc|def|ghi|postfix=pof}}</nowiki> using {{tim|t2}} gives:
 +
 
 +
{{for|; |call=t2|pc1n=2|pc1v=constant|abc|def|ghi|postfix=pof}}
 +
 
 +
----
 +
 
 +
<nowiki>{{for|, |call=concat and link to w:en:|pc1n=1|pc1v=John_|pv=2||Lennon|Wayne}}
 +
</nowiki> using {{timc|concat and link to w:en:}} gives {{for|, |call=concat and link to w:en:|pc1n=1|pc1v=John_|pv=2||Lennon|Wayne}}
 +
 
 +
A for-call can give a row or column of choice, see e.g. [[Template:Power]].
 +
 
 +
A constant parameter is ignored if it is the same as the (implicitly or explicitly given) variable parameter:
 +
 
 +
<nowiki>{{for|; |call=t2|pc1n=1|pc1v=7|pc2n=2|pc2v=5|abc|def|ghi}}</nowiki> using {{tim|t2}} gives:
 +
 
 +
{{for|; |call=t2|pc1n=1|pc1v=7|pc2n=2|pc2v=5|abc|def|ghi}}
  
<pre>
+
----
begin->{{for loop|{{=}} |01|02|03|04|05|06|07|08|09|10
 
|11{{=}}{{=}}{{=}}|12|13|14|15|16|17|18|19|call=1x}}<-end
 
</pre>
 
gives:
 
  
begin->{{for loop|{{=}} |01|02|03|04|05|06|07|08|09|10
+
Using {{tim|plus square}}, <nowiki>{{#expr:{{for||call=plus square|pv=1|1|2|3|4|5}}}}</nowiki> gives {{#expr:{{for||call=plus square|pv=1|1|2|3|4|5}}}}.
|11{{=}}{{=}}{{=}}|12|13|14|15|16|17|18|19|call=1x}}<-end
 
  
== Legacy code ==
+
Semi-recursive: see [[Template talk:Foreach/recursion]]
  
This template is currently in its third incarnation. It now uses the [[WP:Lua|Lua]] code at [[Module:ForLoop]]. It was ported to Lua from [[mw:Help:Extension:ParserFunctions|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.
+
==Value "undefined"==
 +
If a value in the list of values is "undefined" this value is ignored, just like the unused parameters at the end if there are less than 150 values (see also below):
  
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.
+
<code><nowiki>{{for|-|a|3|undefined|c|g|call=3x}}</nowiki></code> gives {{for|-|a|3|undefined|c|g|call=3x}}
  
== Substitution ==
+
==Substitution==
 +
For successive levels of substitution use one or more or the following (the first, the first two, the first three, or all)
  
The current Lua-based template supports [[WP:SUBST|substitution]]. If {{para|substall|no}} is not specified, then substituting the template will substitute everything, including the call to the template passed in {{para|call}}. If it is specified, then the template substitutes into a sequence of calls to the template specified.
+
*substf=subst: - substitute the parser functions in {{tim|for}}
 +
*substfa=subst: - substitute the call of auxiliary {{tim|for/aux}}
 +
*substa=subst: - substitute the parser functions in for/aux (reduces the number of items from 150 to the actual number)
 +
*substfa=subst: - substitute the calls of the user template
  
Example: {{tlsx|for loop|sep |01|02|03|04|05|06|07|call{{=}}1x}} -> <code><nowiki>01sep 02sep 03sep 04sep 05sep 06sep 07</nowiki></code>, {{tlsx|for loop|sep |01|02|03|04|05|06|07|call={{1x}}|substall{{=}}no}} -> <code><nowiki>{{1x|01}}sep {{1x|02}}sep {{1x|03}}sep {{1x|04}}sep {{1x|05}}sep {{1x|06}}sep {{1x|07}}</nowiki></code>
+
If at least the first three are applied, and the separator contains <nowiki>{{!}}</nowiki>, one can replace it with <nowiki>{{subst:!}}</nowiki> to substitute that too.
  
 
For full substitution [[Special:ExpandTemplates]] can also be used.
 
For full substitution [[Special:ExpandTemplates]] can also be used.
  
 +
==Internal technical details==
 +
The template calls [[Template:For/aux]]. It is necessary for avoiding a newline if the separator starts with one of the characters "#&:;" (In particular the semicolon is a likely character to be at the start of the separator.) This is because of the [[Help:Newlines_and_spaces#Automatic_newline|automatic newline]] if the separator is produced by a template or parser function. Even the workaround of moving the previous item into the then- and else parts would not work in the case that that item is empty. What remains is the workaround of using a parameter tag with the separator as default, and a conditional parameter name which expands to the name of an undefined parameter if the separator is needed (i.e., there is another item), and otherwise expanding to the name of a parameter having the empty string as value. To avoid that users of the template have to put "empty=" as parameter, which would bother them with an internal technical problem, the auxiliary Template:For/aux with this "empty=" is needed. All parameters of Template:for have to be passed on to Template:For/aux.
 +
 +
In addition:
 +
 +
*Undefined parameters in the list are given the value "undefined".<ref>Alternatively the undefinedness could have been preserved, see [[Help:Template#A parameter value depending on parameters]].</ref>
 +
*Other undefined parameters are given their default value.
 +
 +
Pipes and equals signs cannot be conditional inside a template call, they only work if they are explicit. To avoid writing out six template calls, one for each number of parameters with constant value (zero through five), a default is used for the names of these parameters. This default name is the value of parameter u and should not be used by template ''call'', or if it is used, it should be used in such a way that empty and undefined are treated equally, because the default for the values of the parameters with constant value is the empty string, and one may assume that if no name is specified for a parameter with constant value, no corresponding value is specified either. Note that "using a parameter" includes having a  parameter tag with a parameter name that is chosen to be one not intended to occur in any call of the template, as is sometimes done for technical reasons. Such designs can make "unlikely" parameter names such as the empty string more likely. Thus the parameter u can be useful. The default value of u is the empty string, not only because it might be unlikely, but also because it minimizes the wikitext on substitution.
  
 
==See also==
 
==See also==
*[[mw:Template:Fe]]
 
 
*{{tim|foreach}}
 
*{{tim|foreach}}
 
*{{tim|tblb}}
 
*{{tim|tblb}}
*{{tiw|for nowiki}}
+
*{{tiw|for loop}}
*{{tiw|item}}
 
 
*{{tiw|loop}}
 
*{{tiw|loop}}
*{{tiw|repeat}}
+
*[[mw:Template:Fe]]
*{{tiw|simple recursion}}
 
 
*[[Help:Parameter default]]
 
*[[Help:Parameter default]]
 
*[[Help:Array]]
 
*[[Help:Array]]
 
*[[mw:Extension:LoopFunctions]]
 
*[[mw:Extension:LoopFunctions]]
  
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||
+
{{refs}}
<!-- CATEGORIES HERE, THANKS -->
+
<includeonly>[[Category:Repetition templates]]</includeonly>
[[Category:Wikipedia formatting and function templates]]
 
}}</includeonly>
 

Revision as of 17:51, 14 August 2018

Template:Documentation subpage Uses Template:For/aux.

Description

The template calls a specified template up to 150 times, where one parameter takes each of a list of values; in addition to the variable parameter, for up to 4 parameters a fixed value can be specified.

Usage

(Note the change for the constant values.)

{{for
  |call=template to call (default: 1x)
  |what to separate the entries with (unnamed parameter; required, but can be the empty string)
  |pc1n=name of first parameter with constant value (default: the value of parameter u)
  |pc1v=first constant value (default: empty)
  |pc2n=name of second parameter with constant value (default: the value of parameter u)
  |pc2v=second constant value (default: empty)
  |pc3n=name of third parameter with constant value (default: the value of parameter u)
  |pc3v=third constant value (default: empty)
  |pc4n=name of fourth parameter with constant value (default: the value of parameter u)
  |pc4v=fourth constant value (default: empty)
  |pc5n=name of fifth parameter with constant value (default: the value of parameter u)
  |pc5v=fifth constant value (default: empty)
  |pv=name of variable parameter (default: 1)
  |prefix=constant first part of the value of pv (default: empty)
  |postfix=constant last part of the value of pv (default: empty)
  |list of 0 to 150 parameter values (without prefixes and postfixes), with separator "|"
     (empty parameter values work in the regular way, they are ''not'' ignored like undefined ones)
  |u=name of a parameter that is not used in template ''call'' (default: empty; see also below)

}}

The separator is unnamed, allowing it to start and/or end with newlines and spaces. This parameter should be put before the list. If a parameter value in the list contains an equals sign the list should use named parameters, at least from that point, where it should be noted 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 then represent it with {{=}}, see Template:Tim.

Examples

Code Result
1{{for|, 1|00|01|02|03|04|05|06|07|08|09
|10|11|12|13|14|15|16|17|18|19
|20|21|22|23|24|25|26|27|28|29
|30|31|32|33|34|35|36|37|38|39
|40|41|42|43|44|45|46|47|48|49
|50|51|52|53|54|55|56|57|58|59
|60|61|62|63|64|65|66|67|68|69
|70|71|72|73|74|75|76|77|78|79
|80|81|82|83|84|85|86|87|88|89
|90|91|92|93|94|95|96|97|98|99
  |call=1x
}}
1Template:For

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


{{for|-|a|3||c|g}} gives

Template:For

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

Template:For

{{for|

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

Template:For

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

gives:

Test
Template:For

Using Template:Tim,

{| class="wikitable sortable" 
|-
!number!!reciprocal
|-
| {{for|
{{!}}-
{{!}} |call=table row example|3|1|5|3|2}}
|}

gives

number reciprocal
Template:For

{{for| / |call=short DOW|3|1|5|3|2}} using Template:Tim gives

Template:For

{{for|; |call=t2|pc1n=2|pc1v=constant|abc|def|ghi|postfix=pof}} using Template:Tim gives:

Template:For


{{for|, |call=concat and link to w:en:|pc1n=1|pc1v=John_|pv=2||Lennon|Wayne}} using Template:Timc gives Template:For

A for-call can give a row or column of choice, see e.g. Template:Power.

A constant parameter is ignored if it is the same as the (implicitly or explicitly given) variable parameter:

{{for|; |call=t2|pc1n=1|pc1v=7|pc2n=2|pc2v=5|abc|def|ghi}} using Template:Tim gives:

Template:For


Using Template:Tim, {{#expr:{{for||call=plus square|pv=1|1|2|3|4|5}}}} gives Expression error: Unrecognized punctuation character "["..

Semi-recursive: see Template talk:Foreach/recursion

Value "undefined"

If a value in the list of values is "undefined" this value is ignored, just like the unused parameters at the end if there are less than 150 values (see also below):

{{for|-|a|3|undefined|c|g|call=3x}} gives Template:For

Substitution

For successive levels of substitution use one or more or the following (the first, the first two, the first three, or all)

  • substf=subst: - substitute the parser functions in Template:Tim
  • substfa=subst: - substitute the call of auxiliary Template:Tim
  • substa=subst: - substitute the parser functions in for/aux (reduces the number of items from 150 to the actual number)
  • substfa=subst: - substitute the calls of the user template

If at least the first three 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.

Internal technical details

The template calls Template:For/aux. It is necessary for avoiding a newline if the separator starts with one of the characters "#&:;" (In particular the semicolon is a likely character to be at the start of the separator.) This is because of the automatic newline if the separator is produced by a template or parser function. Even the workaround of moving the previous item into the then- and else parts would not work in the case that that item is empty. What remains is the workaround of using a parameter tag with the separator as default, and a conditional parameter name which expands to the name of an undefined parameter if the separator is needed (i.e., there is another item), and otherwise expanding to the name of a parameter having the empty string as value. To avoid that users of the template have to put "empty=" as parameter, which would bother them with an internal technical problem, the auxiliary Template:For/aux with this "empty=" is needed. All parameters of Template:for have to be passed on to Template:For/aux.

In addition:

  • Undefined parameters in the list are given the value "undefined".[1]
  • Other undefined parameters are given their default value.

Pipes and equals signs cannot be conditional inside a template call, they only work if they are explicit. To avoid writing out six template calls, one for each number of parameters with constant value (zero through five), a default is used for the names of these parameters. This default name is the value of parameter u and should not be used by template call, or if it is used, it should be used in such a way that empty and undefined are treated equally, because the default for the values of the parameters with constant value is the empty string, and one may assume that if no name is specified for a parameter with constant value, no corresponding value is specified either. Note that "using a parameter" includes having a parameter tag with a parameter name that is chosen to be one not intended to occur in any call of the template, as is sometimes done for technical reasons. Such designs can make "unlikely" parameter names such as the empty string more likely. Thus the parameter u can be useful. The default value of u is the empty string, not only because it might be unlikely, but also because it minimizes the wikitext on substitution.

See also

Template:Refs

  1. Alternatively the undefinedness could have been preserved, see Help:Template#A parameter value depending on parameters.