Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Template:For/doc: Difference between revisions

Template page
Fix language-code links to use Wikipedia interwiki prefixes
 
(23 intermediate revisions by 8 users not shown)
Line 1: Line 1:
[[Template:for]] ([[Template talk:for|talk]], [[Special:Whatlinkshere/Template:for|backlinks]], [http://meta.wikimedia.org{{localurl:Template:for|action=edit}} edit]) containing:
{{for2|the template equivalent to <nowiki>{{</nowiki>[[w:template:for|for]]<nowiki>}}</nowiki> on the English Wikipedia|[[template:for2]]}}
----
{{Lua|Module:ForLoop}}
<pre>
{{TemplateBox
{{for/aux
  |1        = 1
   |c={{{call}}}
  |1label-en= separator
   |pv={{{pv|1}}}
  |1d-en    = What to separate entries with
   |prefix={{{prefix|}}}
  |1aliases = <!-- List of aliases, separated by / (a slash) -->
   |postfix={{{postfix|}}}
  |1type    = <!-- number, string, string/wiki-user-name, string/wiki-page-name, string/line; unknown -->
   |s={{{sep|}}}
  |1def    = <!-- default value for the parameter -->
   |pc1={{{pc1|=}}}
  |1stat    = suggested
   |pc2={{{pc2|=}}}
 
   |pc3={{{pc3|=}}}
  |2        = call
   |pc4={{{pc4|=}}}|
  |2label-en= template
1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}}|...|150={{{150|@}}}
   |2d-en    = The template to call in each iteration
}}
  |2aliases =
</pre>
  |2type    = wiki-template-name
----
  |2def    = void
  |2stat    = required
 
   |3        = pc1n
  |3label-en= constant parameter 1
   |3d-en    = The first constant parameter’s name
  |3aliases =
  |3type    = line
  |3def    =  
  |3stat    = optional
 
   |4        = pc1v
  |4label-en= constant value 1
   |4d-en    = The first constant parameter’s value
  |4aliases =
   |4type    =  
  |4def    =  
   |4stat    = optional
 
  |5        = pc2n
   |5label-en= constant parameter 2
  |5d-en    = The second constant parameter’s name
   |5aliases =  
  |5type    = line
  |5def    =
  |5stat    = optional
 
  |6        = pc2v
  |6label-en= constant value 2
  |6d-en    = The second constant parameter’s value
  |6aliases =
  |6type    =
  |6def    =
  |6stat    = optional
 
  |7        = pc3n
  |7label-en= constant parameter 3
  |7d-en    = The third constant parameter’s name
  |7aliases =
  |7type    = line
  |7def    =
  |7stat    = optional
 
  |8        = pc3v
  |8label-en= constant value 3
  |8d-en    = The third constant parameter’s value
  |8aliases =
  |8type    =
  |8def    =
  |8stat    = optional
 
  |9        = pc4n
  |9label-en= constant parameter 4
  |9d-en    = The fourth constant parameter’s name
  |9aliases =
  |9type    = line
  |9def    =
  |9stat    = optional
 
  |10        = pc4v
  |10label-en= constant value 4
  |10d-en    = The fourth constant parameter’s value
  |10aliases =
  |10type    =
  |10def    =  
  |10stat    = optional
 
  |11        = pv
  |11label-en= variable parameter
  |11d-en    = The variable parameter’s name
  |11aliases =
  |11type    = line
  |11def    = 1
  |11stat    = optional
 
  |12        = prefix
  |12label-en= variable prefix
  |12d-en    = Constant prefix of the variable parameter’s value
  |12aliases =
  |12type    =
  |12def    =
  |12stat    = optional


using [[Template:for/aux]] ([[Template talk:for/aux|talk]], [[Special:Whatlinkshere/Template:for/aux|backlinks]], [http://meta.wikimedia.org{{localurl:Template:for/aux|action=edit}} edit]) containing:
  |13        = postfix
----
  |13label-en= variable postfix
<pre>
  |13d-en    = Constant postfix of the variable parameter’s value
{{{{{subst|}}}#ifeq:{{{1|@}}}|@||      {{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{1  }}}{{{postfix}}}}}}}}
  |13aliases =
{{{{{subst|}}}#ifeq:{{{2|@}}}|@||{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{2  }}}{{{postfix}}}}}}}}
  |13type    =  
{{{{{subst|}}}#ifeq:{{{3|@}}}|@||{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{3  }}}{{{postfix}}}}}}}}
  |13def    =  
...
  |13stat    = optional
{{{{{subst|}}}#ifeq:{{{150|@}}}|@||{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{150}}}{{{postfix}}}}}}}}
</pre>
----
without the newlines and spaces.


== Description ==
  |14        = 2
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.
  |14label-en= variable parameters
  |14d-en    = The variable parameters’ values. Each value goes in its own parameter, starting with parameter 2.
  |14aliases = 3 / 4 / …
  |14type    =
  |14def    =
  |14stat    = required


== Usage ==
  |name              = for
<pre>
  |desc              = The template calls a specified template several 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.
{{for
  |usage-notes        =  
   |call=template to call
  |namespace          =  
   |sep=what to separate the entries with
  |usergroup          =
   |pc1=parameter name=constant value
  |shorthand          =
   |pc2=parameter name=constant value
   |mustbesubst        =  
   |pc3=parameter name=constant value
   |setscats          =  
   |pc4=parameter name=constant value
   |placement          =  
   |pv=name of variable parameter
  |example            =  
   |prefix=constant first part of the value of pv
  |example-value     =
   |postfix=constant last part of the value of pv
   |relieson          =  
   |list of up to 150 parameter values (without prefixes and postfixes), with separator "|"
  |seealso            =  
   |print              = multi
  |type              =  
   |i18n-method        = -
   |i18n-desc          =  
   |i18n-subpage      =  
   |i18n-mediawiki-msg =  
   |useTemplateData    =
}}
}}
</pre>


==Examples==
== Example ==
{| class="wikitable", border=1
{| class="wikitable"
!Code
!Code
!Result
!Result
|-
|-
|
|List of all numbers from 100 to 199, separated by commas:<br>
<pre>1{{for
<pre>1{{for|, 1|call=x1
|00|01|02|03|04|05|06|07|08|09
|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
Line 65: Line 153:
|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=x1
  |sep=, 1
}}</pre>
}}</pre>
|1{{for
<nowiki>
|1{{for|, 1|call=x1
|00|01|02|03|04|05|06|07|08|09
|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
Line 79: Line 166:
|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=x1
  |sep=, 1
}}
}}
|}
</nowiki>
 
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|call=x3|sep=-}}</nowiki> using {{tim|x3}} gives
:{{for|a|3||c|call=x3|sep=-}}
 
----
 
<nowiki>{{for|p|||3||c|call=t|sep=///}}</nowiki> using {{tim|t}} gives
:{{for|p|||3||c|call=t|sep=///}}
 
----
 
<nowiki>{{for|call=short DOW|3|1|5|3|2|sep=&amp;nbsp;/&amp;nbsp;}}</nowiki> using {{tim|short DOW}} gives
:{{for|call=short DOW|3|1|5|3|2|sep=&nbsp;/&nbsp;}}
 
----
 
<nowiki>{{for|call=t2|pc1=2=constant|abc|def|ghi}}</nowiki> using {{tim|t2}} gives:
 
{{for|call=t2|pc1=2=constant|abc|def|ghi}}
 
----
 
<nowiki>{{for|call=concat and link to w:en:|sep=,&amp;nbsp;|pc1=John_|pv=2||Lennon|Wayne}}
</nowiki> using {{timc|concat and link to w:en:}} gives {{for|call=concat and link to w:en:|sep=,&nbsp;|pc1=John_|pv=2||Lennon|Wayne}}
 
A for-call can give a row or column of choice, e.g. using {{tim|power}}:
 
<nowiki>{{for|sep=,|2|3|call=power|prefix=2,}}</nowiki> gives {{for|sep=,|2|3|call=power|prefix=2,}}
 
<nowiki>{{for|sep=,|2|3|call=power|prefix=3,}}</nowiki> gives {{for|sep=,|2|3|call=power|prefix=3,}}
 
<nowiki>{{for|sep=,|2|3|call=power|postfix=,2}}</nowiki> gives {{for|sep=,|2|3|call=power|postfix=,2}}
 
<nowiki>{{for|sep=,|2|3|call=power|postfix=,3}}</nowiki> gives {{for|sep=,|2|3|call=power|postfix=,3}}
 
 
----
 
A constant parameter is ignored if it is the same as the (implicitly or explicitly given) variable parameter:
 
<nowiki>{{for|call=t2|pc1=1=7|pc2=2=5|abc|def|ghi|sep=,}}</nowiki> using {{tim|t2}} gives:
 
{{for|call=t2|pc1=1=7|pc2=2=5|abc|def|ghi|sep=,}}
 
----
 
<nowiki>{{for|sep=,|call=x3|1=a=b|2=c=d|3=e}}</nowiki> gives
{{for|sep=,|call=x3|1=a=b|2=c=d|3=e}}
 
----
 
<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}}}}.
 
Semi-recursive: see [[Template talk:Foreach/recursion]]
 
===[[Help:Table|Table]]s===
XML:
 
<pre>
<table border=1>
<tr><td>{{for|call=X1|3|1|5|3|2|sep=</td><td>}}</td></tr>
<tr><td>{{for|call=square|3|1|5|3|2|sep=</td><td>}}</td></tr>
</table>
</pre>
gives
<table border=1>
<tr><td>{{for|call=X1|3|1|5|3|2|sep=</td><td>}}</td></tr>
<tr><td>{{for|call=square|3|1|5|3|2|sep=</td><td>}}</td></tr>
</table>
----
HTML:
 
<pre>
<table border=1>
<tr><td>{{for|call=X1|3|1|5|3|2|sep=<td>}}
<tr><td>{{for|call=square|3|1|5|3|2|sep=<td>}}
</table>
</pre>
gives
<table border=1>
<tr><td>{{for|call=X1|3|1|5|3|2|sep=<td>}}
<tr><td>{{for|call=square|3|1|5|3|2|sep=<td>}}
</table>
----
Wikitable:
 
<pre>
{| border=1
|-
|-
|{{for|call=X1|3|1|5|3|2|sep={{!!}}}}
|An example used in {{tl|Facts/Major cities}}:<br>
|-
<pre>{{for||pc1n=decade|pc1v={{{2|{{{decade|}}}}}}|call=Facts/placeParts
|{{for|call=square|3|1|5|3|2|sep={{!!}} }}
|Amsterdam|Bangkok|Barcelona|Beijing|Berlin|Brussels|Buenos Aires|Cairo|Canton|Cape Town|Chicago|Delhi|Dubai|Frankfurt am Main|Geneva|Hong Kong|Istanbul|Jerusalem|Kolkata|Lagos, Nigeria|London|Los Angeles|Madrid|Mexico City|Moscow|Mumbai|Nairobi|New York City|Osaka|Paris|Rio de Janeiro (city)|Rome|São Paulo (city)|San Francisco|Seoul|Shanghai|Singapore|Stockholm|Sydney|Tokyo|Toronto|Washington, D.C.|Wuhan}}</pre>
<nowiki>
|{{for||pc1n=decade|pc1v={{{2|{{{decade|}}}}}}|call=Facts/placeParts
|Amsterdam|Bangkok|Barcelona|Beijing|Berlin|Brussels|Buenos Aires|Cairo|Canton|Cape Town|Chicago|Delhi|Dubai|Frankfurt am Main|Geneva|Hong Kong|Istanbul|Jerusalem|Kolkata|Lagos, Nigeria|London|Los Angeles|Madrid|Mexico City|Moscow|Mumbai|Nairobi|New York City|Osaka|Paris|Rio de Janeiro (city)|Rome|São Paulo (city)|San Francisco|Seoul|Shanghai|Singapore|Stockholm|Sydney|Tokyo|Toronto|Washington, D.C.|Wuhan}}
</nowiki>
|}
|}
</pre>
gives
{| border=1
|-
|{{for|call=X1|3|1|5|3|2|sep={{!!}}}}
|-
|{{for|call=square|3|1|5|3|2|sep={{!!}} }}
|}
----
XML:
<pre>
<table border=1>
<tr><td>{{for|call=table row example|3|1|5|3|2|sep=</td></tr><tr><td>|pc1=sep1=</td><td>}}
</table>
</pre>
gives
<table border=1>
<tr><td>{{for|call=table row example|3|1|5|3|2|sep=</td></tr><tr><td>|pc1=sep1=</td><td>}}
</table>
----
HTML:
<pre>
<table border=1>
<tr><td>{{for|call=table row example|3|1|5|3|2|sep=<tr><td>|pc1=sep1=<td>}}
</table>
</pre>
gives
<table border=1>
<tr><td>{{for|call=table row example|3|1|5|3|2|sep=<tr><td>|pc1=sep1=<td>}}
</table>
----
Wikitable:
<pre>
{| border=1
|-
|{{for|call=table row example|3|1|5|3|2|sep=&amp;nbsp;
{{!}}-
{{!}}|pc1=sep1={{!!}}}}
|}
</pre>
gives
{| border=1
|-
|{{for|call=table row example|3|1|5|3|2|sep=&nbsp;
{{!}}-
{{!}}|pc1=sep1={{!!}}}}
|}
----
Wikitable sortable:
<pre>
{| class="wikitable sortable"
|-
!number!!square
|-
|{{for|call=table row example|3|1|5|3|2|sep=&nbsp;
{{!}}-
{{!}}|pc1=sep1={{!!}}}}
|}
</pre>
gives
{| class="wikitable sortable"
|-
!number!!square
|-
|{{for|call=table row example|3|1|5|3|2|sep=&nbsp;
{{!}}-
{{!}}|pc1=sep1={{!!}}}}
|}
==Reserved name==
A reserved name that cannot be used as list item is "@"; it is ignored, but e.g. @1 is not:
<nowiki>{{for|a|@|@1|b|c|call=x3|sep=-}}</nowiki> gives
:{{for|a|@|@1|b|c|call=x3|sep=-}}
==Substitution==
Substitution through [[Template:For/aux]] works also, as follows:
<nowiki>{{subst:for|a|3||c|call=x3|sep=-|subst=subst:}}</nowiki> using {{tim|x3}} gives:
{{x3|{{{pc1|=}}}|{{{pc2|=}}}|{{{pc3|=}}}|{{{pc4|=}}}|{{{pv|1}}}={{{prefix|}}}a{{{postfix|}}}}}-{{x3|{{{pc1|=}}}|{{{pc2|=}}}|{{{pc3|=}}}|{{{pc4|=}}}|{{{pv|1}}}={{{prefix|}}}3{{{postfix|}}}}}-{{x3|{{{pc1|=}}}|{{{pc2|=}}}|{{{pc3|=}}}|{{{pc4|=}}}|{{{pv|1}}}={{{prefix|}}}{{{postfix|}}}}}-{{x3|{{{pc1|=}}}|{{{pc2|=}}}|{{{pc3|=}}}|{{{pc4|=}}}|{{{pv|1}}}={{{prefix|}}}c{{{postfix|}}}}}
[[Special:ExpandTemplates]] gives the correct results.
This is a consequence of the fact that, calling a template with <nowiki>{{{p|q}}}</nowiki>, with p undefined, subst results in the wikitext <nowiki>{{{p|q}}}</nowiki>, while [[Special:ExpandTemplates]] gives the wikitext q. [[Template:For/aux]] uses parameter names like <nowiki>v{{{12|@}}}</nowiki>; if parameter 12 is undefined it matters whether <nowiki>v{{{12|@}}}</nowiki> is taken as the actual name or reduces to the name v@.
==Related templates==
This template is another version of {{tim|foreach}}. That version has stopped working due to some software change.
Comparison:
<pre>
{{{v{{{2 }}}|{{{s }}}{{{{{c}}}|{{{pc1}}}|..|{{{pc4}}}|{{{pv}}}={{{2}}}}}}}}
{{{v{{{2|}}}|{{{s|}}}}}}{{{{{{{{v{{{2|}}}|c}}}|x0}}} |{{{pv}}}={{{2}}}|{{{pc1}}}|..|{{{pc4}}}}}
</pre>
In the first case (for) the whole output is the default of a parameter named v{{{''i'' }}} that is defined [[w:iff|iff]] the ''i''th parameter is ''not'' defined.
In the second case (foreach) the existence is checked twice, once for the separator and once for the main output; if the ''i''th  parameter  is not defined a dummy template is called.
Both are based on {{tiw|en|foreach}}, now deleted. Differences are:
*The Meta versions handle empty parameter values in the regular way, while the en version ignores them.
*Template For allows "=" in parameter values
*The difference in the case of conflicting parameter names, mentioned above.


==See also==
Note that the last part of the separator is used as a prefix; the first prefix needs to be put separately.<includeonly>
*{{tim|tblb}}
[[Category:Function templates]]</includeonly>
*[[Help:Parameter default]]
*[[Help:Array]]
*[[mw:Extension:LoopFunctions]]

Latest revision as of 14:22, 15 August 2026

The template calls a specified template several 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

{{for
 |1 =
 |call =
 |pc1n =
 |pc1v =
 |pc2n =
 |pc2v =
 |pc3n =
 |pc3v =
 |pc4n =
 |pc4v =
 |pv =
 |prefix =
 |postfix =
 |2 =
}}

Template parameters

ParameterDescriptionDefaultStatus
1separatorWhat to separate entries withemptysuggested
calltemplateThe template to call in each iterationvoidrequired
pc1nconstant parameter 1The first constant parameter’s nameemptyoptional
pc1vconstant value 1The first constant parameter’s valueemptyoptional
pc2nconstant parameter 2The second constant parameter’s nameemptyoptional
pc2vconstant value 2The second constant parameter’s valueemptyoptional
pc3nconstant parameter 3The third constant parameter’s nameemptyoptional
pc3vconstant value 3The third constant parameter’s valueemptyoptional
pc4nconstant parameter 4The fourth constant parameter’s nameemptyoptional
pc4vconstant value 4The fourth constant parameter’s valueemptyoptional
pvvariable parameterThe variable parameter’s name1optional
prefixvariable prefixConstant prefix of the variable parameter’s valueemptyoptional
postfixvariable postfixConstant postfix of the variable parameter’s valueemptyoptional
2
3
4
variable parametersThe variable parameters’ values. Each value goes in its own parameter, starting with parameter 2.emptyrequired

Additional information

The template is intended to be used in the following namespaces: no namespace specified

The template is intended to be used by the following user groups: no user group specified

Localization

This template is not intended to be localized.

Example

Code Result
List of all numbers from 100 to 199, separated by commas:
1{{for|, 1|call=x1
|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
}}

|1{{for|, 1|call=x1 |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 }}

An example used in {{Facts/Major cities}}:
{{for||pc1n=decade|pc1v={{{2|{{{decade|}}}}}}|call=Facts/placeParts
|Amsterdam|Bangkok|Barcelona|Beijing|Berlin|Brussels|Buenos Aires|Cairo|Canton|Cape Town|Chicago|Delhi|Dubai|Frankfurt am Main|Geneva|Hong Kong|Istanbul|Jerusalem|Kolkata|Lagos, Nigeria|London|Los Angeles|Madrid|Mexico City|Moscow|Mumbai|Nairobi|New York City|Osaka|Paris|Rio de Janeiro (city)|Rome|São Paulo (city)|San Francisco|Seoul|Shanghai|Singapore|Stockholm|Sydney|Tokyo|Toronto|Washington, D.C.|Wuhan}}

|{{for||pc1n=decade|pc1v={{{2|{{{decade|}}}}}}|call=Facts/placeParts |Amsterdam|Bangkok|Barcelona|Beijing|Berlin|Brussels|Buenos Aires|Cairo|Canton|Cape Town|Chicago|Delhi|Dubai|Frankfurt am Main|Geneva|Hong Kong|Istanbul|Jerusalem|Kolkata|Lagos, Nigeria|London|Los Angeles|Madrid|Mexico City|Moscow|Mumbai|Nairobi|New York City|Osaka|Paris|Rio de Janeiro (city)|Rome|São Paulo (city)|San Francisco|Seoul|Shanghai|Singapore|Stockholm|Sydney|Tokyo|Toronto|Washington, D.C.|Wuhan}}

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

Cookies help us deliver our services. By using our services, you agree to our use of cookies.