Help:Parameter default
Notation: on this page the wikitext {{{a}}} is written as [a].
In a template, [a|b] is equal to [a] if that is defined, and else equal to b.
These results [a] and 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 Template:x0 containing "<noinclude>{{documentation}}</noinclude>
" can be called as {{x0|[a]}} even if [a] is undefined, see e.g. m:Template:foreach/pass1 (backlinks edit).
Limitation
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, parameter or link syntax within it, and within nowiki-tags. The expression for the parameter name can also contain "|", as part of full template or parameter syntax within it.
Examples, using Template:x3 containing "<noinclude>{{Please leave this line alone (template sandbox heading)}}<!--
* Welcome to the template sandbox! *
* Please leave this part alone *
* The page is cleared regularly *
* Feel free to try your editing skills below *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■--></noinclude>
" and Template:t2 containing "[[w:List of people by name: {{{1}}}{{{2}}}|{{{1}}}{{{2}}}]]<noinclude>[[Category:Help:Templates templates]]</noinclude>
":
- {{{a|b|c|d}}} gives b
- {{{a|{{x3|b}}}}} gives
- {{{a|{{{b|c}}}}}} gives c
- {{{a|[[b|c]]}}} gives c
- {{{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 [[w:List of people by name: b|c{{{2}}}|b|c{{{2}}}]]
- {{{{{x3|a}}|b}}} gives b - parameter aaa is undefined
- {{{{{{a|b}}}|c}}} gives 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].
Assume:
- [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 assumptions 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. "@".
Repetition
A "for-loop" is achieved using
m:Template:fors (backlinks edit), containing:
{{fors/aux |v@= |c={{{call}}} |pv={{{pv|1}}} |s={{{sep|}}} |pc1={{{pc1|=}}} |pc2={{{pc2|=}}} |pc3={{{pc3|=}}} |pc4={{{pc4|=}}}| 1={{{1|@}}}|2={{{2|@}}}|3={{{3|@}}} }}
with m:Template:fors/aux (backlinks edit), 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, c=@, 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 v@=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
m:Template:if (backlinks edit) 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|] ]
Server efficiency
Parameter default constructs are said to be more efficiently executed by the server than similar constructs using extra layers of templates.