MediaWiki User Guide/Templates

Templates provide a means to repeat the same text on several pages. More advanced templates make use of parameters, and even of control structures as found in programming languages. That said, basic templates are quite easy to create, requiring no knowledge of programming.

Templates have their own "Template:" namespace.

To create a template called "header", edit the page "Template:header" and place the text to be repeated into that template.

To use the template in a page, type "{{header}}". What marks the use of the template are the "{" and "}" characters, also known as curly brackets.

To replace the name of the template with its contents directly in the source wikitext before the text is saved, use "{{subst:header}}".



Templates can have unnamed and named parameters. The unnamed parameters are automatically numbered.

To use an unnamed parameter inside a template, refer to it using {{{1}}}, {{{2}}}, and the like. Notice the three curly brackets.

To use a named parameter inside a template, refer to it using the same curly brackets and the name, instead of a number, like {{{parameter_name}}}.

To pass a parameter to a template when you use it in a mainspace page:


To pass equality sign (=) in the value of an unnamed parameter, you need a workaround: use, for the parameter number one, {{template|1=text=with=equality=sign}}.



Templates that are to be used by many users are worth documenting. One option is to document them on their talk pages. Another is to document them in their main text, and surround the documentation with <noinclude> tag. A plus side of the second option is that users who want to learn how the template works can see its syntax on the same view as the template if they are experimenting with its code.

Control structures


Control structures such as if and switch are available, if the ParserFunctions extension of MediaWiki is installed.

Overview of control structures
Keyword Syntax Note
{{#switch: <comparison value>
 | <value1> = <result1>
 | <value2> = <result2>
 | ...
 | <valuen> = <resultn>
 | <default result>
{{ #if: <condition string> | <code if true> }}
{{ #if: <condition string> | <code if true>
                           | <code if false> }}
The condition string is considered true if it is non-empty and not consisting only of whitespace.
{{ #ifeq: <text 1> | <text 2> | <code if equal> }}
{{ #ifeq: <text 1> | <text 2> | <code if equal>
                              | <code if not equal> }}
{{ #ifexist: <page name> 
             | <wikitext if page exists>
             | <wikitext if page does not exist> }}

Further reading:

Transcluding any page


Above, you have learned how to use a template, that is, to let MediaWiki replace the name of the template surrounded by curly brackets with the contents of the template. In similar fashion, you can transclude any page, not just a template, by writing the following:


This works for pages in the mainspace. To include any page in any namespace, use:


The use of a template is in fact a special case of this use, just that, as you do not specify any namespace, the Template namespace is used as the default one.

This does not work with some namespaces, such as the Special and Category namespaces.



The method of using templates described in the preceding sections leads to an inclusion of templates, meaning that the source text of the page contains the name of the template surrounded by curly bracket, not its content. There is however another use of templates, in which the content of the template is written directly into the wiki page before the page is saved. This use is called substitution and is achieved as follows:


In a more advanced use, it may be required that control structures such as #if that are present in the substituted template are substituted too, which would seem to be achieved using {{subst:#if ...}}. However, this would lead to a substitution at the time of saving the template, which is undesirable. A solution: {{<includeonly></includeonly>subst:#if ...}}.

String operations


String operations such as obtaining a substring or performing a replacement on a string are not part of the standard template functions. However, on a wiki that has Scribunto extension installed, which enables scripting via Lua, one can create a module for string functions, and templates that make these functions accessible for other templates. This was done in the English Wikipedia:

Example use:

  • {{replace|cat on the mat|cat|bat}} --> "bat on the mat"