A template is a Wikipedia 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, standard warnings or notices, infoboxes, and similar purposes.
The most common method of inclusion is called transclusion, where the of the target page contains a reference to the template, using the <nowiki></nowiki> 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.
Templates can contain any desired , 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 to an article or page, type <nowiki></nowiki> 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 , precede its title with a colon, as, for example, <nowiki></nowiki>.
Note: Attempting to transclude a template that does not exist produces a , 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.
Whitespace characters (spaces, tabs, returns) are stripped from the beginnings and ends of named parameter names and values, but not from the middle: thus <nowiki></nowiki> has the same effect as <nowiki></nowiki>. 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).
Notice that the same double-brace syntax is used for many MediaWiki variables and parser functions (see Magic words). For example, the code <nowiki></nowiki> may look like a template call, but it is actually a variable whose value is the namespace prefix of the current page.
An example of a very simple template can be found at , which places the lambda symbol, λ. Click the "Edit" tab on that template page to see the template code (its wikitext). The "active" part of that code, called the expansion of the template, is &lambda;. (The remainder of the wikitext is enclosed between {{Template name}} tags, which means that it is displayed on the template page itself, but will not be included along with the template.)
To transclude the above template onto another page (e.g. in the Sandbox), type <nowiki></nowiki> (or <nowiki></nowiki> – the case of the first letter is not significant) somewhere into the wikitext of the target page, and save it. The page will be displayed as if the template call were replaced by the expansion of the template, i.e. as if the wikitext contained &lambda; at that point. The displayed page will therefore contain the text "λ".
For example, type "The 11th letter of the Greek alphabet is the lambda (<nowiki></nowiki>)" and you will see "The 11th letter of the Greek alphabet is the lambda ()". You can use templates without knowing the details of their code – you only need to remember what result they produce (this is usually described on the template page).
Another way to use this template is to substitute it. If you type "The 11th letter of the Greek alphabet is the lambda (<nowiki></nowiki>)" and save the page, you will again see ""The 11th letter of the Greek alphabet is the lambda (λ)". However this time, if you look again at the saved wikitext, you will see that the template calls really were replaced by the expansion of the template when you saved the page. The link between the output text and the template is now broken, and the output will not be affected by changes which might be made to the template at some future time (as it would be in the case of transclusion).
This usage takes two unnamed parameters, but the same template can also be used with different numbers of parameters to give slightly different results, as explained in the documentation on the template page. For example, <nowiki></nowiki> produces
Note in particular the usage with an empty parameter – here the consecutive pipes mean that the first parameter passed to the template is the empty string.
Other templates, particularly more complex ones, take named parameters (or a mixture of named and unnamed ones). A simple example is , used to generate a 2-by-2 grid. Type:
to produce the grid you can see on the right.
See the template page for more possibilities. Notice that the template is used here without defining all its possible parameters – undefined parameters are given default values.
The spaces around the equal signs and before and after the parameters are used only for clarity – they are not needed, and are ignored when the template is evaluated (although this is not the case with unnamed parameters). However parameter names are fully case sensitive – for example, it is not possible to replace "DR" by "dr" or "Dr" in the above example. Parameters with names that are not used by the template are simply ignored.
Before creating a template, do a quick search for existing templates (e.g. by exploring Wikipedia templates) to see if there's already a template that does what you want, or a similar template whose code can be copied and modified (or left in place and expanded). Look for generic templates on which the new template can be based (for example, templates can be easily created by calling the generic ).
There is no hard rule about what name to choose for a template – make it short but reasonably descriptive. If similar templates exist, try to follow a consistent naming pattern. Templates can be renamed without breaking existing transclusions, provided a redirect to the new template name is left behind.
Be extremely careful when editing existing templates – changes made can affect a large number of pages, often in ways you might not expect. For this reason many high-use templates are against editing except by (other editors can propose changes on the talk page). Some templates offer a sandbox and test cases for experimentation.
To propose the deletion of unused or inappropriate templates, or other changes in the way particular templates are used, go to (TfD).
If a parameter is not assigned a value, then the above replacements will not take place – the form "}" will remain as the effective value of the parameter. To change this behaviour, define default values using the pipe syntax. For example, <nowiki></nowiki> will be replaced by the first unnamed parameter if there is one, or otherwise by the text "default". Similarly, <nowiki></nowiki> will be replaced by the parameter named xxx if it exists, or otherwise will be left blank. Though if a template is called with the parameter specified as empty (e.g. {{Example|}}), the default for the parameter will be ignored; if that is undesired one can use <nowiki></nowiki> instead to get the text "default" even if the parameter is specified as empty.
Parameters do not get expanded when they are inside nowiki tags or XML-style extension tags. Thus, the following will not work within a template – {{:Page name}} – because the parameter is not expanded.
Because of the multiple uses of double-brace and triple-brace syntax, expressions can sometimes be ambiguous. It may be helpful or necessary to include spaces to resolve such ambiguity, for example by writing <nowiki></nowiki> or <nowiki></nowiki>, rather than typing five consecutive braces. However, watch out for unwanted whitespace appearing in template expansions.
Some of the most often used variables and functions are listed hereafter. For more, see Magic words, and the fuller documentation at the MediaWiki pages and .
| Examples of core parser functions ! width="30%" | Result | |
| Uppercasing text | {{template name|parameter|parameter|...}} | |
| Lowercasing text | {{ ... | myparam = this is a test }} | |
| Getting a namespace name | {{ ... |myparam=this is a test}} | |
| Getting a Wikipedia URL | {{subst:afd}} |
The ParserFunctions extension gives more programming-oriented parser functions.
| Examples of extension parser functions ! width="30%" | Result | |
| Testing between options | {{NAMESPACE}} <noinclude>...</noinclude> | |
| Testing if a parameter is set | {{lambda}} | |
| Making a calculation area | {{Lambda}} | |
| Testing the result of a calculation is | {{lambda}} |
| Examples of System Variables ! width="30%" | Result (for this help page) | |
| Page names | {{subst:lambda}} {{for|the card game|Contract bridge}} | Help:Template |
| Name of the current namespace | {{For||Latins|Latin (disambiguation)}} | help |
| Number of registered users | {{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }} | |
| Number of pages in a given category | {{done|a{{=}}b}} | |
| Current software version | {{done|1=a=b}} | |
| Timestamp of last revision | {{tl|tc}} |
The PAGENAME and NAMESPACE variables are particularly useful, and frequently used, to change template behavior based on context. For example, if the template transcludes a category link (e.g. cleanup templates, which transclude a link categorizing the page as a page which needs cleanup), it will often check the NAMESPACE variable to make sure that talk pages, user pages, or anywhere else the tag might incidentally be placed do not themselves get categorized as pages needing cleanup.
To pass a parameter value to a nested template, place a parameter tag as the value of one of the nested template's parameters.
Template parameters themselves can be chosen conditionally.
A template can call itself, but will stop after one iteration to prevent an infinite loop.
When a nested template contains unmatched braces – as in } – the unmatched braces are treated as text during processing, and do not affect the parsing of braces in the nesting template. If the nested template is substituted, however, the substitution is processed first, and this will change how braces are parsed in the nesting template. This has little practical use, but can occasionally introduce unexpected errors.
See the and for more information. These pages also contain information on unusual calls such as {{{1|default}}}.
Perhaps the most common issue with the use of these blocks is unwanted spaces or lines. It is important to remember that the effect of these tags ends immediately after the last angle bracket, not on the next line or with the next visible character.
These tags can be nested inside each other, though (for a given page) this really only applies to the tag; nesting and tags is fairly pointless. Be careful not to split the tags, however. Constructions like abcdefghi will not work as expected. Use the "first opened, last closed" rule that is standard for XML.
Documentation for users, together with the template's categories and , is normally placed after the template code, inside "noinclude" tags. It is normally necessary to put the opening "noinclude" tag immediately after the end of the code, with no intervening spaces or newlines, to avoid transcluding unwanted whitespace.
In the case of complex templates, the documentation (together with categories and wikilinks) is often kept on a separate of the template page (named "Template:XXX/doc"). This also applies to many templates (to allow the information to be edited by non-administrators). This is achieved by placing the template after the main template code (within "noinclude" tags). If the "/doc" subpage does not exist, a link will then appear enabling it to be created.
Some templates contain category definitions in their transcluded code, i.e. they are intended to place the target pages in particular categories. This is often done with maintenance categories (placing articles into ordinary content categories in this way is discouraged). When doing this, it may be necessary to use "includeonly" tags to keep the template itself out of the category. The template should also be designed to suppress categorization in the case of demonstration uses – see .
It's good to prepare template aliases which only differ in whitespaces and capitalization. For example there's a template . The "W" is capital, since the word "Wiktionary" is so. But a redirect with lower "w" exists because users are likely to use the latter.
The example shows that template expansion is using 63k out of 2M of available memory.
Display problem. If too many templates are included on a page, the post-expand include size may exceed the limit. When this happens, templates after the limit will no longer expand and will instead display as a wikilink (for example, ). Common causes are the inclusion of too many citation templates and/or flag templates. To resolve this problem substitute templates, remove templates, or split the page.
|
|