Jump to content

Help:Parameter default: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
m Reverted changes by Bybr (talk) to last version by Tegel
Tag: Rollback
Replaced content with "{{MovedToMediaWiki|Manual:Advanced templates#Use of parameter default}}"
Tag: Replaced
 
Line 1: Line 1:
{{MovedToMediaWiki|Manual:Advanced templates#Use of parameter default}}
{{h:h|editor toc}}

In non-substituted [[Special:MyLanguage/Help:Template|template]] [[Special:ExpandTemplates|expansion]], an expression <code><nowiki>{{{p|q}}}</nowiki></code> inside the template is expanded to <code>{{{p}}}</code> if that is defined, and else to the [[w:Default argument|default argument]] <code>q</code>. Thus the default can be specified independently for every occurrence of the parameter.

These results <code>{{{p}}}</code> and <code>q</code> can be end results, but they can also be in an expression for a parameter name (inside a pair of triple braces or in a template call or a switch), a parameter value (in the call of a template or parser function), or name of a template, parser function or variable.

Similarly, on [[Special:MyLanguage/Help:Substitution|substitution]] of a template, an expression <code><nowiki>{{{p|q}}}</nowiki></code> inside the template, where p is a parameter name (or an expression which is simultaneously substituted and results in a parameter name) and q is any wikitext (with restrictions regarding pipes and triple braces) is processed as follows:
*if <code>p</code> is defined, <code><nowiki>{{{p|q}}}</nowiki></code> is changed to the wikitext of the value of p (or the wikitext to which that is changed if there is simultaneous substitution inside that wikitext)
*if <code>p</code> is undefined, <code><nowiki>{{{p|q}}}</nowiki></code> is changed to <code>q</code>, possibly with simultaneous substitutions inside the wikitext for <code>q</code>.

==Notes==
In the case of multiple pipes, anything from the second pipe is ignored: <code><nowiki>{{{a|b|c|d}}}</nowiki></code> is equivalent with <code><nowiki>{{{a|b}}}</nowiki></code>. The default part can only contain "<code>|</code>" as part of full template, parser function, parameter, link, or image syntax within it, within nowiki-tags, and as content of a template, as in [[mw:Special:MyLanguage/Help:Magic words#Other|Template:!]].

The expression for the parameter name can also contain "<code>|</code>", as part of full template, parser function, or parameter syntax within it.

Examples, in some cases using {{timc|3x}}<br />and {{timc|t2}}:
*<code><nowiki>{{{a|b|c|d}}}</nowiki></code> gives <code>{{{a|b|c|d}}}</code> - second pipe onward is ignored
*<code><nowiki>{{{a|{{3x|b}}}}}</nowiki></code> gives <code>{{{a|{{3x|b}}}}}</code>
*<code><nowiki>{{{a|{{{b|c}}}}}}</nowiki></code> gives <code>{{{a|{{{b|c}}}}}}</code>
*<code><nowiki>{{{a|[[b|c]]}}}</nowiki></code> gives <code>{{{a|[[b|c]]}}}</code>
*<code><nowiki>{{{{{3x|a}}|b}}}</nowiki></code> gives <code>{{{{{3x|a}}|b}}}</code> - parameter aaa is undefined
*<code><nowiki>{{{{{{a|b}}}|c}}}</nowiki></code> gives <code>{{{{{{a|b}}}|c}}}</code> - parameter b is undefined

*<code><nowiki>{{{a|<nowiki>b|c</nowiki><nowiki><</nowiki>/nowiki>}}}</code> gives <code>{{{a|<nowiki>b|c</nowiki>}}}</code> - works fine for rendering text, but is not suitable for putting parameters b and c in a template call (there is no function for removing nowiki tags):
*:<code><nowiki>{{t2|{{{a|<nowiki>b|c</nowiki><nowiki><</nowiki>/nowiki>}}}}}</code> gives <code>{{t2|{{{a|<nowiki>b|c</nowiki>}}}}}</code>

==Repetition==
A "for-loop" is achieved using

{{tim|fors}}, containing:
----
<pre>{{fors/aux
|v@=
|c={{{call}}}
|pv={{{pv|1}}}
|s={{{sep|}}}
|pc1={{{pc1|=}}}
|pc2={{{pc2|=}}}
|pc3={{{pc3|=}}}
|pc4={{{pc4|=}}}|
1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}}
}}</pre>
---- with {{tim|fors/aux}}, containing:
----
<pre>{{{v{{{1}}}|{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{1}}}}}}}}
{{{v{{{2}}}|{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{2}}}}}}}}
{{{v{{{3}}}|{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{3}}}}}}}}</pre>
----

In short form the latter consists of components

<code>[ v[''i''] | [s] {{[c]|[pc1]|[pc2]|[pc3]|[pc4]|[pv]=[''i'']}} ]</code> (''i'' = 1, 2, 3, for ''i'' = 1 without [s])

or in terms of the parameters of the first template:

<code>[v[''i''|@] | [sep|] {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=[''i'']}} ]</code>

This is indeed of the above-mentioned form <code>[a[b|c]|f([b])]</code>, with <code>a=v</code>, <code>b=''i''</code>, <code>c=@</code>, and

<code>f(x) = [sep|] {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=x}}</code>

The assumptions mentioned above apply for d equal to null, and provided that no <code>[''i'']</code> is equal to <code>"@"</code>.

===Variations===
Since <code>v@=null</code> we can also take make the concatenation the outer operation:
<code>[v[''i''|@] | [sep|]] [v[''i''|@] | {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=[''i'']}} ]</code>

== Last-but technique ==
Sometimes the last or last but <code>''x''</code> parameter value may be needed, but the numbers of supplied parameters is unknown. The following is a technique to get last "assigned" parameter value without using a template:<br>
<code>[4|[3|[2|[1|*]]]]</code>.

Then last but one can be achieved like this: ("<code>ifu</code>" is preassigned with empty)
<pre>
[[if[4|u]|3]|
[[if[3|u]|2]|
[[if[2|u]|1]|
[[if[1|u]|0]|*] //this line is actually useless, can be replaced by "*".
]
]
]
</pre>
I.e., to replace "4", "3", "2", "1" with D<sub>4</sub>, D<sub>3</sub>, D<sub>2</sub>, D<sub>1</sub>. And D<i><sub>x</sub></i>=[if[<i>x</i>|u]|<i>x-1</i>]. Like this, you can get "last but <i>x</i>" parameter value.<br>
Examples using {{tim|lastbut0}} and {{tim|lastbut1}}:
*<code><nowiki>{{lastbut0}}</nowiki></code> gives {{lastbut0}}.
*<code><nowiki>{{lastbut0|a}}</nowiki></code> gives {{lastbut0|a}}.
*<code><nowiki>{{lastbut0|a|b|c|d|e|f|g|h|i|j}}</nowiki></code> gives {{lastbut0|a|b|c|d|e|f|g|h|i|j}}.
*<code><nowiki>{{lastbut1|ifu=}}</nowiki></code> gives {{lastbut1|ifu=}}.
*<code><nowiki>{{lastbut1|ifu=|a}}</nowiki></code> gives {{lastbut1|ifu=|a}}.
*<code><nowiki>{{lastbut1|ifu=|a|b}}</nowiki></code> gives {{lastbut1|ifu=|a|b}}.
*<code><nowiki>{{lastbut1|ifu=|a|b|c|d|e|f|g|h|i|j}}</nowiki></code> gives {{lastbut1|ifu=|a|b|c|d|e|f|g|h|i|j}}.
*<code><nowiki>{{lastbut0|53=53|81=81|28=28}}</nowiki></code> gives {{lastbut0|53=53|81=81|28=28}} (finds maximum of a list of integers in the range 1 - 100)

See also: {{timcnl|max}}

==History==
Parameter defaults were introduced before [[mw:Extension:ParserFunction|parser functions]], and were therefore originally used as a way of creating conditional statements. This was superseded by the <code>#if</code> parser function, and later by [[mw:Extension:Scribunto|Scribunto]].


{{h:f|enname=Parameter default}}

[[Category:Editor handbook]]

Latest revision as of 23:50, 28 June 2024