Help:Templates
From RationalWiki
A template is a page that is included in another page.
Contents |
[edit] Transclusion in MediaWiki
Any page can be used as a template, all that is necessary is to put its title between two pairs of braces: {{namespace:pagename}}. 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, {{Fun:Why_Conservapedia Rocks_More_than_You_Think}} will transclude an exact copy of that page that page (when you alter the page, anywhere that transcludes it will also display the change) like so:
Conservapedia, previously the piƱata of websites, actually ROCKS!
This will explain why.
- It really rocks.
- You get to hang out with cool young chicks.
- Andy loves it when you touch him just right.
- God is on their side.
- You won't roast in Hell
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 an isn't a template by looking at the final article. Mainspace articles are transcluded using only ":": {{:pagename}}.
[edit] Substitution
Putting "subst:" after the double opening braces ({{subst:pagename}}) 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.
[edit] 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: {{pagename}}. For example, {{Example}} will generate this textbox:
[edit] Variables
When using a template with unnamed variables care must be taken.
If using just (say) variables 1 and 4 then use it as: {{templatename|foo|4=bar}} or {{templatename|1=foo|4=bar}}, or {{templatename|foo|||bar}} (see template:Cquote for an occasion when you will probably need to do this).
It will also be necessary to include "number=" if there are any equals signs (=) in the variable (see template:sbs for a template where this might be necessary).
[edit] New templates
If or when you create a new template, especially if it's more than a simple transclusion, please include instructions on a "doc" sub page of the template i.e. "templatename/doc". Then include the instructions on the actual page using the "doc" template thus: <noinclude>{{doc}} </noinclude>. (The <noinclude> </noinclude> tags around the instructions stop the instructions being transcluded) 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.
[edit] Selective transclusion
In order to transclude part of a page only, sections of a template can be blocked out with special <noinclude> tags. When placed around a block of text, <noinclude>like so<noinclude><, the text will be excempt 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 <includeonly> 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 <onlyinclude> tag. This allows the material to appear in the source, but limits transclusion to that section.
[edit] 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.
[edit] 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.

