This page covers advanced template techniques, particularly, the use of variable template names, and parameter names in templates. Readers, should first be thoroughly familiar with the standard template techniques, found in Help:Template. Some of the techniques described below, may be outdated or of limited use; for example, ParserFunctions and Lua, may be more convenient alternatives to using some of the branching techniques discussed.
In general, these techniques rely on the recursive (inside-out) processing of templates. When a template is expanded (processed), it translates the given template code into string values. These are often sent directly to the web browser for display, however, they can be treated as code themselves. For instance, by enclosing the text strings in double-curly-brackets ( {{}} ), they can be reprocessed as template names or variable names; thus, producing different results as the string values are changed.
Notes:
{{{{tctc}} }}
(discussed below) with {{{{subst:tctc}} }}
, on a sandbox page. Although both produce the same visible result, the first will remain as the full code {{{{tctc}} }}
, while the second will replace the inner template {{tctc}}
with its value, leaving the code {{tc }}
.In some cases, it may be useful for a template to call different templates depending on a variable or a passed parameter. This allows a type of branching that can be simpler than with ParserFunctions, though they are generally less versatile.
Examples:
{{ {{NAMESPACE}}}}
{{ {{tctc}}}}
The extra spaces in the above examples are needed: without them, the pair of inner three braces is taken as those for a parameter. On the page itself it just shows as plain text: {{{{Manual}}}}.
{{{{{2}}}x|{{{1}}}}}
{{2x|{{{1}}}}}
would produce {{{1}}}{{{1}}}
, {{3x|{{{1}}}}}
would produce {{{1}}}{{{1}}}{{{1}}}
, and etc. Template:Hist5 uses this technique repeatedly in {{hist5|X|4|7|3|2|9}} to produce the following 5 line histogram:
XXXX
XXXXXXX
XXX
XXXXXXXXXXX
Using parser functions and templates, including those which process strings (see Manual:Performing string operations with parser functions and Category:String manipulation templates), a template name can also depend in a more complicated way on parameters and/or variables.
Templates passed as parameters[edit]Templates can be passed as parameters to other templates. This can mean either that the template is evaluated and the result is passed as a parameter or that the template name is passed and evaluated as part of the other template.
{{tto|t|V}}
{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}
- takes the value V (passed as the second parameter) and produces aVbVcVd. This value is then passed to template:t (which was passed by name as the first parameter), producing "TaVbVcVd".
In interpreting this sequence {{tto|t|V}}
, the parser:
{{tto|t|V}}
is formally a valid template call, with:
{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}
{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}
formally contains a valid template call, with:
start-{{{1}}}-end
A parameter name in a template can be the value of another parameter. This is useful if you want the behavior of a template to change based on the information that is provided to it.
Choosing parameters contextually - {{t pnd|parameter name}}
- Template:t p contains
{{{capital}}} is the capital of {{{country}}}
, with two parameters - "capital" and "country".- Template:t pnd containing "
{{t p|{{{1}}}=abc}}<noinclude>{{doc|content=This template is used as an example at [[Manual:Advanced templates]][[Category:Demo templates]]}}</noinclude>
" can be used to select which parameter is used in a particular case.Thus:
This... Produces this {{t pnd|capital}} abc is the capital of {{{country}}}. {{t pnd|country}} {{{capital}}} is the capital of abc. {{t pnd|something else}} {{{capital}}} is the capital of {{{country}}}.This applies to integer parameters as well, since integer parameters can be specified using the "#=" notation. Passing the integer value N to {{T pnd }} as its parameter will make it look for the Nth unnamed parameter.
This is e.g. applied in w:Template:Reg polyhedra db, which contains a 2D array in the form {{{{{1}}}|{{{2}}}|1-1=a11|..|m-n=amn}}. The first parameter is the name of a template that provides a particular selection and presentation of a selected row of the array, e.g. w:Template:Reg polyhedron stat table, the second parameter (which is the first parameter of the latter template) specifies the row. The latter templates references element j of the row concerned by a tag of the form {{{{{{1}}}-j}}}
Whilst the same output could also be produced using {#switch:}, this method is less intensive on the server and may help to stay under page limits; see Help:Array.
{{ppp|p=foo|foo=bar}}
A parameter name is a wikitext that is expanded before being used as name. For instance, using Template:ppp, which contains {{{{{{p}}}}}}
(six braces, the parameter name is itself a parameter), the code {{ppp|p=foo|foo=bar}}
:
{{{...}}}
= a variable name.
{{{p}}}
.{{{foo}}}
The result of the template call is therefore bar.
The name expansion is recursive, so the technique can be applied multiple times—e.g., using Template:tvvv, which contains {{{{{{{{{p}}}}}}}}}
, {{tvvv|p=foo|foo=bar|bar=biz}}
gives :
Result is biz.
And so on, using Template:tvvvv, which contains {{{{{{{{{{{{p}}}}}}}}}}}}
(forth level indirection, twelve braces), {{tvvvv|p=alpha|alpha=beta|beta=gamma|gamma=delta}}
gives delta (whatever use that may have).
The parameter default feature was introduced before Extension:ParserFunctions. This led to the development of branching methods through the parameter default mechanism.
{{{test{{{test|}}}|{{{then}}}}}}
There was also an array technique using parameter defaults, with the disadvantage that a template using this technique had to be called with, in addition to the normal parameters, a standard parameter definition not reflecting a choice, but necessary to make the template work.
An even older branching technique dates from before the introduction of the parameter default mechanism. It is based on the fact that if in a template call a parameter is assigned a value more than once, the last one counts. This is used in combination with specifying the value of a parameter in a template call, where the name of that parameter depends on a parameter of an outer template. In a call {{a|b=c|{{{d}}}=e}}
, template:a uses b=c if b≠{{{d}}} and b=e if b={{{d}}}.
Another old "branching technique" is using a template name depending on the value of a parameter (see above).
Variable variable names[edit]{{t curr}}
{{CURRENT{{{1|DAY}}} }}<noinclude>{{doc|content=This template is used as an example at [[Manual:Advanced templates]][[Category:Demo templates]]}}</noinclude>
" gives the text 4 without parameters, since it defaults to {{CURRENTDAY}}, but {{t curr|DAYNAME}} gives Friday while {{t curr|MONTHNAME}} gives July. Any magic word that begins with "CURRENT" can be accessed this way.
{{ns:{{{1}}}}}
namespace number:{{{1}}} - name: {{ns:{{{1}}}}}<noinclude>{{doc|content=This template is used as an example at [[Manual:Advanced templates]][[Category:Demo templates]]}}</noinclude>
", {{namespace|4}}
gives "namespace number:4 - name: Project", because Meta is the name of namespace 4.
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:
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)p
is undefined, {{{p|q}}}
is changed to q
, possibly with simultaneous substitutions inside the wikitext for q
.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 templates]]</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}}}"
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 m:Template:lastbut0 and m: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)A "for-loop" is achieved using
m: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 m: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 "@"
.
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]}} ]
#if:, #expr:
, etc.)RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4