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
Substitution: works now
Line 267: Line 267:
{{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|}}}}}
{{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.
[[Special:ExpandTemplates]] gives full substitution.
 
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==
==Related templates==

Revision as of 09:04, 11 November 2007

Template:for (talk, backlinks, edit) containing:


{{for/aux
  |c={{{call}}}
  |pv={{{pv|1}}}
  |prefix={{{prefix|}}}
  |postfix={{{postfix|}}}
  |s={{{sep|}}}
  |pc1={{{pc1|=}}}
  |pc2={{{pc2|=}}}
  |pc3={{{pc3|=}}}
  |pc4={{{pc4|=}}}|
1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}}|...|150={{{150|@}}}
}}

using Template:for/aux (talk, backlinks, edit) containing:


{{{{{subst|}}}#ifeq:{{{1|@}}}|@||       {{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{1  }}}{{{postfix}}}}}}}}
{{{{{subst|}}}#ifeq:{{{2|@}}}|@||{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{2  }}}{{{postfix}}}}}}}}
{{{{{subst|}}}#ifeq:{{{3|@}}}|@||{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{3  }}}{{{postfix}}}}}}}}
...
{{{{{subst|}}}#ifeq:{{{150|@}}}|@||{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{prefix}}}{{{150}}}{{{postfix}}}}}}}}

without the newlines and spaces.

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

{{for
  |call=template to call
  |sep=what to separate the entries with
  |pc1=parameter name=constant value
  |pc2=parameter name=constant value
  |pc3=parameter name=constant value
  |pc4=parameter name=constant value
  |pv=name of variable parameter
  |prefix=constant first part of the value of pv
  |postfix=constant last part of the value of pv
  |list of up to 150 parameter values (without prefixes and postfixes), with separator "|"
}}

Examples

Code Result
1{{for
|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=x1
  |sep=, 1
}}
1Lua error: expandTemplate: template "x1" does not exist.

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|call=x3|sep=-}} using

Timbe: x3

gives

Lua error: expandTemplate: template "x3" does not exist.

{{for|p|||3||c|call=t|sep=///}} using

Timbe: t

gives

{{For/doc}}p{{For/doc}}p{{3}}p{{For/doc}}p{{C}}

{{for|call=short DOW|3|1|5|3|2|sep=&nbsp;/&nbsp;}} using

Timbe: short DOW

gives

Lua error: expandTemplate: template "short DOW" does not exist.

{{for|call=t2|pc1=2=constant|abc|def|ghi}} using

Timbe: t2

gives:

{{Def}}abc{{Ghi}}


{{for|call=concat and link to w:en:|sep=,&nbsp;|pc1=John_|pv=2||Lennon|Wayne}} using Template:Timc gives Lua error: expandTemplate: template "concat and link to w:en:" does not exist.

A for-call can give a row or column of choice, e.g. using

Timbe: power

:

{{for|sep=,|2|3|call=power|prefix=2,}} gives Lua error: expandTemplate: template "power" does not exist.

{{for|sep=,|2|3|call=power|prefix=3,}} gives Lua error: expandTemplate: template "power" does not exist.

{{for|sep=,|2|3|call=power|postfix=,2}} gives Lua error: expandTemplate: template "power" does not exist.

{{for|sep=,|2|3|call=power|postfix=,3}} gives Lua error: expandTemplate: template "power" does not exist.



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

{{for|call=t2|pc1=1=7|pc2=2=5|abc|def|ghi|sep=,}} using

Timbe: t2

gives:

{{Def}}abc{{Ghi}}


{{for|sep=,|call=x3|1=a=b|2=c=d|3=e}} gives Lua error: expandTemplate: template "x3" does not exist.


{{#expr:{{for|call=plus square|pv=1|1|2|3|4|5}}}} gives Expression error: Unexpected < operator..

Semi-recursive: see Template talk:Foreach/recursion

XML:

<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>

gives

Lua error: expandTemplate: template "X1" does not exist.
Lua error: expandTemplate: template "square" does not exist.

HTML:

<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>

gives

Lua error: expandTemplate: template "X1" does not exist.
Lua error: expandTemplate: template "square" does not exist.

Wikitable:

{| border=1
|-
|{{for|call=X1|3|1|5|3|2|sep={{!!}}}}
|-
|{{for|call=square|3|1|5|3|2|sep={{!!}} }}
|}

gives

Lua error: expandTemplate: template "X1" does not exist.
Lua error: expandTemplate: template "square" does not exist.

XML:

<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>

gives

Lua error: expandTemplate: template "table row example" does not exist.

HTML:

<table border=1>
<tr><td>{{for|call=table row example|3|1|5|3|2|sep=<tr><td>|pc1=sep1=<td>}}
</table>

gives

Lua error: expandTemplate: template "table row example" does not exist.

Wikitable:

{| border=1
|-
|{{for|call=table row example|3|1|5|3|2|sep=&nbsp;
{{!}}-
{{!}}|pc1=sep1={{!!}}}}
|}

gives

Lua error: expandTemplate: template "table row example" does not exist.

Wikitable sortable:

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

gives

number square
Lua error: expandTemplate: template "table row example" does not exist.

Reserved name

A reserved name that cannot be used as list item is "@"; it is ignored, but e.g. @1 is not:

{{for|a|@|@1|b|c|call=x3|sep=-}} gives

Lua error: expandTemplate: template "x3" does not exist.

Substitution

Substitution through Template:For/aux works also, as follows:

{{subst:for|a|3||c|call=x3|sep=-|subst=subst:}} using

Timbe: x3

gives:

Template:X3-Template:X3-Template:X3-Template:X3

Special:ExpandTemplates gives full substitution.

This template is another version of

Timbe: foreach

. That version has stopped working due to some software change.

Comparison:

{{{v{{{2 }}}|{{{s }}}{{{{{c}}}|{{{pc1}}}|..|{{{pc4}}}|{{{pv}}}={{{2}}}}}}}}
{{{v{{{2|}}}|{{{s|}}}}}}{{{{{{{{v{{{2|}}}|c}}}|x0}}} |{{{pv}}}={{{2}}}|{{{pc1}}}|..|{{{pc4}}}}}

In the first case (for) the whole output is the default of a parameter named v{{{i }}} that is defined iff the ith 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 ith parameter is not defined a dummy template is called.

Both are based on en:Template:foreach (talk, backlinks, edit), 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

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