Content deleted Content added
TRANSWIKI: Copied from Meta:Help:Parameter default. |
m →Expressions containing a parameter, with default: syntaxhighlight |
||
(40 intermediate revisions by 17 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}}
Notation: on this page the wikitext <code>{{{A}}}</code> is written as {{mono|[{{var|a}}]}}.
In a [[help:template|template]], [a|b] is equal to [a] if that is defined, and else equal to b.▼
▲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 {{ ==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
The expression for the parameter name can also contain
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>:
*<syntaxhighlight lang="wikitext" inline>{{{a|b|c|d}}}</syntaxhighlight> gives <code>{{{A|b|c|d}}}</code>
*<syntaxhighlight lang="wikitext" inline>{{{a|{{x3|b}}}}}</syntaxhighlight> gives <code>{{{A|bbb}}}</code>
*<
*<syntaxhighlight lang="wikitext" inline>{{{a|[[b|c]]}}}</syntaxhighlight> gives <code>{{{A|[[b|c]]}}}</code>
*<
*<
*<
▲The expression for the parameter name can also contain "|", as part of full template or parameter syntax within it.
*:<syntaxhighlight lang="wikitext" inline>{{t2|{{{a|<nowiki>b|c</nowiki>}}}}}</syntaxhighlight> gives <code>start-b|c-middle-{{{2}}}-end</code>
*<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.
Examples, using {{timc|x3}} and {{timc|t2}}:▼
*<nowiki>{{{a|b|c|d}}}</nowiki> gives {{{a|b|c|d}}} ▼
▲*<nowiki>{{{a|{{{b|c}}}}}}</nowiki> gives {{{a|{{{b|c}}}}}}
*<nowiki>{{{a|[[b|c]]}}}</nowiki> gives {{{a|[[b|c]]}}}▼
▲*<nowiki>{{{a|<nowiki>b|c</nowiki><nowiki><</nowiki>/nowiki>}}} gives {{{a|<nowiki>b|c</nowiki>}}} - 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):
▲*<nowiki>{{{{{x3|a}}|b}}}</nowiki> gives {{{{{x3|a}}|b}}} - parameter aaa is undefined
▲*<nowiki>{{{{{{a|b}}}|c}}}</nowiki> gives {{{{{{a|b}}}|c}}} - parameter b is undefined
==Expressions containing a parameter, with default==
Consider the expression [a[b|c]|f([b])] occurring in a template, where f([b]) denotes an expression in [b].
Conditions:
*[a[b]] is undefined for all applicable [b] (all values of parameter b for which the template is called)
*[ac]=d (the template is called with ac=d)
Line 33 ⟶ 38:
Then the expression gives f([b]) if [b] is defined, and otherwise d.
Note that the
If it is desirable or at least acceptable that if [b] is empty, the result is not f("") but "",
*[a[b]] is undefined for all applicable [b] (all values of parameter b for which the template is called)
*the template is called with "a="
*the template is called with "a=" (while normally a parameter specification in a template call represents a choice the user of the template has, the need to specify "a=" is an unfortunate technical requirement imposed on the user of the template; it can be shielded from the user by putting the template call inside another template; on projects where server-strain is a concern this may not be desirable). ▼
The same template can contain several expressions [a<sub>''i''</sub> [b<sub>''i''</sub> | c ] | f<sub>''i''</sub> ([b<sub>''i''</sub> ])]. By choosing all a<sub>''i''</sub> equal we need only one "a=" in the template call. Then the expressions are [a [b<sub>''i''</sub> | c ] | f<sub>''i''</sub> ([b<sub>''i''</sub> ])].
The name "a" can be chosen such that no other parameters of the template have a name starting with it; then the first condition is fulfilled (apart from the case, already discussed, that [b] is the empty string).▼
A special case is with constant functions f<sub>''i''</sub>, i.e. not dependent on [b<sub>''i''</sub> ]. Then the expressions are [a [b<sub>''i''</sub> | c ] | f<sub>''i''</sub> ]. In the template call it only matters for each b<sub>''i''</sub> whether is gets a value, not which value. For convenience we can assign the empty string to those which are defined at all. Since no applicable [b] should have value c (see above), c should not be the empty string in this case. With d the empty string, the template serves as an array, where for index b<sub>''i''</sub> the array value is f<sub>''i''</sub>. It is called with "ac=" and "b<sub>''i''</sub>=", or even with a list of assignments "b<sub>''i''</sub>=", giving a list of the corresponding array values. See {{Tim|Short DOW alt 2}}.
▲
▲The name "a" can be chosen such that no other parameters of the template have a name starting with it
:[if [b<sub>''i''</sub> | c ] | f<sub>''i''</sub> ([b<sub>''i''</sub> ])]
means
:if b<sub>''i''</sub> is defined then f<sub>''i''</sub> ([b<sub>''i''</sub> ])
c="" gives the if-statement which is shortest and with the best appearance, while e.g. c="u" (for "undefined") allows for the shortest specification in the template call to define b<sub>''i''</sub> ("b<sub>''i''</sub>=") in cases where the value is irrelevant. The latter may be confusing in contexts where "empty" and "undefined" are usually treated as equivalent.
If the result, f([b]) or the empty string, is for final display only, i.e., not for use in expressions for template names, parameter names, parameter values, page names in links, etc., an alternative is using CSS, see [[MediaWiki talk:Common.css]].
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">
<span class=" if{{{b|}}} "> ... </span>
{{{ 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.
In the first method conflicts with other class names have to be avoided, in the second method conflicts with other parameter names.
==Repetition==
A "for-loop" is achieved using
{{
----
<
|v@=
|c={{{call}}}
Line 58 ⟶ 84:
|pc4={{{pc4|=}}}|
1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}}
}}</
----
with {{ ----
<
{{{v{{{2}}}|{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{2}}}}}}}}
{{{v{{{3}}}|{{{s}}}{{{{{c}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}|{{{pv}}}={{{3}}}}}}}}</
----
In short form the latter consists of components
<
or in terms of the parameters of the first template:
<
This is indeed of the above-mentioned form <code>[a[b|c]|f([b])]</code>, with {{mono|1=a=v, b=''i'', c=@}}, and
<
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:
<
==Conditional statement==
{{
----
<
----
or in short form:
<syntaxhighlight lang="text">[ else[test|] | [ test[test|] | [then|] ] ]</syntaxhighlight>
If
<syntaxhighlight lang="text">[ else[test] | [ test[test] | [then|] ] ]</syntaxhighlight>
For test equal to null this reduces to
If
<syntaxhighlight lang="text">[ else | [then|] ]</syntaxhighlight>
==Last-but technique==
Somewhen maybe the last or last but ''x'' parameter value is needed, but number of inputted parameter 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: ("ifu" 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>
*<code><nowiki>{{lastbut0}}</nowiki></code> gives no input.
*<code><nowiki>{{lastbut0|a}}</nowiki></code> gives a.
*<code><nowiki>{{lastbut1|ifu=|a}}</nowiki></code> gives no input.
*<code><nowiki>{{lastbut1|ifu=|a|b}}</nowiki></code> gives a.
*<code><nowiki>{{lastbut1|ifu=|a|b|c|d|e|f|g|h|i|j}}</nowiki></code> gives i.
*<code><nowiki>{{lastbut0|53=53|81=81|28=28}}</nowiki></code> gives 81 (finds maximum of a list of integers in the range 1 - 100)
See also {{Tim|max}}
==Server efficiency==
Line 109 ⟶ 162:
==See also==
*{{
|