Help:Template

A template is a page that is included in another page.

Transclusion in Media Wiki
Any page can be used as a template, all that is necessary is to put its title between two pairs of braces:  . When viewing a page with another page included in it, the template will appear exactly where code is located in the wikitext. The official term used when a template is put into a page is "transclusion".

For example,   will transclude an exact copy of that page (when you alter the page, anywhere that transcludes it will also display the change) like so (without the lines above and below which have been added on this page) Note that in this example a blank space is left where namespace should appear because it is in "Main" namespace.:

As you can see, when transcluding an article like the above it's as if the exact wikitext was pasted into where the template is - you can't tell what is and what isn't a template by looking at the final article. It even appears in this page's table of contents.

Substitution
Putting "subst:" after the double opening braces ( ) is called substitution. When the page is saved, the subst tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters. N.B.: This means that the displayed content will not be changed if the template itself is amended.

Template space
Pages that are designed to be readily and repeatedly used are usually in the Template namespace. In the case of the template space the namespace does not need to be included inside the braces:  . For example,   is very commonly used on Help articles and is responsible for generating.

Parameters
There are complications. Templates can have "parameters" which will affect the appearance or content.

When using a template with unnamed variables care must be taken.

If using just (say) variables 1 and 4 then use it as: or or
 *  
 *  </tt>,
 *  </tt>
 * (see template:Cquote for an occasion when you will probably need to do this).

It will also be necessary to include "number="</tt> if there are any equals signs (=</tt>) in the variable.

If or when you create a new template, especially if it's more than a simple transclusion, please include instructions on the template page itself. Render them safe using the  </tt> tags around the instructions so they aren't transcluded.

Restricted characters
Restricted characters are those that have special meaning within MediaWiki's template syntax, such as:


 * = (used to assign parameter names)
 * (used to separate parameters)

To use a restricted character within a template parameter, transclude it from another template! Restricted characters are not restricted in templates, just in parameters.

Examples, in template parameter calls:
 * For "=" use = -- Template:= contains "="
 * For "|" use | -- Template:! contains "|" (Note that we use ! in this template name instead of | because | cannot be part of a page name.)
 * For "||" use -- Template:!! contains "||"

Selective transclusion
In order to transclude part of a page only, sections of a template can be blocked out with special   tags. When placed around a block of text,  like so </tt>, the text will be exempt from transclusion. This is often used for explanations of how to use the template. It was used in the example ( </tt>) at the top of the page to avoid carrying a "stub" template and a category over.

Content can be made to show up only in transclusions by using   tags. This might be used to put the receiving page into a particular category without putting the template itself into that category:  </tt> will put the receiving page into the (non-existent) category Greek</tt>.

The last option is the   tag. If this tag is used, only the content it encloses will be transcluded but it will also show on the Template page itself.

Making a template
Templates are made like any other Media Wiki page but are put in the template namespace. The easiest way to learn about templates is to copy and alter an existing one - testing the results on your user page or in a sandbox before placing it all over the site, of course. This can be easily done with the many "userbox" templates that adorn many userpages.

Text between noinclude tags on a template wont be transcludeded--it will only show on the template page itself. Use this to put a note about how to use the template on the template page itself. Example:

Use
Say what the template is for, and show how to put it on a page (typically we show wiki markup in &lt;pre> tags so that it looks like an example and doesn't get parsed).

&lt;pre>

&lt;/pre>

Text between includeonly tags on a template will only be transcludeded--it will not show on the Template page itself. If you want all pages with the template on it to be listed in a particular categories (but you don't want the Template page listed in the category) then put a category tag in your Template within includeonly tags.

A basic template that uses noinclude and includeonly tags is Template:Reference needed. Click on its Edit tab and have a look!

Complex templates
Templates can be extremely complex, allowing the use of many variables and "sub" templates. If you really want to know about them try here at meta Wikimedia. The majority of the situations where a complicated and multilayered template may be needed (such as those that make up the mainpage), templates are already in existence.

Purge
Sometimes a template that has been recently edited does not display as expected because MediaWiki's cache still contains the old version of the template. If you are experiencing this, purge the page to force MediaWiki to refresh its cache for that page.

A simple way to "purge" a MediaWiki page you are viewing in your browser is:
 * click its "View history" tab, then in your browser's location field delete just the word "history" off the end of the URL, type "purge" there (after the "=") on the end of the URL, and hit return.

This refreshes MediaWiki's cache for all templates used on that page.