Template:For loop
| Uses Lua: |
This template implements the for loop and foreach loop computer programming constructs as applied to MediaWiki templates. It calls a user-specified template (the "called template") multiple times: once for each value in either:
- an iterated sequence, or
- an explicit list.
Each value in the sequence or list is passed to the same specified parameter (the "variable parameter") of the called template. Optionally, pre- and postfix additions can be attached to the passed values. In addition to the specified variable parameter, other parameters of the called template (the "static parameters") may be assigned a value which remains constant for each iteration.
Usage
[edit]| Group | Parameter | Meaning | Default value |
|---|---|---|---|
| Mandatory | |1= (unnamed)
|
The separator to output between template calls; may be blank, and whitespace is preserved. | |
|call=
|
The template to call at each iteration. | ||
|pv=
|
The name (or number) of the variable parameter of the called template to iterate over. |
| |
| Option 1: Iterated sequence |
|start=
|
The first numeric value to pass to the variable parameter. |
|
|stop=
|
The maximum numeric value for the variable parameter. | ||
|by=
|
The iteration step size between values passed to the variable parameter. |
| |
| Option 2: Explicit values |
(unnamed parameters) | The explicit values, given as separate parameters (whitespace is stripped). | |
|skipBlanks=
|
When set to , empty parameter values are skipped.
|
| |
| Other optional parameters |
|pcNn=
|
The name (or number) of the Nth static parameter of the called template. | |
|pcNv=
|
The value for the Nth static parameter of the called template. | ||
|prefix=
|
The static prefix to prepend to each value passed to the variable parameter. | ||
|postfix=
|
The static postfix appended to each value passed to the variable parameter. | ||
|substall=
|
When set to , the called template is not substituted when {{For loop}} is substituted.
|
|
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=, however it is not output after the last call.
Caution
[edit]- The separator can be prefixed with
|1=, but in that case it cannot contain newlines or spaces that precede or follow it. - If any parameter value contains an equality symbol (
), use={{=}}(see template usage hints and workarounds). - If using numbered parameters:
- The first value is parameter 2 (e.g.
|2=yourFirstValue), because parameter|1=is the separator. - You must not skip any numbers. The loop will terminate after the first absent numbered parameter. (Parameters can be blank, but not absent.)
- The first value is parameter 2 (e.g.
Substitution
[edit]The template is Lua-based and thus 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. For example:
{{subst:For loop|sep |01|02|03|04|05|06|07|call=1x}}↩
01sep 02sep 03sep 04sep 05sep 06sep 07
{{subst:For loop|sep |01|02|03|04|05|06|07|call=1x|substall=no}}⮯
{{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
[edit]| Form | Code | Explanation | Result | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Iterator | {{For loop|<hr />|call=Box
| pv = image-size
| start = 64
| stop = 128
| by = 32
| prefix = x
| pc1n = header
| pc1v = Example
| pc2n = image
| pc2v = Feed-icon_headphones.svg
| pc3n = background-color
| pc3v = yellow
}}
|
Call the {{Box}} template with the values
separating outputs with a horizontal rule. |
| |||||||||
| Explicit values | {{For loop|, |call=2x
| prefix = 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 the template {{2x}} (which just repeats its input twice) with values "1NN", where NN = "00" through "99" (given explicitly), separating the outputs with the string ""
|
Lua error: expandTemplate: template "2x" does not exist. | |||||||||
| Explicit values | {{For loop||call=Tsh|skipBlanks=true
|{{#switch:{{ROOTPAGENAME}}
| Namespace links = ln
| Article links = la
}}
}}
|
{{For loop}} is useful for omitting a template call with one parameter being an inline expression that can be empty. It avoids repeating the parameter that the usual {{{1|}}} {{#if:{{{1|}}}|{{Example|{{{1}}}}}}} does.
|
({{For loop}} helped avoid the error message) |
Other examples with explicit values
[edit]{{For loop|-|a|3||c|g|call=3x}}
Lua error: expandTemplate: template "3x" does not exist.
{{For loop|
|a|3||c|g|call=3x}}
Lua error: expandTemplate: template "3x" does not exist.
{| class="sortable wikitable"
! Test
|-
| {{For loop|
{{!}}-
{{!}} |a|b|c|d|e|call=3x}}
|}
gives
| Test |
|---|
| Lua error: 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
|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
|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
|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|call=1x}}
gives: 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 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
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
gives:
begin->01= 02= 03= 04= 05= 06= 07= 08= 09= 10= 11==== 12= 13= 14= 15= 16= 17= 18= 19<-end
See also
[edit]- {{Tblb}} on Meta-Wiki
- At MediaWiki:
- {{Fe}}
- {{Foreach}}
- The LoopFunctions extension
- The Loops extension
- On English Wikipedia:
- {{For nowiki}}
- {{Item}}
- {{Loop}}
- {{Simple recursion}}
- Help:Parameter default
- Help:Array
| The above documentation is transcluded from Template:For loop/doc (edit | history). Editors can experiment in this template's sandbox (create | mirror) and testcases (create) page. Please add categories to the /doc subpage. Subpages of this template. |