Help:Parameter default: Difference between revisions

Content deleted Content added
inuse
 
(29 intermediate revisions by 12 users not shown)
Line 1:
{{historical|reason=<br>Although some of it may still be relevant, this page describes techniques made obsolete by [[Help:ParserFunctions|ParserFunctions]] and [[Help:Lua|Lua]].|brief=y}}
 
{{inuse|for a short while to make wikicode clearer}}
'''Note:''' Information on this page relates to MediaWiki versions 1.6 and later.
 
Notation: on this page the wikitext <code>{{{A}}}</code> is written as {{mono|[{{var|a}}]}}.
 
In a [[help:template|template]], <code>[{{var|a}}|{{var|b}}]</code> is equal to {{mono|[{{var|a}}]}} if that is defined, and else equal to {{mono|{{var|b}}}}.
 
These results {{mono|[{{var|a}}]}} and {{mono|{{var|b}}}} can be end results, but they can also be parameter names, parameter values, template names, parts thereof, etc.
 
If the results are template names as part of template calls involving a parameter of the outer template which may be undefined, one can arrange that an inner template is called with a parameter depending on an undefined outer parameter only if the parameter concerned is not actually used in the inner template, e.g. dummy template {{Timc|x0}} can be called as <nowiki>{{tlx|x0|[a]}}</nowiki> even if {{mono|[{{var|a}}]}} is undefined, see e.g. {{Tim|foreach/pass1}}.
 
==Limitation==
In the case of multiple default parts, only the first applies: <code>[a|b|c|d]</code> is equivalent with <code>[a|b]</code>. The default part can only contain "<code>|"</code> as part of full template, parameter or link syntax within it, and within {{xtag|nowiki-tags}}. Note that "<code>|"</code> can be avoided in table syntax by applying HTML coding (element "<nowiki><{{tag|table></nowiki>"}}).
 
The expression for the parameter name can also contain "<code>|"</code>, as part of full template or parameter syntax within it.
 
Examples, using <code><nowiki>{{x3}}</nowiki></code> containing <syntaxhighlight lang="wikitext" inline>{{{1}}}{{{1}}}{{{1}}}</syntaxhighlight> and <syntaxhighlight lang="wikitext" inline>{{t2}}</syntaxhighlight> containing <syntaxhighlight lang="wikitext" inline>start-{{{1}}}-middle-{{{2}}}-end</syntaxhighlight>:
Examples, using {{Timc|x3}}<br />and {{Timc|t2}}:
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{a|b|c|d}}}</nowiki></ttsyntaxhighlight> gives <ttcode>{{{A|b|c|d}}}</ttcode>
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{a|{{x3|b}}}}}</nowiki></ttsyntaxhighlight> gives <ttcode>{{{A|{{x3|b}}bbb}}}</ttcode>
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{a|{{{b|c}}}}}}</nowiki></ttsyntaxhighlight> gives <ttcode>{{{A|{{{b|c}}}}}}</ttcode>
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{a|[[b|c]]}}}</nowiki></ttsyntaxhighlight> gives <ttcode>{{{A|[[b|c]]}}}</ttcode>
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{{{x3|a}}|b}}}</nowiki></ttsyntaxhighlight> gives <ttcode>{{{{{x3|a}}aaa|b}}}</ttcode> - parameter aaa is undefined
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{{{{a|b}}}|c}}}</nowiki></ttsyntaxhighlight> gives <ttcode>{{{{{{A|b}}}|c}}}</ttcode> - parameter b is undefined
 
*<tt><nowikisyntaxhighlight lang="wikitext" inline>{{{a|<nowiki>b|c</nowiki><nowiki><</nowiki>/nowiki>}}}</ttsyntaxhighlight> gives <ttcode>{{{A|<nowiki>b|c</nowiki>}}}</ttcode> - 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):
*:<tt><nowikisyntaxhighlight lang="wikitext" inline>{{t2|{{{a|<nowiki>b|c</nowiki><nowiki><</nowiki>/nowiki>}}}}}</ttsyntaxhighlight> gives <ttcode>{{T2start-b|c-middle-{{{A|<nowiki>b|c</nowiki>}}2}}}-end</ttcode>
 
*<syntaxhighlight lang="wikitext" inline>{{{a|b{{!}}c}}}</syntaxhighlight> gives <code>{{{a|b{{!}}c}}}</code>, and the bar will be interpreted by the next layer of templates.
 
==Expressions containing a parameter, with default==
Line 59 ⟶ 61:
For comparison using a="if" and c=d="", and also the shorter class name "if" instead of "HiddenStructure", the two lines are:
 
<syntaxhighlight lang="wikitext">
<nowiki><span class=" if{{{b|}}} "> ... </span></nowiki>
<nowiki>{{{ span class=" if{{{b|}}} | "> ... }}}</nowikispan>
{{{ if{{{b|}}} | ... }}}
 
</syntaxhighlight>
In the second method the wikitext in the template is 15 characters shorter for each optional item, but each call is 4 characters ("if=|") longer.
 
Line 71 ⟶ 74:
{{Tim|fors}}, containing:
----
<presyntaxhighlight lang="wikitext">{{fors/aux
|v@=
|c={{{call}}}
Line 81 ⟶ 84:
|pc4={{{pc4|=}}}|
1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}}
}}</presyntaxhighlight>
----
with {{Tim|fors/aux}}, containing:
----
<presyntaxhighlight lang="wikitext">{{{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}}}}}}}}</presyntaxhighlight>
----
 
In short form the latter consists of components
 
<codesyntaxhighlight lang="wikitext">[ v[''i''] | [s] {{[c]|[pc1]|[pc2]|[pc3]|[pc4]|[pv]=[''i'']}} ]</codesyntaxhighlight> (''i'' = 1, 2, 3, for ''i'' = 1 without [s])
 
or in terms of the parameters of the first template:
 
<codesyntaxhighlight lang="wikitext">[v[''i''|@] | [sep|] {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=[''i'']}} ]</codesyntaxhighlight>
 
This is indeed of the above-mentioned form <code>[a[b|c]|f([b])]</code>, with {{mono|1=a=v, b=''i'', c=@}}, and
 
<codesyntaxhighlight lang="wikitext">f(x) = [sep|] {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=x}}</codesyntaxhighlight>
 
The assumptions mentioned above apply for d equal to null, and provided that no [''i''] is equal to "@".
 
===Variations===
Since {{code|1=v@=null}} we can also take make the concatenation the outer operation:
<codesyntaxhighlight lang="wikitext">[v[''i''|@] | [sep|]] [v[''i''|@] | {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=[''i'']}} ]</codesyntaxhighlight>
 
==Conditional statement==
{{Tim|if}} contains:
----
<presyntaxhighlight lang="wikitext">{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}</presyntaxhighlight>
----
 
 
or in short form:
 
<syntaxhighlight lang="text">[ else[test|] | [ test[test|] | [then|] ] ]</syntaxhighlight>
 
If [<code>{{bracket|test]}}</code> is defined this reduces to
 
<syntaxhighlight lang="text">[ else[test] | [ test[test] | [then|] ] ]</syntaxhighlight>
 
For test equal to null this reduces to [else<code>{{bracket|]else{{!}}}}</code>; otherwise, if no parameter name starting with "test" or "else" applies except these themselves, then this reduces to [then<code>{{bracket|]then{{!}}}}</code>.
 
If [<code>{{bracket|test]}}</code> is undefined we get
 
<syntaxhighlight lang="text">[ else | [then|] ]</syntaxhighlight>
 
==Last-but technique==
Line 143 ⟶ 146:
</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 <nowiki>{{Tim|lastbut0}} and {{Tim|lastbut1}}</nowiki>:
*<code><nowiki>{{lastbut0}}</nowiki></code> gives {{lastbut0}}no input.
*<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=}}no input.
*<code><nowiki>{{lastbut1|ifu=|a}}</nowiki></code> gives {{lastbut1|ifu=|a}}no input.
*<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 {{TimcnlTim|max}}
 
==Server efficiency==
Line 160 ⟶ 163:
==See also==
*{{Tim|T opt par}} - examples
 
==References==
{{Reflist}}