Help:Templates

From RationalWiki
Jump to navigation Jump to search

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

Transclusion in MediaWiki

"Transclude" is wikitext for "transfer/include"; 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 ({{ }}): {{Namespace:Pagename}}. 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 Template:Helpnav. 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 Template namespace, i.e. {{Name}} will produce the same result as {{Template:Name}}.

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

Substitution

The method above ({{Template}}) 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 subst: at the beginning i.e. {{subst:Pagename}} will replace it with the content within {{Pagename}} saved at the time. When the page being edited is saved, the subst 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 {{example}} with the following content:

Hello world!

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

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

Benefits of substitution

Section adapted from Wikipedia:Template 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 Template namespace. Again:

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

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 {{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. (templatename/doc).

Then, include the instructions on the actual page using the {{doc}} template: <noinclude>{{doc}}</noinclude>.

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.

Note: The <noinclude></noinclude> tags around the instructions stop the instructions being transcluded. That means, whatever is within those tags won't appear on the page that is calling for transclusion.

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.

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:

See also