Help:Advanced templates
From WikiNASIOC
| MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/- |
This page is an extension of Help:Template. It concerns template names, variable names, and parameters depending on a variable or parameter. Note: This document refers to features currently only available in the Mediawiki software starting with version 1.6 and later.
Some described applications of these techniques may be outdated; for example, if-statements in the extension ParserFunctions are often more convenient. Note that Help:Parser functions is a different page. See also other "Advanced functioning" help pages listed below.
A template name or a parameter value depending on a parameter or variable
A template name and a parameter value can both depend on a parameter or variable:
-
{{tt|t|efg}}using Template:Tt containing "{{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}}" and template:T ( talk edit history links ), produces "start-aefgbefgcefgd-end" -
{{ {{NAMESPACE}} }}, using Template:Help containing "demonstration<noinclude>[[Category:Test pages]]</noinclude>", gives "demonstration" (spaces needed) - see Template:Histogram
This allows branching depending on variables and parameters. However, using parser functions is more versatile.
Since with the method of this section no conditions such as {{{1}}}<a can be evaluated, and no substrings can be extracted from strings (not even currently with parser functions), etc., this works best if each parameter and variable has only a limited number of possible values. For example {{CURRENTDAYNAME}} has only seven possible values, so all cases can be handled with seven templates; if there are really only two cases, five of the seven could be redirects. On the other hand , {{CURRENTTIME}} has 1440 possible values, so it would be impractical to create a template or even just a redirect for each. Even with parser functions all the 1440 values have to be listed, see template:hour ( talk edit history links ) and template:minute ( talk edit history links ). An older method described below is another possibility, see e.g. template:timedepcolor ( talk edit history links ).
Having multiple parameters, which each can have only few values, is more flexible than having the same total number of possibilities in a single parameter.
If a template depends on PAGENAME, then the many possible values need not be a problem: only the pagenames of the pages that call the template apply, and these can be limited.
Branching according to {{{1}}}=a or {{{1}}}≠a is possible, see the section #A parameter name depending on a parameter.
Examples of pages that call a template with a name depending on a variable
- NAMESPACE
- Template:H:h and Template:H:f (see edit pages, MediaWiki help policy, and list)
- Template:Nsn - namespace number (see also below)
- Template:Gns - generic namespace name, useful for linking to the corresponding page on another project
- de:MediaWiki:Newarticletext and de:MediaWiki:Noarticletext (see edit pages and list)
- PAGENAME
- w:en:Template:H:f - for project-specific content on all help pages of the English Wikipedia (requires a blank template on all pages that do not have this content)
A variable name depending on a parameter
Example:
{{namespace|4}}, using Template:Namespace, containing
- namespace number:{{{1}}} - name: {{ns:{{{1}}}}}
gives:
- namespace number:4 - name: WikiNASIOC
A parameter name depending on a parameter
A parameter name in a template call (before "=") can depend on a parameter.
For example, using Template:t pnd containing "{{t p|{{{1}}}=abc}}" with Template:t p containing "{{{capital}}} is the capital of {{{country}}}.":
*{{t pnd|capital}}
*{{t pnd|country}}
*{{t pnd|something else}}
gives
- abc is the capital of {{{country}}}.
- {{{capital}}} is the capital of abc.
- {{{capital}}} is the capital of {{{country}}}.
This applies also for integer parameter names which normally do not require "=", but are now referred to by an expression on the left of "=".
Furthermore, multiple assignments of values to the same parameter in the same template call are allowed and result in the last value being used.
For example, {{t p|country=Belgium|capital=Paris|country=France}} gives "Paris is the capital of France.".
- The following is somewhat obsolete on wikis with the ParserFunctions extension, which allows easier branching.
This can be combined into the following branching technique:
{{tts|first parameter, with fixed name = name of template to be called if the parameter names are not equal | second parameter, with the name depending on parameters and/or variables = name of the template to be called if the parameter names are equal }} with Template:Tts containing {{{{{name of first parameter }}}}} (in this case that name is the empty string).
Thus, while carrying out an action in the case of equality can also be done simply by using a template name equal to the constant (the fixed name above), this technique also allows an action in the case of inequality, without having to cover all alternative values (different from the constant) separately.
Example:
Branching depending on whether a parameter is empty is illustrated with Template:T pdc containing "{{tts|=do if parameter is not empty|{{{1}}}=do if parameter is empty}}".
- {{t pdc|abc}} gives Template:Do if parameter is not empty
- {{t pdc|}} gives Template:Do if parameter is empty
- {{t pdc}} gives Template:Do if parameter is not empty
Example with an extra parameter:
Consider Template:ine containing "{{call|={{{1}}}|{{{2}}}=void|1={{{2}}}|2={{{3}}}|3={{{4}}}}}", which calls Template:call containing "{{{{{}}}|{{{1}}}|{{{2}}}|{{{3}}}}}": If the first parameter is Not Empty then do the task given by the first parameter with the second, third and fourth parameter as its parameters, else do nothing; if the number of parameters of the task is 0, 1, or 2, ignore the superfluous values.
Using Template:Death containing "Date of death: {{{1}}}, place of death: {{{2}}}":
- {{ine|death|[[November 2]], [[2004]]|[[Amsterdam]]|}} gives:
"Date of death: November 2, 2004, place of death: Amsterdam".
- {{ine|death||comment|}} gives "".
This can be useful inside another template, to avoid the texts "Date of death:" and "place of death:" for a living person.
Also, without a separate template Death, with just the more general Template:p2p1p3 containing "{{{2}}}{{{1}}}{{{3}}}":
- {{ine|p2p1p3|[[November 2]], [[2004]]|Date of death: |, place of death: [[Amsterdam]]}} gives:
"Date of death: November 2, 2004, place of death: Amsterdam".
This would not be convenient to use in many template tags, but could be used in an infobox in the form
- {{ine|p2p1p3|{{{1}}}|Date of death: |, place of death: {{{2}}}}}
(Alternatively a separate parameter "dead" with one of the values "dead" and "alive" is used, see e.g. w:en:Template:Infobox President with the auxiliary templates w:en:Template:Infobox President/dead and the empty w:en:Template:Infobox President/alive. A disadvantage is the extra parameter: it has to be specified that a president is alive, it is not sufficient to leave the date of death empty, or that a president is dead, even if a date of death has been specified. An advantage is that backlinks are available, providing not only a list of dead, but also a list of alive presidents for which the infobox is used.)
Similarly, where a table row dealing with a parameter is removed if the parameter is empty:
w:en:Template:If defined call1 calls w:en:Template:Template_call1 with 1={{{2}}} followed by 1{{{1}}}=void; if {{{1}}} is empty then this overwrites the value of 1, so 1=void, otherwise 1={{{2}}}, giving an empty result or {{{{{2}}}|{{{3}}}}}. {{{3}}} is typically the same as {{{1}}} (at least that is the case in all five cases in w:en:Template:Infobox CVG). It avoids e.g. the text "Designer:" if the parameter {{{designer}}} is empty.
More generally, using Template:If equal g containing "{{call{{{3}}}|{{{3}}}={{{2}}}|{{{4}}}={{{1}}}|1={{{5}}}|2={{{6}}}|3={{{7}}}}}":
- {{if equal g|2x|3x|{{NAMESPACE}}|Help|a||}} gives aa
- {{if equal g|2x|3x|Help|{{NAMESPACE}}|a||}} gives aa
- {{if equal g|2x|3x|{{NAMESPACE}}|Template_talk|a||}} gives aaa
- {{if equal g|2x|3x|Template_talk|{{NAMESPACE}}|a||}} gives aaa
Note that {{NAMESPACE}} produces underscores for spaces, which are not considered equal by the template:
- {{if equal g|2x|3x|Template_talk|Template talk|a||}} gives aaa
- {{if equal g|2x|3x|Template_talk|Template_talk|a||}} gives aa
A parameter name in a parameter tag can now depend on a parameter, see Template:T pnd1 and its talk page.
Also a parameter name in a template call can now depend on a variable: {{ns:2}} gives User and {{PUser|User=abc}} gives abc, hence {{PUser|{{ns:2}}=abc}} gives abc
Using "subst:" with manual recursion gives a stepwise replacement, useful for analyzing and explaining the working of templates calling other templates. See e.g. Template talk:Lop.
Including the content of a template in a template or variable name or parameter value
It is possible to use a template with a name dependent on the content of another template. For example: {{{{tctc}} }}, using Template:Tctc containing "tc" and Template:Tc containing "in<noinclude>{{documentation}}</noinclude>", gives "in". If there are no parameters, a space is needed between e.g. the two pairs of closing braces.
It is possible (since about 19 October 2005) to use a variable with a name dependent on the content of a template.
- For example: {{CURRENT{{t day}}}} using Template:T day containing "
DAY" gives the text 28, which is indeed the value of {{CURRENTDAY}}
It is possible (since about 19 October 2005) to include the content of another template in a parameter value
Examples:
- {{3x|{{tc}}}}, using Template:3x, gives ininin.
- {{3x|{{5x|{{tc}}}}}} gives ininininininininininininininin.
Thus it is also possible to use a data array as parameter of a template, e.g.:
{{hist5a|x|t data}} using
Template:hist5a (talk, backlinks, edit) containing:
{{Xx|{{{{{2}}}|1}}|{{{{{2}}}|2}}|{{{1}}}}}
{{Xx|{{{{{2}}}|3}}|{{{{{2}}}|4}}|{{{1}}}}}
{{Xx|{{{{{2}}}|5}}|{{{{{2}}}|6}}|{{{1}}}}}
{{Xx|{{{{{2}}}|7}}|{{{{{2}}}|8}}|{{{1}}}}}
{{Xx|{{{{{2}}}|9}}|{{{{{2}}}|10}}|{{{1}}}}}
and
Template:t data containing "{{p{{{1}}}|3|2|7|4|3|8|4|4|6|5}}" gives:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Producing the value of a parameter whose name depends on the value of another parameter
A parameter value can be used as the name of another parameter, even of the same template. For example,
{{tvv | parameter=foo | foo=bar}}
using Template:tvv containing "{{{{{{parameter}}}}}}" gives bar.
For different names the order of the parameters does not matter:
{{tvv | foo=bar | parameter=foo }}
still gives bar.
{{tvv|parameter={{{a|foo}}}|foo=bar}} gives bar
However, with substitution it gives the wikitext {{{{{{a|foo}}}}}} rendered as {{{foo}}} .
Also, the technique cannot be applied multiple times. For example,
{{tvvv | 1=foo | foo=bar | bar=biz}}
using Template:tvvv containing "{{{{{{{{{1}}}}}}}}}" gives {{{bar}}}.
This is e.g. applied in en:template:Reg polyhedra db ( talk edit history links ), 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. en:template:Reg polyhedron stat table ( talk edit history links ), 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}}}
The technique can also be used to create control flow structures. An ifdef-function as in template:ifdef ( talk edit history links ) could look like:
{{{test{{{test|}}}|{{{then}}}}}}
If test is empty, we expand the empty test parameter, but if it contains data, then after trying to expand a non-existing testdata-parameter, we get the then-parameter value instead. Or a literal {{{then}}}, if there's intentionaly no default value for then, and then is undefined - a crude mechanism to catch errors.
Of course this fails for a say test value it if testit is defined. An example with numerical parameters:
{{{1{{{1|}}}|okay}}}
For a defined first parameter we expect to get the text okay. But if the value of the first parameter is say 0, and a tenth parameter exists, we get its value {{{10}}} instead of okay.
It's slightly different for an ifndef-function as in template:ifndef ( talk edit history links ):
{{{then{{{test|}}}|}}}
If test is empty, then we expand the then parameter. But if test contains data, then we try to expand a nonexisting thendata-parameter, finally arriving at its empty default value (= after the second "|" pipe symbol).
Again this fails if the non-existing parameter in fact does exist, e.g. for
{{{x{{{1|}}}|}}}
with a defined value y for x we expect to get the empty default for a non-existing xy. This fails if {{{xy}}} is defined and non-empty.
An if-then-else could be a combination of those two, as in Template:ifold containing "{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}<noinclude>[[Category:Miscellaneous templates|{{PAGENAME}}]]</noinclude>".
For simple ifdef-cases the best solution is arguably to use it without additional template, e.g. for text relevant only if the third positional parameter is defined use:
{{{3{{{3|}}}|found third parameter}}}
For the opposite ifndef-case there's unfortunately no similar direct approach. Substitution works for ifdef, but not ifndef or ifold.
A parameter value can also be used as part of the name of another parameter:
{{switch
|{{CURRENTDOW}}
|case: 1=Mon
|case: 2=Tue
|case: 3=Wed
|case: 4=Thu
|case: 5=Fri
|case: 6=Sat
|case: 0=Sun
|default=No date
}}
using Template:switch containing "{{{case: {{{1|}}}|{{{default|}}}}}}<noinclude>[[Category:Templates|Switch]]</noinclude>" gives Thu
Using this technique and an auxiliary template, we can produce a function that checks if two parameter values are the same:
Template:equal containing "{{vv@|{{{1}}}=1|@={{{2}}}}}", with Template:vv2 containing "{{{{{{@}}}|}}}".
It will return 1 if equal, or null (the empty string) if not.
See also
- ParserFunctions (Syntax for #if:, #expr: etc.)
- Meta_talk:Templates_for_translating_language_names
- Help:Array
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)
+/-

