Help:Template

From para.wiki
Revision as of 21:32, 14 December 2018 by Anonymous
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

A template is a page made to be included in other pages. Templates usually contain repetitive material that might need to show up on any number of articles or pages. They are commonly used for boilerplate messages, standard warnings or notices, infoboxes, navigational boxes and similar purposes.

The most common method of inclusion is called transclusion, where the wikitext of the target page contains a reference to the template, using the {{Template name}} syntax. Another method is substitution, where the content of the template is copied into the wikitext of the target page, just once, when it is saved.

For a brief introduction to the subject, see A quick guide to templates.

General

Most templates are in the Template namespace, which means that they have titles in the form "Template:XXXX". It is possible, however, to transclude and substitute from any namespace, and so some template pages are placed in other namespaces, such as the User namespace. Template pages have associated talk pages.

Templates can contain any desired wikitext, including calls to other templates. They have some limited programming capacities: customizable values called parameters, calculation and branching (using parser functions), and access to wiki-specific variables (magic words), such as dates, times, and page names. They may also contain tags which define which parts of the wikitext are to be included when the template is transcluded or substituted. This means that the appearance of the template page itself need not be the same as that of the transcluded content (for example, it can contain documentation, categories, etc. for the template).

How to do it: To transclude a template to an article or page, type {{template name}} in the wikitext at the place where the template is to appear. The prefix "Template:" need not be included, and an initial capital is not necessary.

Templates stored in other namespaces are transcluded in the same way, except that the namespace prefix, such as "User:", must be included. To transclude a page in mainspace, precede its title with a colon, as, for example, {{:Page name}}.

Note: Attempting to transclude a template that does not exist produces a red link, just like linking to any other nonexistent page. Following the link allows one to create that particular template. It is not possible to transclude pages between projects (such as different language Wikipedias) – to use a template on another language project, a copy of the template must be created in that project.

Usage syntax

Parameters

Further information: metawikimedia:Help:Templates § Parameters

The basic transclusion syntax given above can be extended by the addition of parameters, which are used to control the template's output. The syntax for this is

  • {{template name|parameter|parameter|...}}

where each "parameter" may either contain just a value (these are called unnamed parameters) or be of the form name=value (named parameters). The first, second, etc. unnamed parameters are equivalent to parameters named "1", "2", etc.

Whitespace characters (spaces, tabs, returns) are stripped from the beginnings and ends of named parameter names and values, but not from the middle: thus {{ ... | myparam = this is a test }} has the same effect as {{ ... |myparam=this is a test}}. This does not apply to unnamed parameters, where the whitespace characters are preserved.

What parameters (if any) can or should be passed to a template, and how they are to be named, depends on the coding of that template. Named parameters can be defined in any order. Superfluous or misnamed parameters will be ignored; undefined parameters will be assigned default values. If a parameter is defined more than once, the last value takes effect.

The value of a parameter can be the empty string (pipe or equals sign followed immediately by the next pipe or the closing braces). This is different from leaving the parameter undefined (although templates are often coded so as to behave the same in both cases).

Substitution

If a template is to be substituted in the wikitext rather than transcluded, add the modifier subst: after the initial pair of braces, as in {{subst:afd}}. Parameters can be added as normally.

  • Note: if substituting a page from mainspace, the word "subst" must be followed by two colons.

Other details

Template names are exactly like other page names: case-sensitive except for the first letter, with spaces indistinguishable from underscores. If the symbol # (normally used to link to a section of a page) appears in a transclusion, then it and any characters that follow it are ignored. If a redirect is transcluded, the redirect's target will be transcluded instead. See #Aliases for more.

Notice that the same double-brace syntax is used for many MediaWiki variables and parser functions (see Help:Magic words). For example, the code {{NAMESPACE}} may look like a template call, but it is actually a variable whose value is the namespace prefix of the current page.