User:Happy-melon/Templates

The MediaWiki software includes the capacity to include the contents of on page in the display of another page - this process is known as transclusion. Since one page can be transcluded on many other pages, the most common use of transclusion is to create boilerplate text, which can be displayed on many pages but updated easily from one location. For this reason, pages which are created primarily for the purpose of being transcluded on other pages are known as templates. Most wikis include a "Template:" help:namespace to house these templates.

This page and its extension Help:Advanced templates form a reference manual for how to use templates and the associated wikimarkup on MediaWiki. Templates can also make use of other features of the MediaWiki software, such as ParserFunctions.

Note: The master version of this page is held at m:Help:Template on Meta-Wiki. All links and template examples work correctly there, but may not if you are viewing a copy of this page on another project.

Introduction
Most MediaWiki sites include a Template namespace which holds pages designed specifically for transclusion on other pages. While this namespace can be renamed, for instance if the project is in a language other than English, the canonical prefix Template: will always function as a shortcut to that namespace. So while the correct name of the Template: namespace on the German Wikipedia is Vorlage:, and so the correct link to the User en Babel box is w:de:Vorlage:User en, using w:de:Template:User en will also work.

A page within the Template: namespace is known as a template, although it is no different to any other page: the same wikimarkup can be used on templates as on any other page. However, many Wikipedias and other MediaWiki sites have policies to restrict the type of content that can appear in the Template: namespace. A page in the template namespace can be linked to like any other page: Template:TEx1 is a link to an example template that will be used later.

This example format will be used throughout this documentation page. If, instead of enclosing the link in square brackets, the link is enclosed in braces  , this is a signal to the MediaWiki parser to transclude the page at that point, rather than link to it. So:

Note that when using braces, the page to be transcluded defaults to the Template: namespace if no other namespace is specified. To transclude a page in another namespace, the full pagename must be specified. So to transclude this page's talk page, type. To transclude a page from the main namespace, use an 'empty' namespace field; to transclude the Main Page, you would need to type. Not all namespaces can necessarily be transcluded: this is a definable parameter of the MediaWiki software which is set in $wgNonincludableNamespaces. It is not possible to transclude pages across projects: tyring to transclude w:de:Vorlage:User en anywhere but the German wikipedia outputs:.

The process of transcluding a template in the manner shown above has several synonyms, including: inclusion and embedding; however inclusion is sometimes used to mean substitution, which is entirely different. The code within the curly braces is often known as a template call.

Whatlinkshere
When viewing the Special:Whatlinkshere page for a page which transcludes templates, the pages which are transcluded are noted. This note is stored in MediaWiki:Istemplate, and defaults to "inclusion"; some projects (such as the English Wikipedia set this to "transclusion", and of course projects in other languages will use an appropriate translation. This list also appears at the bottom of the edit page.

Each page on a wiki is associated with several tables which store useful information; a list of all pages transcluded on the current version of the page is stored in the  table. When a page is edited, this table is used to create the list of transcluded pages. This has several caveats: Pressing the "show preview" button generates a new temporary  table, resulting in a correct display thereafter. Pages are also included on the  table if they are transcluded via conditional statements: if the code   were included on a page, both Template:TEx1 and Template:TEx2 will be marked as transcluded, even though the conditional statement will always display.
 * Editing an old version of the page will cause the current list of transcluded pages to be displayed, even if they are not transcluded in the old version
 * Editing a section of a page will display the full list, even if some of the pages are not transcluded in that section.

Passing parameters
While some templates simply include pure text, most include customisable values called parameters. Parameters are signified in the wikimarkup of the template page inside triple braces:  signifies that when the page is transcluded, the contents of the parameter name, if it exists, will be included in the text at that location.

By default, the parameters are numbered in order, starting from 1; there is no practical limit to the number of parameters that can be defined (at least 65536). However, named parameters may be added at any point without interrupting the numbering:

Some points to note from these examples:


 * If no parameter name is specified, the value is assigned to the first unused numbered parameter.
 * Parameters cannot be 'overloaded' - if a parameter is assigned a value more than once, only the last value is retained
 * Whitespace is always stripped from before and after named parameters, but not always from unnamed parameters.
 * It is possible to assign values to the 'null' parameter by using . These values will never be displayed

Based on these concepts, it is possible to develop some quite sophisticated template layouts. A good real example is w:en:Template:Chess position on the english wikipedia. This template takes 65 unnamed parameters, but to standardise the layout of the wikicode dummy parameters are used:

In this template call, the row numbers and column numbers are repeatedly assigned to the null parameter using the code. The template call is syntactically equivalent to, but is much easier to understand.

Equals signs
Unnamed parameter values cannot contain equals signs. Named parameters can contain equals signs. This is most commonly seen with User signatures containing HTML code. Imagine a signature:, which would display as Example. If this code were passed to a template as an unnamed parameter (eg, then when the template is parsed, the code is instead interpreted as assigning the value   to the named parameter  .  To prevent this, the value must be passed to a named parameter.  So   will work correctly (producing ).

It is sometimes easier to replace the equals sign with its HTML equivalent, which renders as. However this will not work if the equals sign is involved in another template call or HTML tag:  will display as Example.

Pipe character
The pipe character cannot appear in a parameter value unless it is a part of: This means that parameter values cannot contain raw wikitable code. However, the template  has been provided on most Wikis; this template contains only the raw pipe character, so it can be used in table code. It is also possible to use the HTML pipe character (&amp;#124;)unless the pipe is 'functional':
 * 1) Another template call
 * 2) A piped link
 * 3) An image tag

Default parameters
If a parameter is included in the template code, but is not defined in the template call, then it is not replaced when the template is transcluded, as shown in the last two examples above. To avoid this, default parameters can be specified. Defualt parameters are placed within the triple braces, after a pipe character, and can be another parameter: