Help:Templates

From RationalWiki
Jump to: navigation, search

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, {{helpnav}} 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:

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 isn't a template by looking at the final article. If no namespace is given it looks in the template space: {{templatename}}. Mainspace articles are transcluded using only ":" before the article title: {{:pagename}}. For convenience and organisation purposes, everything that is meant to be transcluded should be kept in the template space.

[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] Benefits of substitution

Section adapted from Wikipedia
  • 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.
  • 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. 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.
  • Substituting frequently used templates limits the damage that can be caused by vandalising them.
  • 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.

[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:

This is an example template

[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 a template where this often occurs).

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

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
support
Community
Tools