Help:Parameter default
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/- |
To meet Wikimedia's quality standards and make it more accessible, this article or section needs a better explanation of technical details or more context regarding applications or importance to make it more accessible to a general audience, or at least to technical readers outside this specialty. Please help expand it without removing the technical details. Additional rationale may be on this talk page. |
In non-substituted template expansion, an expression {{{p|q}}} inside the template is expanded to {{{p}}} if that is defined, and else to q.
These results {{{p}}} and q 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 substitution of a template, an expression {{{p|q}}} 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 p is defined, {{{p|q}}} 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 p is undefined, {{{p|q}}} is changed to q, possibly with simultaneous substitutions inside the wikitext for q.
Contents |
Notes
In the case of multiple default parts, only the first applies: {{{a|b|c|d}}} is equivalent with {{{a|b}}}. The default part can only contain "|" 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 template:! ( talk edit history links ).
The expression for the parameter name can also contain "|", as part of full template, parser function, or parameter syntax within it.
Examples, using Template:3x containing "{{{1}}}{{{1}}}{{{1}}}<noinclude>{{documentation}}</noinclude>
"
and Template:t2 containing "parameter 1 is "{{{1}}}", parameter 2 is "{{{2}}}"<noinclude>[[Category:Demo template]]</noinclude>
":
- {{{a|b|c|d}}} gives b
- {{{a|{{3x|b}}}}} gives bbb
- {{{a|{{{b|c}}}}}} gives c
- {{{a|[[b|c]]}}} gives c
- {{{{{3x|a}}|b}}} gives b - parameter aaa is undefined
- {{{{{{a|b}}}|c}}} gives c - parameter b is undefined
- {{{a|<nowiki>b|c</nowiki>}}} gives b|c - 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):
- {{t2|{{{a|<nowiki>b|c</nowiki>}}}}} gives parameter 1 is "b|c", parameter 2 is "{{{2}}}"
Expressions containing a parameter, with default
- The following was written before there were ParserFunctions, to allow branching without them.
- Notation: from here the wikitext {{{a}}} is written as [a].
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)
Then the expression gives f([b]) if [b] is defined, and otherwise d.
Note that the conditions require that no applicable [b] has value c. If one wants to be able to use all letters and digits in [b], and also allow null (the empty string), then for c one can take e.g. "@".
If it is desirable or at least acceptable that if [b] is empty, the result is not f("") but "", and the same when [b] is undefined, we take d="", and can for example choose c to be the empty string too. Then we have the expression [a[b|]|f([b])] occurring in the template, giving f([b]) if [b] is defined, and otherwise the empty string, under the following conditions:
- [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 same template can contain several expressions [a_{i} [b_{i} | c ] | f_{i} ([b_{i} ])]. By choosing all a_{i} equal we need only one "a=" in the template call. Then the expressions are [a [b_{i} | c ] | f_{i} ([b_{i} ])].
A special case is with constant functions f_{i}, i.e. not dependent on [b_{i} ]. Then the expressions are [a [b_{i} | c ] | f_{i} ]. In the template call it only matters for each b_{i} 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_{i} the array value is f_{i}. It is called with "ac=" and "b_{i}=", or even with a list of assignments "b_{i}=", giving a list of the corresponding array values. See template:Short DOW alt 2 ( talk edit history links ).
While normally a parameter specification in a template call represents a choice the user of the template has, the need to specify "ac=" 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 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 good choice is "if". Thus
- [if [b_{i} | c ] | f_{i} ([b_{i} ])]
means
- if b_{i} is defined then f_{i} ([b_{i} ])
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_{i} ("b_{i}=") 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:
<span class=" if{{{b|}}} "> ... </span> {{{ if{{{b|}}} | ... }}}
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
template:fors ( talk edit history links ), containing:
{{fors/aux |[email protected]= |c={{{call}}} |pv={{{pv|1}}} |s={{{sep|}}} |pc1={{{pc1|=}}} |pc2={{{pc2|=}}} |pc3={{{pc3|=}}} |pc4={{{pc4|=}}}| 1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}} }}
with template:fors/aux ( talk edit history links ), containing:
{{{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}}}}}}}}
In short form the latter consists of components
[ v[i] | [s] {{[c]|[pc1]|[pc2]|[pc3]|[pc4]|[pv]=[i]}} ]
(i = 1, 2, 3, for i = 1 without [s])
or in terms of the parameters of the first template:
[v[i|@] | [sep|] {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=[i]}} ]
This is indeed of the above-mentioned form [a[b|c]|f([b])], with a=v, b=i, [email protected], and
f(x) = [sep|] {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=x}}
The assumptions mentioned above apply for d equal to null, and provided that no [i] is equal to "@".
Variations
Since [email protected]=null we can also take make the concatenation the outer operation:
[v[i|@] | [sep|]] [v[i|@] | {{[call]|[pc1|]|[pc2|]|[pc3|]|[pc4|]|[pv|1]=[i]}} ]
Conditional statement
template:ifold ( talk edit history links ) contains:
{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}
or in short form:
[ else[test|] | [ test[test|] | [then|] ] ]
If [test] is defined this reduces to
[ else[test] | [ test[test] | [then|] ] ]
For test equal to null this reduces to [else|]; otherwise, if no parameter name starting with "test" or "else" applies except these themselves, then this reduces to [then|].
If [test] is undefined we get
[ else | [then|] ]
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:
[4|[3|[2|[1|*]]]]
.
Then last but one can be achieved like this: ("ifu" is preassigned with empty)
[[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 "*". ] ] ]
I.e., to replace "4", "3", "2", "1" with D_{4}, D_{3}, D_{2}, D_{1}. And D_{x}=[if[x|u]|x-1]. Like this, you can get "last but x" parameter value.
Examples using template:lastbut0 ( talk edit history links ) and template:lastbut1 ( talk edit history links ):
{{lastbut0}}
gives no input.{{lastbut0|a}}
gives a.{{lastbut0|a|b|c|d|e|f|g|h|i|j}}
gives j.{{lastbut1|ifu=}}
gives no input.{{lastbut1|ifu=|a}}
gives no input.{{lastbut1|ifu=|a|b}}
gives a.{{lastbut1|ifu=|a|b|c|d|e|f|g|h|i|j}}
gives i.{{lastbut0|53=53|81=81|28=28}}
gives 81 (finds maximum of a list of integers in the range 1 - 100)
See also Template:max (talk, backlinks, edit) containing:
{{lastbut0|{{{1}}}={{{1}}}|{{{2}}}={{{2}}}}}
Server efficiency
Parameter default constructs are said to be more efficiently executed by the server than similar constructs using extra layers of templates.
Versions
Information on this page relates to MediaWiki versions 1.6 and later.
History
Parameter defaults were introduced before parser functions, and therefore used ingeniously for branching, see above and e.g. template:T opt par ( talk edit history links ).
See also
- [1] - deliberate use of the notation "{{{1}}}" (identical to the automatic result if no default is specified) as shorthand for "parameter 1 is undefined"
Links to other help pages
- Help contents | search (all help pages)
- Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s:
- Reading
- Go | Search | Stop words | URL | Namespace | Page name | Section
- Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
- Logging in and preferences
- Logging in | Preferences | User style
- Editing
- Advanced editing | Editing FAQ | Edit toolbar | Export
- Editing shortcuts
- Tracking changes
- Recent changes (enhanced) | Related changes | Watching pages | Diff
- Page history | Edit summary | User contributions | Minor edit | Patrolled edit
- Style & formatting
- Wikitext examples | Reference card
- HTML in wikitext | HTML elements
- List | Table | Sorting
- Mathematical symbols on English Wikipedia
- Special input and output
- Inputbox | Special characters | Displaying a formula | Images and other uploaded files | EasyTimeline
- Advanced functioning
- Template | Advanced templates | Parser function | ParserFunctions | Parameter default | Variable | Magic word | System message | Substitution
- Array | Calculation
- Page management
- Starting a new page | Renaming (moving) a page | Maintenance | Merging and moving pages | Protecting pages
- Resolving disputes | Deleting a page
- Special pages
- Talk page | Testing | Sandbox
- Lists of resources
- Lists of Categories | Copyrights | Infobox
- Redirect | Reference Desk | Shortcuts | Stub types
Other languages: English (en)
+/-
<math>Insert formula here</math>