Help:Parameter default

From Meta, a Wikimedia project coordination wiki

In non-substituted template expansion, an expression {{{p|q}}} inside the template is expanded to {{{p}}} if that is defined, and else to the default argument q. Thus the default can be specified independently for every occurrence of the parameter.

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.

Notes[edit]

In the case of multiple pipes, anything from the second pipe is ignored: {{{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:!.

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

Examples, in some cases 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 - second pipe onward is ignored
  • {{{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}}}"

Repetition[edit]

A "for-loop" is achieved using

Template:Fors, 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 Template:Fors/aux, 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[edit]

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]}} ]

Last-but technique[edit]

Sometimes the last or last but x 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:
[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 D4, D3, D2, D1. And Dx=[if[x|u]|x-1]. Like this, you can get "last but x" parameter value.
Examples using Template:Lastbut0 and Template:Lastbut1:

  • {{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}}}}}


History[edit]

Parameter defaults were introduced before parser functions, and were therefore originally used as a way of creating conditional statements. This was superseded by the #if parser function, and later by Scribunto.


Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences
Editing
Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors
Other languages: