Creating an account only takes 20 seconds, and doesn’t require any personal info.

If you’ve got one already, please log in.🤝

Help:Templates

From Teflpedia

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: {{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, {{:Michael Lewis}} 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.:

Michael Lewis (/ˈmaɪ.kəl ˈlu:.wɪs/ 1945[1] – 2019)[2][3] was an English teacher and author. He was the author of The Lexical Approach and The English Verb.[4]

He was co-founder of Language Teaching Publications (LTP).

Also Thornbury.[5]

Publications

  • The English Verb ISBN 0 906717 40 X
  • The Lexical Approach
  • Implementing the Lexical Approach ISBN 1 899396 60 8

References

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

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, {{Boxword‎ }} is very commonly used on Help articles and is responsible for generating this box.

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:

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

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 <noinclude> </noinclude> 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 <noinclude> </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. It was used in the example ({{:Michael Lewis}}) 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 <includeonly> tags. This might be used to put the receiving page into a particular category without putting the template itself into that category: <includeonly>[[category:Greek]]</includeonly> will put the receiving page into the (non-existent) category Greek.

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

<noinclude> Text between noinclude tags on a template wont be transcludeded--it will only show on the Template page itself. Put a note about how to ==Use== the template on the template's page within these noinclude tags.</noinclude>

<includeonly> 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 category (but you don't want the Template page listed in the category) then put a category tag in your Template within includeonly tags</includeonly>

An basic Template example that does all of the above is Template:Reference needed. Click on it's 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.

See also