Help:Templates

From Bulbapedia, the community-driven Pokémon encyclopedia.
Jump to navigationJump to search
Shortcut
H:T

A template is a Bulbapedia page created to be included on other pages. They usually include repetitive material that might need to show up on multiple articles or pages, often with customizable input. They are commonly used for boilerplate messages, standardized warnings or notices, infoboxes, navigational boxes, and similar purposes. Templates can have parameters so that the same page generates different text on multiple pages that include it.

The most common method of inclusion is called transclusion, where the wiki source 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 wiki source of the target page, just once, when it is saved.

General description

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

Templates can contain any desired wikicode, including calls to other templates. There is some programming ability: customizable values (via parameters); calculation and branchings (using parser functions); and access to wiki-specific variables (magic words), such as dates, times, and page names. They may also contain markup to define certain parts of the page to be included while other parts are not. This means that when you display the template page itself, you may see more than when you display a page that includes it (for example, it can contain documentation, categories, etc. for the template).

To call a template (cause it to be transcluded or substituted in the page), a page contains wikicode in double braces. For example, the wikicode {{example}} calls the Example template. Sometimes the call includes parameters and looks like {{example|comment=This is a comment}}. Note that template calls are not the only thing for which double braces are used in wikicode.

Using templates

General

Using a template is much like calling a function in a programming language – call it, and it returns a value. For a template, the return value is called the expansion of the template and is treated as part of the calling page source. Like functions, some templates accept parameters that affect the output.

Calling a template causes it to be either transcluded or substituted in the page that calls it (which is known as the target page).

Transcluding a template means that when MediaWiki displays the page, it treats the template as if it were in the wiki source of the page, whereas substituting means MediaWiki does that when it saves the page (i.e. the content of the template gets stored as part of the target page and subsequent editors cannot tell that it got there via a template).

To transclude a template into a page, put {{Template name}} in the page source at the place where the template is to appear. The first letter of each word of the title may equivalently be lower- or upper-case.

To substitute a template into a page, use {{subst:Template name}} instead.

This wikicode is called a template call.

In MediaWiki, the wiki software that Bulbapedia uses, variables are distinct from templates, but they are both identified by double braces {{ }} and they both return a value.

Whereas MediaWiki variable names are all uppercase, template names have the same basic features and limitations as all page names: they are case-sensitive (except for the first character); underscores are parsed as spaces; and they cannot contain any of these characters: # < > [ ] | { }. This is because those are reserved for wikicode.

The number sign # is called a "fragment identifier" because it denotes a fragment or section of a document (such as a section in a Bulbapedia article). Although it can be used to link to a section of a template page (like Template:Pokémon Infobox#Example), there is no reason to put a fragment identifier or fragment name in a template reference. In {{Example#Location}}, for example, the string #Location has no purpose and is thus ignored.

The template namespace is the default, so you can leave out the namespace Template: in the template name, and it is conventional to do so. However, you must specify the namspace prefix for templates in other namespaces, such as User:. To transclude a page in mainspace, precede its title with a colon, as {{:Page name}}.

OOjs UI icon lightbulb-yellow.png
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 Bulbagarden Archives). To use a template on another project, a copy of the template must be created in that project.

Parameters

Shortcut
H:PARAMETER

The basic transclusion syntax given above can be extended with parameters, which are used to control the template's output. The syntax for this is {{Template name|parameter|parameter|...}} where Template name is the name of the template, and 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, third, etc. unnamed parameters are given the names 1, 2, 3, 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 all whitespace characters are preserved.

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

The value of a parameter can be the empty string, such as when the pipe or equals sign is followed immediately by the next pipe or the closing braces. This is different from not specifying the parameter at all, which results in a default value, although templates are often coded so as to behave the same in both cases.

If a template call specifies a parameter which is not defined in the template, it has no effect. Sometimes editors do this on purpose: For example, |reason= is frequently used as a pseudo-parameter to explain briefly in the wiki source why the template call was put there.

Hints & tips

The following points may be worth noting when using templates:

  • An unnamed parameter value cannot contain an ordinary equals sign, as this would be interpreted in the parameter specification as a named parameter (with the equals sign separating the name from its value) To specify an unnamed parameter including an equals sign (for example in a URL with name–value pairs), replace the equals sign with the magic word {{=}}, which expands to an equals sign that will not be interpreted. Another method is to replace the unnamed parameter specifications with named parameters specifications—unnamed parameters are equivalent to named parameters where the first unnamed parameter is named "1" and so on. To call template {{Example}} with a=b as the literal value for the first parameter, type either {{Example|a{{=}}b}} or {{Example|1=a=b}}.
  • Similarly, it is not possible to use an ordinary pipe character | in a template parameter specification, as it would be interpreted as separating one parameter specification from another. This problem can similarly be solved by using the magic word {{!}} in place of the pipe, or—if the pipe is not intended to be parsed at a higher level—using the HTML entity &#124;.
  • Remember that whitespace characters (spaces, tabs, carriage returns, and line feeds) are not automatically stripped from the start and end of unnamed parameters, unlike with named parameters. Including such characters (or any other non-visible characters in any parameters) may in some cases affect the template's behavior in unexpected ways.
  • When an editor changes a template (by editing the template or one of its subtemplates), the change will ultimately be reflected any time any page that transcludes that template is subsequently displayed. However, the change may not become visible on all pages immediately; a previously cached version of a page, based on the previous version of the template, may continue to be displayed for some time. Use the purge function to force a page to be displayed using the latest versions of templates—including on the template page itself, if it contains usage examples.
  • When viewing old versions of pages, remember that templates will be transcluded as they are now, not necessarily as they were when the old page version was active.
  • To list all pages that transclude a template, use the What links here link on the page when you display the template page. Note that this will not include pages where the template has been substituted, as there is no record of substitutions.
  • To get a list of templates transcluded on a page, bring up the edit page and find the list below the edit window. This list also includes the subtemplates used by the templates that are directly transcluded. To get such a list for a page section, an old version of the page, or your newly edited version prior to saving, click "Show preview" on the appropriate edit page.
  • There are limits to the number and complexity of the templates that an article may have. See wp:Help:Template limits for more information.

Creating templates

See: Help:Creating templates.

See also