Constant Noble:Templates

From Constant Noble
Jump to navigation Jump to search

A template is a wiki page created 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, standardized 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.

Help:A quick guide to templates gives a brief introduction to the subject. There is further help from MediaWiki and Wikimedia at mw:Help:Templates, m:Help:Template, and m:Help:Advanced templates.

Template link to template

To include the actual full name of the template as text (including the surrounding braces) in a Wikipedia article, apply the tl (template link) template. The primary motivation to do this is in instruction and documentation. A short example is the code:

{{tl|main}}
generates
{{main}}

General description

Most templates are pages 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,[1] 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 branchings (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 into an article or page, type {{Template name}} in the wikitext at the place where the template is to appear. The first letter may be indifferently lower- or upper-case.

The prefix Template: before the template name is the default one and need not be included. However, for templates stored in other namespaces, the prefix, such as User:, must be specified. To transclude a page in mainspace, precede its title with a colon, as {{: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 or MediaWiki)—to use a template on another language project, a copy of the template must be created in that project.

Usage syntax

Parameters

This section is about template parameters. For search parameters, see w:Help:Searching#Parameters.
Further information: m: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 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 {{{1}}} (named parameters). The first, second, third, etc. unnamed parameters will be 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.

Which 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, such as when the pipe or equals sign is followed immediately by the next pipe or the closing braces. This is different from omitting the parameter altogether, which leaves it undefined, although templates are often coded so as to behave the same in both cases.

Parameters can be specified (and will do nothing) even if not represented in the template's code. For example, |reason= is frequently used as a pseudo-parameter to explain briefly in the wikisource why the template was placed.[2] Some templates call Module:Check for unknown parameters to warn the editor if a parameter is being used that is not accounted for in the template's code; this is mostly used for infoboxes and other templates with a large number of complicated parameters, where the presence of an unknown one is usually an unintentional error. If you update such a template to include a new parameter, its call to the module must also be updated to include the new parameter.

Calling

Further information: mw:Help:Magic words

Calling a template is not unlike calling a variable or a function in a programming language: call it with a reference and it returns a value (the output). Like functions, some templates accept parameters that change the output.

In MediaWiki, the wiki software that Wikipedia uses, variables have a more specific meaning that distinguishes them 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 wiki markup and HTML.

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

Substitution

Further information: w:Help:Substitution

When a template is substituted, its content is hard-coded in the page rather than transcluded. To learn how and when to substitute a template, see w:Help:Substitution.

Template list

Notes

  1. ^ Namespaces from which transclusion is not allowed are specified on a wiki by the variable $wgNonincludableNamespaces.
  2. ^ Some templates, such as {{Requested move}}, have code to display |reason= as visible output; whether to do so is determined on a template-by-template basis.
From Wikipedia
From Wikipedia

Part or all of this page uses material from Help:Template, originally located at Wikipedia. The list of original contributors can be found in its page history. Text on Wikipedia is available under the Creative Commons Attribution-ShareAlike 3.0 license; additional terms may apply.