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

Parameters
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,  , 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.

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

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

Further reading:
 * Help:Extension:ParserFunctions, mediawiki.org

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.

Substitution
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. However, this would lead to a substitution at the time of saving the template, which is undesirable. A solution:.

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:
 * W:Module:String
 * W:Template:Replace

Example use:
 * bat on the mat --> "bat on the mat"