Help:Advanced templates

This article covers advanced template techniques, in particular the use of variable template names and parameter names in templates. Readers should be thoroughly familiar with the standard template techniques found in Help:Template. Some techniques described below may be outdated or of limited use; for example, may be more convenient than 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 template code into string values. These are often sent directly to the web browser for display, but they can be treated as code themselves. By enclosing the text strings in double-curly-brackets ( –  ), for instance, they can be reprocessed as template names or variable names, producing different results as the string values are changed.

Notes:


 * This document refers to features currently available only in the Mediawiki software starting with version 1.6 and later.
 * The term "variable" has two uses in this document:
 * As a noun it means a type of magic word, which is a system-based variable that can be accessed in the same manner as templates (thus will return the name of the current namespace, depending on the page).
 * As an adjective or adverb, it is used in the general sense, to indicate that something may vary with context (thus a "variable template name" means that the name of the template being called can change according to parameters or variables).
 * Using "subst:" with manual recursion gives a stepwise replacement, useful for analyzing and explaining the working of templates calling other templates.

Variable templates
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 that can be simpler than with, though they are generally less versatile.

Examples:


 * Using a variable to choose a template -
 * The magic word returns the current namespace, like so:  = '. The outer brackets then process this result as a template in its own right - in this case Template:Help - which produces '.


 * Using a template to choose a template -
 * the template Tctc contains the text "tc". This text is processed by the outer brackets as Template:Tc which contains the word "".

The extra spaces in the above examples are needed. Without the extra spaces, the pair of inner three braces is taken as those for a parameter. On the page itself it just shows as plain text:.


 * Using a parameter to choose a template -
 * The second parameter passed becomes part of the template name to which the first parameter is passed. In this case  would produce ,   would produce  , and etc. Template:Histogram uses this technique repeatedly in  to produce the following 5 line histogram:

"" Using parser functions and templates. A template name can also depend in a more complicated way on parameters and/or variables.

Templates passed as parameters
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.


 * Passing a template result -
 * Template:3x contains .  first evaluates tc (which yields the word in), and passes that to template 3x, to give.


 * Passing a template result recursively -
 * Just as above except tc (in) is first passed to 5x and the result of that is passed to 3x, to give.


 * Passing a template name -
 * Template:Tt -  - 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 "tt t V".

Variable parameter name
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 - Template:T p contains, with two parameters - "capital" and "country". can be used to select which parameter is used in a particular case. Thus:

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.
 * Parameter name from another parameter in the same template -
 * using Template:Ppp, which contains, the code   will first set the parameter named "foo" to the value "bar", and then set the parameter named "p" to the value of foo, yielding . The order in which the parameters appear in the code does not matter, but the technique cannot be applied multiple times—e.g., using Template:Tvvv, which contains  ,   gives.

This is e.g. applied in Template:Reg polyhedra db, which contains a 2D array in the form. 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. 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

While 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.

Branching techniques without ParserFunctions
The parameter default feature was introduced before. This led to the development of branching methods through the parameter default mechanism.


 * If-defined branches -
 * If no value is passed for the parameter test, then resolves to  and returns a blank entry (since test is defined as blank). If the parameter "test" is assigned the value "boo", however,  resolves to , and so long as no parameter "testboo" exists, then this will return the value of the parameter "then".

See Template:Ifwpc for comparisons.

There was also an Help:Array#Array_technique_using_parameter_defaults|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, template:a uses b=c if b≠ and b=e if b=. See Template:T pdc.

Another old "branching technique" is using a template name depending on the value of a parameter (see above).

Variable variable names

 * Magic word depending on a template -
 * gives the text  without parameters, since it defaults to

27, but gives  while gives . Any magic word that begins with "CURRENT" can be accessed this way.


 * Parser function parameter depending on a template parameter -
 * In Template:Namespace, which contains " ",  gives "", because Meta is the name of namespace 4.

External examples

 * VARIABLE MAGIC WORDS
 * Template:H:h and Template:H:f (see edit pages, Help:MediaWiki help policy#Keeping_project-specific_material_separate|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)
 * 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)

<!-- obsolete items hidden for the time being - need to review


 * The following is somewhat obsolete on wikis with the ParserFunctions extension, which allows easier branching.

This can be combined into the following branching technique:

with Template:Tts containing  (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.


 * gives
 * gives
 * gives

Example with an extra parameter:

Consider, which calls : 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 :


 * gives:

"".
 * 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 :


 * gives:

"".

This would not be convenient to use in many template tags, but could be used in an infobox in the form



(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= followed by 1=void; if is empty then this overwrites the value of 1, so 1=void, otherwise 1=, giving an empty result or. is typically the same as (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 is empty.

More generally, using :


 * gives
 * gives
 * gives
 * gives

Note that produces underscores for spaces, which are not considered equal by the template:


 * gives
 * gives

Producing the value of a parameter whose name depends on the value of another parameter
The technique can also be used to create control flow structures. An ifdef-function as in could look like: 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 {&#123;{then}&#125;}, 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: 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 {&#123;{10}&#125;} instead of okay.

It's slightly different for an ifndef-function as in : 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 with a defined value y for x we expect to get the empty default for a non-existing xy. This fails if {&#123;{xy}&#125;} is defined and non-empty.

An if-then-else could be a combination of those two, as in.

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: For the opposite ifndef-case there's unfortunately no similar direct approach. Substitution works for Template:ifdef|ifdef, but not Template:ifndef|ifndef or Template:ifold|ifold.

A parameter value can also be used as part of the name of another parameter:

using gives

Using this technique and an auxiliary template, we can produce a function that checks if two parameter values are the same:

, with.

It will return 1 if equal, or null (the empty string) if not.

-->