Templates

Offline MediaWiki Code Editor

Assistant for Templates
Previous Top Next

For the Administrator of Templates see: Administrator of Templates
graphic
An assistant to insert templates stored in the database application.
A template is a Wikipedia page created to be included in other pages. Templates usually contain repetitive material that might need to show up on any number of articles or pages. They are commonly used for boilerplate messages, standard warnings or notices, infoboxes, navigational boxes and similar purposes.

The most common method of inclusion is called transclusion, where the wikitext of the target page contains a reference to the template, using the {{Template name}} syntax. Another method is substitution, where the content of the template is copied into the wikitext of the target page, just once, when it is saved.

Templates can contain any desired wikitext, including calls to other templates. They have some limited programming capacities: customizable values (called parameters), calculation and branchings (using parser functions) and access to wiki-specific variables (magic words), such as dates, times, and page names. They may also contain tags which define which parts of the wikitext are to be included when the template is transcluded or substituted. This means that the appearance of the template page itself need not be the same as that of the transcluded content (for example, it can contain documentation, categories, etc. for the template).


Notice that the same double-brace syntax is used for many MediaWiki variables and parser functions (see Help:Magic words). For example, the code {{NAMESPACE}} may look like a template call, but it is actually a variable whose value is the namespace prefix of the current page.

To transclude a template in an article type {{template name}} in the wikitext in the point where the template should appear.

Sintax

The basic transclusion sintaxis given above may be extended with the addition of parameters, used to control the display. The sintaxis is:

{{template name | parameter | parameter |...}}


where template name is the name of the template and each parameter may contain a value (unnamed parameters) or be in the form name=value (named parameters). The unnamed parameters are given numbers 1, 2, 3 etc.


White spaces, line-breaks and tabulations are disregarded from the beginning and end of the named parameters and values, but not from the middle: thus {{...| myparam = this is a test }} has the same value than {{...|myparam=this is a test}}. This is not applied to unnamed parameters, where all white spaces are preserved.
   
The parameters (if any) that can pass to a template, and the way they should be named depend on the codification of that template. The named parameters may appear in any order. The superfluous or non-identified parameters will be ignored, while unnamed parameters will receive default values. If a parameter is defined more than once, the last value will be taken.

A parameter value may be left blank (the vertical pipe or equal sign may appear before the following vertical pipe or by the closing braces. This is different from omitting the parameter leaving it undefined, although the templates are often codified to behave the same way in both cases.


Call

To call a template is not like calling a variable or function in a programming language: It is called with a reference and it returns a value. But like functions, some templates accept parameters that change the layout.

In MediaWiki, the variables have a more specific meaning which distinguish them from the templates, but both are identified by double braces {{ }} and both return a value.

While the variable names in MediaWiki are all in capital letters (see: Magic Words), the template names have the same basic characteristics and limitations than all page names; they are case sensitive (except the first character), lower dashes become spaces, these cannot contain any of the characters # < > [ ] | { } because are reserved for wiki and HTML code.

Substitution

When a template is substituted, its content is inserted in the page instead being transcluded.

Examples with parameters

An example of a template which take parameters is {{about}}. Try typing {{about|how to use templates|how to use modules|Help:Module}} in the sandbox an it will produce the following text:

This page is about how to use templates. For how to use modules, see Help:Module.


{{about}} uses three unnamed parameters in this example, but you can use the template with a different number of parameters to produce a slightly different result. For example:

{{about || how to use modules | Help:Module}}.

Note that there is an empty parameter. At this instance the consecutive vertical pipes mean that the first parameter is an empty string, so in this template the initial phrase will be omitted, and the result will be:

For how to use modules, see Help:Module.

Other templates, the most complex ones, take named parameters or a combination of named and unnamed parameters. A simple example is the template Payoff matrix used to obtain a 2x2 grid.  Example:

{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}

         left               right
5
7
2
9

        Example usage
This template is used without defining all possible parameters. The undefined parameters take default values. The spaces around equal signs are only shown for clarity, but are not necessary and are ignored evaluating the templates (although this do no happen with the unnamed parameters.

The names of the parameters are case sensitive. For example: You can not substitute "DR" by "dr" or "dR". When named parameters are not used, these are ignored.

Infoboxes

Templates for Infoboxes are mostly represented in this repository. Those are more than one- third of all templates stored in the application. Although the assistant contains by default more than 80 templates for infoboxes, including 55 of the most commonly used, these are only a fraction of the total. There are hundreds of templates, too much of them, and the editors are always creating new templates or improving the existing ones.


You can use any of the templates stored in the application, and also add templates of your particular field of interest you may have copied from Wikipedia (see: Administrator of Templates)

If you are looking for a particular infobox you may consult the list of infoboxes in the Collection of Guidelines of this application (see: Collection of Guidelines). But if you are online, see: https://en.wikipedia.org/wiki/Wikipedia:List_of_infoboxes

If you can't find out a suitable infobox template, you still have the resource to emulate one with the help of one of the assistants (see: Infoboxes emulated with tables). The emulated infoboxes you can build with that assistant are entirely similar to the registered ones, except for the fact that the page will not be automatically included into the corresponding category. But this drawback may be easily overcome assigning manually the article to the corresponding category.

Frequently asked questions

Can I use a template in other projects?

No. If you need to use a template in another version of Wikipedia (in other language) you need to create it in that language. Templates in a language cannot be used in other language.

Are templates case sensitive?

Yes, except the first character, which can be either uppercase or lowercase.

Can I use a template inside another template?

Yes.