Help:Templates

A template is a page that is intended to be transcluded in another page.

Transclusion in MediaWiki
"Transclude" is wikitext for " trans fer/in clude "; it means the copying (transfering) of the contents of another page and adding (including) it in the target page.

Usage
Any page can be transcluded; all that is necessary is to call it correctly. To do so, use double brackets :. For a list of namespaces, see Help:Namespace.

When viewing a page with another page included in it, the transcluded page will appear exactly where the code is located in the wikitext source code. If you want a template to be located on top of the page, call the page to be transcluded on the top.

Example:

helpnav will transclude an exact copy of the page. Result:

As you can see, when transcluding a page like above, it's as if the exact wikitext was pasted into where the template is — you can't tell what is and isn't a template by looking at the final article.

If no namespace is given, calling a page will default it to the  namespace, i.e.   will produce the same result as.

If you want to transclude a mainspace article, use a colon before the article title:. For the sake of convenience and organisation, everything that is meant to be transcluded should be kept in the  namespace.

Substitution
The method above loads a template as it was last saved. That means, everytime a template is modified, every page that uses it will be re-rendered if the aforementioned method is used. There are alternatives, however.

Usage: Writing  at the beginning i.e.   will replace it with the content within   saved at the time. When the page being edited is saved, the  tag is replaced by the wikitext of the template. In other words, the displayed content will not be changed if the template itself is modified afterwards.

Example: Imagine there is a hypothetical template called  with the following content: Hello world!

If you use the first method (loading: ), it will produce: When you edit the page (after saving it, of course), it will still show  in the source code. If someone edits the template, the page will re-render to show the updated version of  , which may differ from the original content.

If you use substitution, it will also produce: But, this time, when you edit the page (after saving it, of course), it will not show. Instead, it will show the wikitext contained at the time:.

Benefits of substitution

 * 1) Templates are often modified or deleted.  If a template is boilerplate text, consider whether you want it to vary as the template is modified.  If your answer is "no", substitution is warranted.  An example of this is the welcome template.  The archives of a user's talk page should show the actual welcome message they received, not the current welcome message.
 * 2) If there is a template you would like to modify for a single occasion, but you do not want to modify the template for all the pages that use it, and you do not want to make a new template, you can substitute the template and then with a second edit, make the modifications.  For example: a template might be used with all the states of the United States and with the District of Columbia (DC). The template might be worded correctly for the states, but not for DC. To fix the wording, you can substitute the template and then fix the wording to fit it for DC.
 * 3) Substituting frequently used templates limits the damage that can be caused by vandalizing them.
 * 4) Substituting en masse may speed up the site, though the amount and significance of this is the subject of frequent debate. Every time a page is rendered, the server must get text from a separate page for each template used; while each individual template has little effect, the number of templates used on RationalWiki is one factor affecting server load and article load times.

Template space
Pages that are designed to be readily and repeatedly used are usually in the  namespace. Again:

If no namespace is given, calling a page will default it to the  namespace, i.e.   will produce the same result as.

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

If using just (say) variables  and   then use it as:   or , or   (see template:Cquote for a template where this often occurs).

It will also be necessary to include  if there are any equals signs   in the variable (see sbs for a template where this might be necessary).

Creating templates
If you create a new template, especially if it's more than a simple transclusion, please include instructions on a "doc" (document) sub page of the template i.e..

Then, include the instructions on the actual page using the doc template:.

The function and usage of all parameters should be included in the instructions, since most people just want to use the template, not understand how it works.

Lastly, please add it to the template list.

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,, the text will be exempt from transclusion. This is often used for explanations of how to use the template.

Content can be made to show up only in transclusions by using  tags. This is used, for example, on some of RationalWiki's cover stories where we want a different (or a differently sized or aligned) image to be displayed on the mainpage to that on the article.

The last option is the  tag. This allows the material to appear in the source, but limits transclusion to that section.

Making a template
Templates are made like any other MediaWiki 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.

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 Wikipedia, but be warned, it's not simple. A better "Help" page is 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.

Useful reference links:
 * The Parser functions extension
 * Magic words