Changes
→Use of templates across projects: use plainlinks because wikia interwiki is wrong
{{H:h|editor toc}}
In MediaWiki, a '''template''' is a page which can be inserted into another page via a process called [[w:transclusion|transclusion]]. It is a wiki [[w:en:subroutine|subroutine]] or [[w:en:HTML_element#Other_containers|iframe]] facility and is comparable to a #include statement or [[wikt:en:macro#Noun|macro]] that is expanded at page view time. [[Help:Substitution|Substitution]] allows templates to be used as a macro facility.
This page and its extension '''[[Help:Advanced templates]]''' provide a reference manual on templates in MediaWiki. For an introduction, see ''[[Help:A quick guide to templates|A quick guide to templates]]'' and [[mw:Help:Templates]].
'''Note:''' The master version of this page is the page [[m:Help:Template]] on Meta-Wikipedia; template demos work there, but may not work if you are viewing a copy of this page on another project.
==General==
The '''template namespace''' is the [[Help:Namespace|namespace]] with prefix "'''Template:'''" (this prefix can be used in all languages, but the canonical prefixes in languages other than English include Vorlage, Modèle, Sjabloon, with colon; the site WikiFAQ calls it Snippet).
A page in this namespace is called a '''template'''. The contents of such a page (either fixed or depending on [[w:en:Parameter (computer science)|parameters]] and/or [[Help:Variable|variable]]s) is designed to be useful for insertion in other pages ([[en:transclusion|transclusion]], creating a [[w:compound document|compound document]]).
The syntax for insertion of the page "Template:''name''" is <nowiki>{{</nowiki>''name''<nowiki>}}</nowiki>. This is called a template tag.
'''Example:''' <nowiki>abc{{tc}}def</nowiki>
{{ut1|tc}}
'''Gives:''' abc{{tc}}def.
This reference in the wikitext causes insertion of the template content when the referring page is rendered. Various equivalent terms are:
*[[w:call|call]]ing the template
*including the template
*transcluding the template
*using the template
Depending on context "including" can be used as opposed to ''substituting'' the template.
To create a hyperlink to a template page, use the usual wikilink syntax <nowiki>[[</nowiki>Template:''name'']].
If the page "Template:''name''" does not exist, then <nowiki>{{</nowiki>''<nowiki>name</nowiki>''<nowiki>}}</nowiki> works as <nowiki>[[</nowiki>Template:''name''<nowiki>]]</nowiki>, a link to a non-existing page, leading to the edit page. Thus, one way of making a template is putting the tag first, and then following the link.
If ''name'' starts with a valid namespace prefix, or a colon, then it is not prefixed with "Template:". This way any page can be used as template (a colon at the start refers to the main namespace).
Also it provides an alternative way of calling a template, which can be used in the case of a name conflict with a variable, e.g. [[Template:Ns:3]] and [[Template:PAGENAME]].
Using a page outside the template namespace as template can be useful for:
*quoting interface messages in the MediaWiki namespace
*templates for personal use in subpages of one's userpage, e.g. for experimenting
*inclusion of pages in other pages in the same namespace, e.g. having an extra page associated with each page, such as a to-do list, with a simple name correspondence; it can be created and included with <nowiki>{{{{NAMESPACE}}:{{PAGENAME}}/todo}}</nowiki>.
As usual the template name is case sensitive except (on most projects) for the first letter, and a blank space is equivalent with an underscore (as opposed to parameter names, see below: they are case-sensitive, even with respect to the first letter, and spaces are distinguished from underscores).
One can call a template that calls another template. If a template calls itself directly or indirectly, inclusion works only on one level for each template. However, with templates redirecting to the template that one would want to call recursively, one can achieve recursion without having to make copies of the whole template content, with the number of levels limited by the number of redirects. See also [[Help:Template#Repetition_within_a_page|Repetition within a page]] and {{tim|List of template calls}}.
A [[Help:Variable|variable]] in a template is evaluated after insertion in the referring page, i.e. if <nowiki>{{PAGENAME}}</nowiki> occurs in the wikitext of a template, it is rendered as the name of the referring page, not the name of the template.
A list of templates used on a page, all with links, is given on the edit page. Notes:
*even on a section edit page the whole list is given, i.e. also the templates used in other sections
*even when editing an old version of a page, the list is given for the current version
*templates for which the tags have not been saved yet are not shown, even on the preview page
(''Supported in version 1.4 and higher.'')
When saving a page without newline at the end, and going to the edit page again, there is a newline at the end. However, this does not seem to have effect, see the example above and the following example:
<pre>{{Numbered list demo}}{{Numbered list demo}}
{{Numbered list demo}}</pre>
using [[Template:Numbered list demo]], gives
{{Numbered list demo}}{{Numbered list demo}}
{{Numbered list demo}}
In a template name the character "#" and any characters after that are ignored: <nowiki>{{tc#abc}}</nowiki> gives {{tc#abc}}. This is used in {{tiw|en|hif}}.
==Use of templates across projects==
A template only works when referred to from a page in the same project. To use it in another project, one has to copy it to there. [[Bugzilla:1126]] is a request to lift that restriction, i.e. allow interwiki use of templates.
The adjustments needed in the copy, to work on the other project, can be reduced by using <nowiki>{{</nowiki>SERVER}}, localurl, and generic namespace names, and writing links in a way that works on multiple projects, e.g. [[m:Help:Table]]. For copying multiple templates, [[Help:Export|export]] and [[Help:Import|import]] can be helpful. It is also convenient if the template names can be the same on the other project. Therefore, when choosing template names, check which names are in use on other projects to which people might want to copy the templates. Note that if a template is referred to by something like abc<nowiki>{{</nowiki>NAMESPACE}}, a project with different namespace names requires a different template name, or a redirect.
Transclusion across projects, hence changing contents from a central place, is only possible:
*for content in the form of an image on Commons, to any page,
*for editable content on an image page on Commons, including content transcluded from other pages on Commons, to the corresponding local image pages, if there is no local image with the same name
Templates on the central Wikia work in the separate Wikia projects, provided that the prefix "wikia:" is used, see <span class="plainlinks">[http://www.wikia.com/wiki/Help:Shared_templates shared templates on wikia]</span>. (Please note that these external Wikia projects are NOT hosted and managed by the Wikimedia Foundation.)
==Parameters==
Template parameters are either named or numbered. They are [[w:en:Parameter (computer science)#Calling conventions|called-by-value]], and therefore input parameters only.
In the template ''definition'', the formal parameter (the [[w:en:Free variables and bound variables|placeholder]] for the parameter value) is a parameter name with three pairs of braces. So, for example, you would write {{{myVariableName}}} for a template parameter which you wanted to call ''myVariableName''.
{{MW 1.6|(for parameters defaults)}}
In the template ''call'', the syntax is either
*<tt><nowiki>{{templatename|parname1=parvalue1|parname2=parvalue2}}</nowiki></tt> with tags <tt><nowiki>{{{parname1|default}}}</nowiki></tt>, <tt><nowiki>{{{parname2|default}}}</nowiki></tt>, etc. in the template, or
*<tt><nowiki>{{templatename|parvalue1|parvalue2}}</nowiki></tt> with tags <tt><nowiki>{{{1|default}}}</nowiki></tt>, <tt><nowiki>{{{2|default}}}</nowiki></tt>, etc. in the template.
The default (optional, together with the pipe character in front) can be different for every occurrence, and applies if no value is specified when calling the template, not to be confused with the case that the empty value is specified.
A way to determine whether a parameter is defined is by comparing the results of two occurrences with different defaults, e.g. with <tt><nowiki>{{#ifeq: {{{a|+}}}|{{{a|-}}}|1|0}}</nowiki></tt>.
In the case of [[Help:Substitution|page substitution]] with an undefined parameter, not the default but the parameter itself with default is substituted into the wikitext.
===Example: named parameter substitution ===
Let us create a template called '''Name_example''' (that is, the template will be '''[[Template:Name_example]]'''), with a parameter for the first name which we will call ''firstName'', and a parameter for the last name which we will call ''lastName''.<br>
If we call the template ''without'' any parameters, i.e. if we write <nowiki>{{Name_example}}</nowiki>, then we will get:<br>
{{Name_example}}
However, now if we call it and pass parameters to it as follows:
<nowiki>{{Name_example | firstName=John | lastName=Smith}}</nowiki> the output will look like this:<br>
{{Name_example | firstName=John | lastName=Smith}}
===Example: numeric parameter substitution===
An example of how the contents of a parameter function.
====The simplest case====
Let us first ''define'' a template with the name ''t0'', which will
*expect a single argument
*give back the text of that argument, with no spaces before or after it, between "start-" and "-end":
:'''<nowiki>start-{{{1}}}-end</nowiki>'''
the definition of course being on page '''[[Template:t0]]'''.
#Calling the template named ''t0'' with a parameter value of "''a''", i.e.,
#::<nowiki>{{t0|a}}</nowiki>
#:gives
#::"{{t0|a}}"
#Calling the template named ''t0'' with a parameter which is blank, i.e.,
#::<nowiki>{{t0| }}</nowiki>
#:gives
#::"{{t0| }}" .
#Calling the template named ''t0'' with a null parameter, i.e.,
#::<nowiki>{{t0|}}</nowiki>
#:gives
#::"{{t0|}}".
#Calling the template named ''t0'' and not passing any parameters, i.e.,
#::<nowiki>{{t0}}</nowiki>
#:gives
#::"{{t0}}".
If something like '''{{{1}}}''' or in fact any '''{{{name}}}''' is visible on an ordinary page it typically indicates that a mandatory parameter of a template used on this page isn't defined.
====Declaring a default value====
Now let us ''define'' a similar template with the name ''t'' which contains a single numbered parameter 1 with a default of ''pqr''. The only difference between the effects of t and t0 appears when they are called with no parameter (and no "|"):
:'''<nowiki>start-{{{1|pqr}}}-end</nowiki>'''<br>
the definition of course being on page '''[[Template:t]]'''.<br>
#Calling the template named ''t'' with a parameter value of "''a''", i.e.,
#::<nowiki>{{t|a}}</nowiki>
#:gives
#::"{{t|a}}".
#Calling the template named ''t'' with a parameter which is blank, i.e.,
#::<nowiki>{{t| }}</nowiki>
#:gives
#::"{{t| }}".
#Calling the template named ''t'' with a null parameter, i.e.,
#::<nowiki>{{t|}}</nowiki>
#:gives
#::"{{t|}}".
#Calling the template named ''t'' with named parameter '''1=''', i.e.,
#::<nowiki>{{t|1=no surprise}}</nowiki>
#:gives
#::"{{t|1=no surprise}}".
#Calling the template named ''t'' with '''1=''' after an unnamed parameter, i.e.,
#::<nowiki>{{t|no|1=surprise}}</nowiki>
#:gives
#::"{{t|no|1=surprise}}".
#Calling the template named ''t'' with '''1=''' before an unnamed parameter, i.e.,
#::<nowiki>{{t|1=no|surprise}}</nowiki>
#:gives
#::"{{t|1=no|surprise}}".
#Calling the template named ''t'' and no parameter at all, i.e.,
#::<nowiki>{{t}}</nowiki>
#:gives
#::"{{t}}".
#Calling the template named ''t'' and no named or unnamed parameter '''1''', i.e.,
#::<nowiki>{{t|2=two}}</nowiki>
#:gives
#::"{{t|2=two}}".
===Template mechanism===
The template mechanism involves the following steps:
*In template calls, the template name, parameter names, and parameter values depending on variables are evaluated; the result of this step can be viewed by applying subst (possibly multiple times), saving and editing (see below). In the case of a variable depending on a variable, this may have to be done a second time.
*the values are assigned to the parameters from left to right (the result can not be directly viewed), meaning that a named or numbered parameter may be specified several times, the last one being the actual value that will be kept when passing the parameter during the final instantiation of the template below.
*template calls are replaced by template content, where the parameter tags with matching names are replaced by their values or defaults (the result of this step can be viewed by applying subst either again, or directly to the template and the variables).
The process is applied recursively until there are no more template calls to replace.
All whitespaces before and after a parameter value are usually ignored.
:<nowiki>"{{t| a}}"</nowiki> gives "{{t|a}}".
:<nowiki>"{{t|a }}"</nowiki> gives "{{t|a}}".
A parameter value can contain a piped link; the "|" in such a link is not taken as parameter separator.
:Example: <nowiki>"{{t|[[a|b]]}}"</nowiki> gives "{{t|[[a|b]]}}".
:Example: <nowiki>"{{t|[[a|b]] }}"</nowiki> gives "{{t|[[a|b]] }}".
(In this case the space ''has'' effect.)
On rendering, surplus parameter values (including named parameters that do not occur in the template) are ignored: <nowiki>{{t|a|b}}</nowiki> gives {{t|a|b}}.
Parameters without names (i.e. that don't contain an equal sign) are numbered implicitly starting by 1, from left to right.
:Example: <nowiki>"{{t|a|b|c}}"</nowiki> is equivalent to <nowiki>"{{t|1=a|2=b|3=c}}"</nowiki>
Numbered parameters can also be skipped or can be specified in alternate order by numbering them explicitly.
:Example: <nowiki>"{{t|3=c|2=b|1=a}}"</nowiki> is equivalent to <nowiki>"{{t|1=a|2=b|3=c}}"</nowiki>
:Example: <nowiki>"{{t|a|b|c|10=j}}"</nowiki> is equivalent to <nowiki>"{{t|1=a|2=b|3=c|10=j}}"</nowiki>
This also makes it possible to apply a default value for parameter 1, while specifying a value for parameter 2:
<nowiki>{{t2d|2=a}}</nowiki>, using {{timc|t2d}}, gives {{t2d|2=a}}.
Template calls can contain multiple occurrences of the same named parameter, and a numbered parameter can be specified multiple times by using its number explicitly. The effective value of parameters is the value of the last occurrence (from left to right) of the parameter with the same name, once the parameter names have been evaluated.
:Example: <nowiki>"{{t|a|b|c|2=B}}"</nowiki> is equivalent to <nowiki>"{{t|a|B|c}}"</nowiki>
The final list of formal parameters before the template itself is instantiated is then unordered because the list is indexed by the evaluated parameter named (this list is an [[:en:Associative array|associative array]]).
A useful example that uses of multiple assignments of parameters with the same name (and thus allowing parameter overrides) ''was'' performed in the [[:en:Template:If defined]] template used for example in English Wikipedia (and many other localized editions). This template, though, was deleted.
See also [[Help:Parameter default]].
==Restrictions on parameter values==
* If a parameter value contains an equals sign, the parameter name must be put explicitly, even when it is 1, 2, 3, etc.
* If the expression for a parameter value contains two consecutive closing braces which are not part of a variable or parameter tag, they have to be put in nowiki tags.
* If the expression for a parameter value contains a pipe character (|) which is not part of a piped link or within an image tag, it has to be put in nowiki tags: <nowiki><nowiki>|</nowiki></nowiki>.
* The expression for a parameter value cannot contain the variable localurl with query string, due to the pipe character.
*A parameter value can be quite long, see [[Help:Long parameter demo]].
*A parameter value can contain pairs of square brackets or text to be used within a link, but not a closing part of one link and an opening part of another link: <nowiki>{{</nowiki>tlis|abc]]def[[ghi}} using {{timc|tlis}} gives {{tlis|abc]]def[[ghi}}
==Mix of named and unnamed parameters==
In the case of a mix of named and unnamed parameters in a template tag, the unnamed parameters are numbered 1,2,3,.., so they are ''not'' numbered according to the position in the mixed list as a whole.
For example, <nowiki>{{</nowiki>t sup|3=1|2|1=3|4|5|6|7}} using {{tcw|t sup}} gives {{t sup|3=1|2|1=3|4|5|6|7}}.
== Template tag lay-out ==
To have a desired template tag lay-out, especially in the case of many parameters, additional dummy parameters can be used with a newline, spaces, and/or comments. In the case of unnamed parameters, this means that some parameter numbers are not used in the template content, so that their values, present in the template tag, do not affect the rendering of the template. For example, using {{ut|t3d}},
<pre>
{{t3d |a|b|c| 1
|d|e|f| 2
|g|h|i| 3
}}
</pre>
gives:
{{t3d |a|b|c| 1
|d|e|f| 2
|g|h|i| 3
}}
If parameters are named, dummy parameters can easily be inserted at any time. If they are unnamed, then, to avoid renumbering, one can insert named parameters: any text including an equals sign will do, if the text on the left is not one of the parameter numbers: this text is interpreted as the name of an unused parameter, hence ignored.
Example:
Using {{tim|Chess position}}
<pre>
{{chess position|=
8 |rd|nd|bd|qd|kd|bd|nd|rd|=
7 | |pd|pd|pd|pd|pd|pd|pd|=
6 |pd| | | | | | | |=
5 | | | | | | | | |=
4 | |pl| | | | |pl| |=
3 | | | | | | | |bl|=
2 |pl| |pl|pl|pl|pl| |pl|=
1 |rl|nl|bl|ql|kl| |nl|rl|=
a b c d e f g h
|30}}
</pre>
which gives
{{chess position|=
8 |rd|nd|bd|qd|kd|bd|nd|rd|=
7 | |pd|pd|pd|pd|pd|pd|pd|=
6 |pd| | | | | | | |=
5 | | | | | | | | |=
4 | |pl| | | | |pl| |=
3 | | | | | | | |bl|=
2 |pl| |pl|pl|pl|pl| |pl|=
1 |rl|nl|bl|ql|kl| |nl|rl|=
a b c d e f g h
|30}}
In this example, the 1 to 8 row numbers and a to h column numbers (as well as their surrounding spaces and newlines) in the parameter list are hidden within a dummy parameter (with an empty name before the equal sign).
The template generates a HTML table where each cell contains an image whose name is dynamically generated from the 64 first automatically numbered parameters (there are intermediate parameters within the numbered list, but as these dummy parameters are named explicitly, they don't increment the parameter number used to generate default parameter names. So the parameter with autogenerated name "1" effectively contains the "rd" value).
The template is then called with a parameter list that contains:
* 65 parameters automatically named from "1" to "65" (64 parameters for the cells content, and the 65th for the image width),
* and a dummy named parameter (whose name is an empty string, and whose effective value results from the last assignment with the chessboard column names a to h and the surrounding spaces and newlines), which is not used in the internal template content.
Note also that all parameters are trimmed, so the parameter with autogenerated name "9" (at position a7 on the chessboard above) is assigned the empty string value.
==Template page==
Without noinclude and includeonly parts the following are identical:
*the rendering of the wikitext of the template (this is on the template page, and would be the same in other pages with that wikitext, except for page-dependent variables)
*the rendering of the template on a page that calls it without parameters
Thus the defaults are shown, e.g. {{timc|T 1}} shows the same as <nowiki>{{</nowiki>t 1}}: "{{t 1}}".
===Template page if no defaults have been specified===
In simple cases this corresponds to treating the parameter tags as ordinary text, for example:
<nowiki>"{{t2}}"</nowiki> (using [[Template:t2]] containing {{msgnw:t2}}) gives {{t2}}
Similarly, if part of the parameters is undefined, only those show up as {{{''parameter number or name''}}}.
However, some templates produce an error message when called without parameters, so unless includeonly is used (see below) the template page also shows this message, suggesting that there is something wrong with the template. For example, a template that doubles parameter {{{1}}} with the wikitext <nowiki>{{#expr:2*{{{1}}}}}</nowiki> gives {{#expr:2*{{{1}}}}}. In such cases includeonly can be used to avoid uninformative and messy rendering on the template page, but either way a disadvantage during template development is that preview does not (meaningfully) reflect changes in the parts of the page outside the noinclude tags (i.e. the included parts). Therefore it can be useful to provide representative default values, even if that is only done temporarily during development of the template.
Note also that variable tags are not shown, but evaluated.
With noinclude an informative template page can be produced, containing more demos and test cases than just the single default set. Typically, the noinclude-part of a template page contains examples including or [[Help:substitution|substituting]] the template. Changes in the working of the template (i.e. changes outside the noinclude-part) are not yet effective in these examples in preview and, in the case of [[Help:substitution|substitution]], in "show changes".
However, they are reflected in the rendered page after saving.
Some formatting aspects may show up which are not readily seen from the edit box.
==A parameter value depending on parameters==
As mentioned above, if a parameter is left undefined, the triple braced parameter name in the result does not function as a parameter if the page is called as a template from another page; if it should, do not omit the parameter value but specify it in terms of a parameter of the calling page; compare:
*<nowiki>{{t2a|b}}</nowiki> (using [[Template:t2a]] containing {{msgnw:t2a}}) gives {{t2a|b}}
*<nowiki>{{t2a1|b}}</nowiki> (using [[Template:t2a1]] containing {{msgnw:t2a1}}) gives {{t2a1|b}}.
*Similarly, <nowiki>{{t2a2|b}}</nowiki> (using [[Template:t2a2]] containing {{msgnw:t2a2}}) gives {{t2a2|b}}. See also below.
The most straightforward way of expressing a parameter value in terms of a parameter is using the same name and making it equal, e.g. <nowiki>{{</nowiki>Lan is|lr=af|le={{{le}}}}} in [[Template:Lan is mr]].
==Empty vs. undefined==
<nowiki>{{t2||a}}</nowiki> makes the first parameter equal to the empty string rather than leaving it undefined: it gives {{t2||a}}. By treating the second unnamed parameter as a parameter with the name "2", the first unnamed parameter can be left undefined.
:Example: <nowiki>{{t2|2=a}}</nowiki> gives {{t2|2=a}}.
If the parameter is used in a tag like <nowiki><font size></nowiki> the default text may give invalid code that is conveniently ignored, rather than resulting in perhaps ugly code being rendered (but this may depend on the browser). See e.g. [[Template talk:Fontsize]].
Parameter names (as opposed to template names) are case-sensitive, even with respect to the first letter, and spaces are distinguished from underscores. The [[w:en:String_%28computer_science%29#Formal_theory|empty string]] is also a valid parameter name, see [[Template:T empty string as parameter name]].
Parameter 02 is distinguished from parameter 2: using {{timc|t pl0}}, <nowiki>{{</nowiki>t pl0|5|6|7}} gives {{t pl0|5|6|7}}
Note that parameters in the [[MediaWiki namespace]] (interface messages) are written differently: as $1, $2, etc., see [[Meta-Wikimedia:MediaWiki namespace text]].
==Equals sign in parameter value==
When a parameter value contains an equals sign, the parameter name must be stated explicitly, otherwise the part of the parameter value before the equals sign is taken as the parameter name. If that happens accidentally, this parameter is likely to be unused, hence harmless, but the intended implicit parameter 1, 2, or 3, etc., gets no value.
:Example: <nowiki>{{TTT|a=b|c}}</nowiki> {{ut1|TTT}} gives {{TTT|a=b|c}} (unused parameter a is assigned the value b, the first parameter value for which no parameter name is specified is assigned to the parameter with the implicit name "1" — it reads like so: Use template TTT, assign "b" to the parameter "a", use the value "c" for your first ''implicit'' parameter <nowiki>{{{1}}}</nowiki>.), while <nowiki>{{TTT|1=a=b|2=c}}</nowiki> gives {{TTT|1=a=b|2=c}} (the first parameter is assigned the value "a=b" — it reads like so: Use template TTT, assign "a=b" to the ''expressed'' parameter "1", assign "c" to the ''expressed'' parameter "2"). In this case, if template TTT had a parameter <nowiki>{{{3}}}, {{TTT|1=a=b|2=c|d}}</nowiki>, "d" would be assigned to "3" as first ''implicit'' parameter without a value.
The same applies if a parameter value depends on a parameter, and ''may'' therefore contain an equals sign. Compare:
*<nowiki>{{t2a1|1=x=y}}</nowiki> (using [[Template:t2a1]] containing {{msgnw:t2a1}}) gives {{t2a1|1=x=y}}.
*<nowiki>{{t2a2|1=x=y}}</nowiki> (using [[Template:t2a2]] containing {{msgnw:t2a2}}) gives {{t2a2|1=x=y}}.
==msgnw==
The prefix '''msgnw''' (short for "message, nowiki") is used to display the uninterpreted wikitext of a template (the same thing you'd see if you were editing the page) on another page. An exception is that numbered and unnumbered lists are rendered as such instead of displaying # or * signs. Parameter values are ignored.
With the introduction of noinclude and includeonly tags, allowing template pages to take care of definition as well as displaying information, msgnw displays no longer just the technical information about what the template does when included, but a mix of that with wikitext for rendering the template page itself, with the tags. This limits the usefulness of applying this.
Examples:
[[Template:en]]
*<nowiki>{{msgnw:en}}</nowiki> gives {{msgnw:en}}
*<nowiki>{{en}}</nowiki> gives {{en}}
{{tim|t}}
<nowiki>{{msgnw:t|a}}</nowiki> gives
*{{msgnw:t|a}}
<br />...which is much more than just the formal info about what the template does when included:
*<nowiki>start-{{{1|pqr}}}-end</nowiki>
[[Template:Numbered list demo]]
<nowiki>{{msgnw:Numbered list demo}}</nowiki> gives:
{{msgnw:Numbered list demo}}
<nowiki>{{Numbered list demo}}</nowiki> gives:
{{Numbered list demo}}
The edit box for [[Template:Numbered list demo]] shows:
<pre>
#Amsterdam
#Rotterdam
#The Hague
</pre>
==Usage==
Templates are useful for any text for which one wants a copy in two or more pages, and there is no need for each copy to be edited independently to adapt it to the page it is in. Since parameters can be used, versions may to that extent even be different, and parameter values can be edited independently for each. Templates are not only convenient, but they can also "force" a useful uniformity. See also [[w:en:Subroutine#Advantages_of_subprograms|advantages of subprograms]].
Typical applications are:
*[[w:Wikipedia:Template messages|template messages]] such as [[w:en:Template:stub]]
*[[w:Wikipedia:Navigational templates|navigational templates]] such as [[w:en:Template:europe]]
*[[w:Wikipedia:Infobox|infoboxes]], each time showing the same kind of information in the same format, but each time for a different case, using parameters, e.g. the [[w:en:Template:Infobox Countries]].
*provide cross-language portability of texts which are largely internationally the same, but contain some standard terms different in each language; the standard terms are replaced by {{..}} referring to template pages which have the same name but localised content in each language. This is [[m:Using templates in Wiktionary|applied in Wiktionaries]] for names of languages, terms like "noun", etc.; it is also [[w:Wikipedia:Template messages#Taxobox_components_.28Taxons.29|applied for taxoboxes]], see e.g. [[w:en:Template:Familia]] and [[w:fr:Modèle:Familia]].
*allow a composite image, composed of a table of images, to be specified by a template tag which itself has a corresponding table-like lay-out, see [[w:Template:Chess position]] and [[w:Template:Game of Go Position]] (the latter example also shows that the number of parameters a template can have is quite large, in this case 362). Newlines in parameter values provide the lay-out of the template tag, but they are not allowed in image names, therefore they are put within comment tags; alternatively, dummy parameters could be used on these positions.
Various combinations are possible. E.g., the last two could be combined, so that e.g. every language version of Wikipedia has the same Infobox Countries template (for each project a copy, because the template mechanism can not import across projects), which has parameters for the data, and refers to templates with translations of standard terms.
See also '''[[Help:Advanced templates]]'''.
===Composite pages===
The wikitext of a page may (partly or fully) consist of tags for the inclusion of component pages. The "templates" are not necessarily in the template namespace, and may be more or less self-contained pages by themselves. An advantage of putting the components in the template namespace is that they are then included in the list of templates used on the composite page, all with links, on its edit page. On the other hand, the component page may, by itself, be of a nature that it belongs in e.g. the main namespace, while the prefix would suggest otherwise; also the prefix would clutter the pagename.
Examples are:
*[[Help:A simple composite example]]
*[[w:Wikipedia:Village pump]] with six component pages, e.g. [[w:Wikipedia:Village pump (technical)]]
*daily a page like [[w:en:Wikipedia:Votes for deletion/Log/2005 May 31]] where each component page consists of the discussion on the deletion of one Wikipedia page, e.g. [[w:en:Wikipedia:Votes for deletion/Sp3tt]] (for this day there are 75 of such component pages)
*[[m:Meta:Translation/Coordination]], mainly containing
**[[m:Meta:Translation/Coordination/List/Meta]]
**[[m:Meta:Translation/Coordination/List/Main]]
*[http://en.wikipedia.org/w/index.php?title=2005_English_cricket_season_%281-14_June%29&diff=27917447&oldid=27330886] - rejection of composite page by w:en
This allows the user the choice between viewing the component pages separately or combinedly. One may want to view a page separately if one has a slow connection; also sometimes people have sentimental reasons for having a separate page about a topic, e.g. a village. Viewing the combined page is much more convenient if there are many small component pages; even if a subbtopic page does not provide additional info, if it exists anyway, and is linked to, it is useful to display the content (making clear that the whole content is shown), it saves the trouble of checking the contents by following the link.
In the latter case the search function of the browser can be used for searching in the combined page. Similar to the section editing feature, it allows editing a component page; however, there is no possibility to have the combined wikitext in the edit box.
Edit history, recent changes, watching pages, and "what links here" work separately for the component pages and the ''composition'' page, not for the ''composite'' page. Related changes works for the component pages and for the composite page. Page protection can be set for selected component pages and for the composition page, not directly for the composite page.
The talk page of a composition page is used to talk about the composition and the page in general, and could in addition be a composite page of the talk pages of the component pages.
A composite page has an integrated TOC and section numbering; the numbering is not reset for each component page.
Editing a section of a component page can be done directly from the composite page, see [[Help:Section#Editing sections of included templates|editing sections of included templates]]. After saving, one ends up at the page for the component page to which the section belongs.
On projects with the [[Help:Interwiki linking#Interlanguage_link_.28software_feature.29|interlanguage link feature]] the composite page shows the combined interlanguage links of all component pages, hence possibly multiple links for one language or even for one page.
For easy access to the component pages, links to them on the composite page (for example by self-links on the component pages) are convenient. Alternatively, access is through section editing, or, if the component pages are in the template namespace, through the list of templates called from the composite page, at its edit page.
Conversely, for easy access from a component page to the composite page(s) that call(s) it (if there are not too many of them) it is convenient to link the component page to them (on a composite page one becomes a self-link).
See also [[w:Wikipedia talk:Template namespace#transcluding prose]].
===Pages with a common section===
A section on the relationship of the subjects A and B, or a subtopic equally relevant for A as for B, can be put both in page A and in page B, by making it a separate page C, called as a template from A and B.
Remarks:
*For readers reading both A and B, it is useful that the duplication is indicated by a message or a special lay-out; otherwise it is confusing and inconvenient. One can for example use {{tim|cs}}: {{cs|Help:Template}}
*After saving one ends up viewing the template page. For easy access to the calling pages it is convenient to link the called page to them (on a calling page one becomes a self-link). This may well be integrated in the message about the duplication. It is even more convenient than the two-step link as shown above.
*The section level of the embedded section(s) has to be the same in each embedding page. The same applies to lay-out and style.
*Pages like C can be put in
**the same namespace as A and B; advantage:
***the kind of content of C is the same as that of A and B
**the template namespace; advantages:
***C appears in the list of templates called from A and B, at their edit pages
***C by itself may not satisfy the standards for pages in the namespace of A and B, such as providing context (however, the message about the duplication explains it)
Examples:
*{{pim|Help|Editing sections of included templates}}
*{{pim|Help|Alphabetic order}}
For various remarks which also apply here, see also the previous section.
===Repetition within a page===
For a succession of similar pieces of content, as in a list, a computer program would use a [[w:en:Conditional_branching#Loops|loop]]. What comes closest in MediaWiki is putting the loop body in a template, and calling the template repeatedly, usually with a varying parameter value. This is somewhat primitive for a loop:
*instead of "for i=1 to 5" we need to enumerate the values 1, 2, 3, 4, 5; on the other hand, this gives the versatility to use just as easily less regular values such as de, en, fr ([[w:en:Conditional_branching#Collection-controlled_loops|collection-controlled loop]]).
*repetition of code is minimized by the template, but the template call itself has to be repeated each time (but see also the next section)
Example:
*[[Template:List of Languages]] - the "loop body" is in [[Template:Lang def]]; compared with putting the content of the latter directly in the list the advantage is that the variable part (the value of "code") occurs once each time in the code, although it is used twice. This is important because the fixed part of the list item code can be copied while the variable part is typically inserted manually.
For repetition of the same text the loop body can be a parameter, e.g. [[Template:X5]] (see [http://meta.wikimedia.org/w/index.php?title=Special%3AAllpages&from=X&namespace=10 list]).
A double loop is achieved by a second template that repeatedly calls the first. Similarly for a triple loop, etc. See e.g. [[w:Template:Ld]], [[w:Template:L2d]], and [[w:Template:L3d]].
An example where the same template is used for different loop levels is [[Template:Loop 2]]. Because the software has a built-in protection against infinite nesting (if there is, there will be an error e.g. <code>Fatal error: Maximum execution time of 30 seconds exceeded in /w/includes/Parser.php on line 775</code>), a redirect is used for each level. The number of levels that the template can handle is determined by the number of parameters specified in the two template calls it comprises (because going up a level requires an extra parameter) and the number of existing redirects.
Similarly [[Template:Lanlp 1]] is used for both the inner and the outer of a double loop. Adding an item (here a language) adds to each row and also adds an extra row.
'''Providing effective repetition without repetition of similar template calls in the wikitext'''
A technique for repetition of similar template calls without actually putting this list in the wikitext is demonstrated in {{tim|foreach}} and {{tiw|en|foreach}} (on which the version on Meta is based; it is similar but different on details), and a more primitive method described below.
Example {{ut1|t2}}:
<nowiki>{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}</nowiki> gives:
{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}
Description of the old {{tim|List of template calls}}:
For an arbitrary template with up to three nameless parameters, it is equivalent to calling the template repeatedly, with the first parameter varying, and the possible other parameters fixed: one specifies the name, the values of the fixed parameters, and the list of values the first parameter should have in consecutive template calls, with a sequential number each, and ending with "end". The technique requires redirects like [[Template:List of template calls 3]], as many as the maximum number of repetitions.
If e.g. the second parameter varies, or two parameters, then a similar template can be made, but each version requires its own set of redirects.
Example {{ut1|t2}}:
<nowiki>{{List of template calls|t2|constant||abc|1|def|2|ghi|end|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}}</nowiki>
gives:
{{List of template calls|t2|constant||abc|1|def|2|ghi|end|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}}
Compare <nowiki>{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}</nowiki> giving:
{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}
The empty parameters are needed in accordance with the maximum number of parameters the repeated template can have (currently three) and the maximum number of repetitions. Putting more empty parameters than needed at the end does not matter, but the number of empty parameters in the beginning of the list should be correct.
==Internal links==
In the case of an internal link in a template with the target depending on a parameter, and in the case of a link with the target depending on a template, existence detection works as usual.
Examples:
*<nowiki>{{tli|insert in target|insert in label}}</nowiki> {{ut1|tli}} gives {{tli|insert in target|insert in label}}
*<nowiki>[[a{{tciit}}b|c{{tciil}}d]]</nowiki> {{ut2|tciit|tciil}} gives [[a{{tciit}}b|c{{tciil}}d]]
*<nowiki>{{tli|insert also in target|insert in label}}</nowiki> (using [[Template:tli]]) gives {{tli|insert also in target|insert in label}} {{ne}}
*<nowiki>[[a{{tciit}}e|c{{tciil}}d]]</nowiki> (using [[Template:tciit]] and [[Template:tciil]]) gives [[a{{tciit}}e|c{{tciil}}d]] {{ne}}
*<nowiki>{{la|Help:Tabl|tabl}}</nowiki> {{ut1|la}} gives: {{la|Help:Tabl|tabl}}
*[[Template:Pvn]]
==External links==
Examples:
*<nowiki>{{tle|insert in target|insert in label}}</nowiki> {{ut1|tle}}} gives {{tle|insert in target|insert in label}}
*<nowiki>{{tle2|insert in target}}</nowiki> {{ut1|tle2}}} gives "{{tle2|insert in target}}"; note that brackets separated from the http:// are not recognized as belonging together:
**<nowiki>[{{tle2|insert in target}} label]</nowiki> gives [{{tle2|insert in target}} label]
**<nowiki>[{{tle2|insert in target}}]</nowiki> gives [{{tle2|insert in target}}]
*<nowiki>{{tle3|insert in target}}</nowiki> {{ut1|tle3}}} gives {{tle3|insert in target}}
*<nowiki>[http://a{{tciit}}b c{{tciil}}d]</nowiki> gives [http://a{{tciit}}b c{{tciil}}d]
*<nowiki>http://a{{tciit}}b</nowiki> gives "http://a{{tciit}}b"
*<nowiki>[http://a{{tciit}}b]</nowiki> gives [http://a{{tciit}}b]
However, a URL can not be composed of:
*a first part in a parameter and a second part added by the template (although we have seen above that reversed it works: a second part in a parameter and a first part prefixed by the template)
*a first part in a template and a second part after the template (although we have seen above that reversed it works: the second part in a template and the first part before the template)
Example of first restriction:
*<nowiki>{{tae|http://www.}}</nowiki> {{ut1|tae}}} gives {{tae|http://www.}}
Examples of second restriction:
*<nowiki>[{{th}}add at the end]</nowiki> {{ut1|th}}} gives [{{th}}add at the end]
*"<nowiki>{{th}}add at the end</nowiki>" gives "{{th}}add at the end"
==Noinclude and includeonly==
This feature is not available before version 1.6.
Anything between <nowiki><noinclude> and </noinclude></nowiki> will be processed and displayed only when the page is being viewed directly; it will not be included or substituted.
Possible applications are:
# Categorising templates, see [[Help:template documentation|template documentation]].
# [[Help:Interwiki_linking|Interlanguage link]]s to similar templates in other languages.
# Pages in the [[Help:MediaWiki namespace|MediaWiki namespace]].
<nowiki>The converse is <includeonly>. Text between <includeonly> and
</includeonly></nowiki> will be processed and displayed only when the page is
being included. Applications include:
# Adding all pages containing a given template to a category.
# Avoiding messy rendering on the template page, e.g. {{evaldemo|#expr:{{{1}}}}}.
Note that spaces and newlines between the general content and the tagged part belong to the general content. If they are not desired the include tag should directly follow the content on the same line:
<noinclude>this is </noinclude>fine<includeonly>, closing tags are
</includeonly><noinclude>
less critical, but must be specified.</noinclude>
Attempts to nest split pairs of these tags, or the similar <tt><nowiki></tt> and <tt></nowiki></tt> pair, won't work as expected. If say <tt><nowiki></tt> begins within the general content,
or in a "noinclude" part, or in an "includeonly" part, then it also has to be closed within the same part.
The code <code><nowiki>~<includeonly>~</includeonly>~~</nowiki></code> will be displayed as <nowiki>~~~</nowiki> when the template is not included, <nowiki>~~~~</nowiki> when the template is included, and it will be expanded as the active user when the template is subst'd.
== Wiki markup at the begin of a template==
If the first included character of a template is one of the Wiki markup characters '''<tt>:;*#</tt>''', then it's interpreted as being at the begin of the line (even when the template call is not).
To avoid this effect use <tt><nowiki>#</nowiki></tt> or a [[w:en:Numeric character reference|NCR]] like say <tt>&#58;</tt> for a colon, this NCR is also useful in conjunction with definition [[Help:list|list]]s.
==Substitution==
:''Main article: '''[[Help:Substitution]]'''''
Putting "subst:" after the double opening braces causes an [[Help:Automatic conversion of wikitext|automatic conversion of wikitext]] when the referring page is saved: the subst tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters.
==Redirection==
When a page called for inclusion is a redirect page, the redirect target is included instead. As usual, a double redirect does not work.
A page that consists of nothing else than the inclusion of another page as a template, shows similarities to a redirect to that other page, but also many differences, including:
*the result has as header the name of the referring page
*there is no redirect message
*the buttons such as edit (for editing the whole page), watch, talk, history, "what links here", and "last modified", etc., refer to the referring page (to go to the target page, a section edit link can be used, from there the whole page can be accessed)
*the referring page is in the same categories as the target page
*"double redirects", with one or both being this kind of "pseudo-redirect", work.
Compare [[Help:Template namespace]] with [[Help:Template namespace - demo of alternative for redirect]].
In a page that in turn includes such a referring page there is no difference.
==Comparison of linking and embedding==
Linking and embedding have in common:
*if the page does not exist, a link to the edit page is shown; the links are identical if it is not a piped link; the link does not show parameters and does not show whether, after creation, the new page will be linked or embedded.
*the name can depend on variables and parameters
Note that embedding works for all pages where [[Help:Redirect|redirecting]] works, and it also fails for all pages where a redirect won't work.
==Predefined templates==
[[Help:Variable|Variable]]s, [[ParserFunctions|parser function]]s, and other "[[help:colon function|colon function]]s" can be considered predefined templates. They do not have a page that defines in terms of wikitext what they do, because their action is something extra that cannot be constructed from more basic wikitext functionality.
Some additional differences:
*Predefined templates with one or more parameters use ":" instead of the first "|".
*An edit page does not list predefined templates used on the page.
*No "What links here" is available for predefined templates, to find the pages where they are used.
*Apart from #switch, predefined templates don't allow you to define named parameters, and therefore equal signs generally have no special effect, comparison:
*:<tt>{{ #if: not blank | 2=abc }}</tt> gives '''{{ #if: not blank | 2=abc }}'''
*:<tt>{{ ifdef | not blank | 2=abc }}</tt> gives '''{{ ifdef | not blank | 2=abc }}'''
*It's however possible to corrupt the value shown for undefined parameters without default, for more examples see {{tim|ifeq demo}}:
*:<tt>{{ #if: {{ns:0}} | 2=xy | {{{2|blank}}} }}</tt> gives '''{{ #if: {{ns:0}} | 2=xy | {{{2|blank}}} }}'''
*:<tt>{{ #if: {{ns:0}} | 2=xy | {{{2 }}} }}</tt> gives '''{{ #if: {{ns:0}} | 2=xy | {{{2 }}} }}'''
*:<tt>{{ #if: {{ns:0}} | oops | {{{1|blank}}} }}</tt> gives '''{{ #if: {{ns:0}} | oops | {{{1|blank}}} }}'''
*:<tt>{{ #if: {{ns:0}} | oops | {{{1 }}} }}</tt> gives '''{{ #if: {{ns:0}} | oops | {{{1 }}} }}'''
Ordinary and predefined templates have in common that their parameter values can depend on ordinary and predefined templates. In the case of named parameters, the names used in the template call can also depend on ordinary and predefined templates. This applies also for the named parameters of #switch.
==Templates in different versions of MediaWiki==
===Version 1.6===
* More magic words and the [[Help:parameter default|parameter default]] mechanism were added.
* <tt><noinclude></tt> and <tt><includeonly></tt> (see above) were added.
===Version 1.5===
* More [[Help:magic words|magic words]] were added.
===Version 1.4===
*Including the same template more than five times in the same page works from version 1.4, now on all Wikimedia sites. Also new was that subst can be used with parameters.
===Version 1.3===
* See, among other things, [{{fullurl:m:Help:Template|oldid=86075}}#Default_values_of_parameters default values of parameters]. Starting with version 1.3 the prefix "msg:" is generally optional.
===Version 1.2.6===
* In MediaWiki version 1.2.6, make a page MediaWiki:mytemplate and refer to it with <nowiki>{{msg:mytemplate}}</nowiki>. In this version parameters are not possible.
* Compatibility with 1.3, in the sense of having the content in Template:mytemplate, is possible with a redirect from MediaWiki:mytemplate to Template:mytemplate on the 1.2.6 project.
== Revision history of pages containing templates ==
Pages in the stored [[Help:Page history|page history]] consist of wikitext with possible ''references'' to templates and images. When viewing an old version of a page, these refer to the current versions of the templates and images, if these still exist. Thus the former composite page is not reconstructed.
==See also==
*[[m:Help:Advanced templates|Help:Advanced templates]]<!-- m: not directly portable due to examples -->
*[[Help:Calculation]]<!-- maybe portable, test it -->
*[[Help:Colon function]]
*[[m:Help:Parameter default|Help:Parameter default]]<!-- m: -->
*[[Help:Magic words]]
*[[Help:Substitution]]<!-- maybe portable, test it -->
*[[Help:Template documentation]]
*[[Help:Variable]]
*[[Help:What links here]]
*[[m:Help:MediaWiki help policy|MediaWiki help templates]]
*[{{fullurl:Special:Allpages|namespace=10}} List of all templates on this server]
*[[Templates of Wikimedia projects]]
*[[w:Wikipedia:Template namespace]]
*[[DynamicFunctions##arg:]] - instead of including a page for given parameter values, this allows linking to a page for given parameter values. Syntax for parameter use: <nowiki>{{</nowiki>arg:''parameter name''|''default''}}. Syntax for linking: external link style with "&''parameter name''=''parameter value''" added to the URL.
{{h:f|enname=Template}}
In MediaWiki, a '''template''' is a page which can be inserted into another page via a process called [[w:transclusion|transclusion]]. It is a wiki [[w:en:subroutine|subroutine]] or [[w:en:HTML_element#Other_containers|iframe]] facility and is comparable to a #include statement or [[wikt:en:macro#Noun|macro]] that is expanded at page view time. [[Help:Substitution|Substitution]] allows templates to be used as a macro facility.
This page and its extension '''[[Help:Advanced templates]]''' provide a reference manual on templates in MediaWiki. For an introduction, see ''[[Help:A quick guide to templates|A quick guide to templates]]'' and [[mw:Help:Templates]].
'''Note:''' The master version of this page is the page [[m:Help:Template]] on Meta-Wikipedia; template demos work there, but may not work if you are viewing a copy of this page on another project.
==General==
The '''template namespace''' is the [[Help:Namespace|namespace]] with prefix "'''Template:'''" (this prefix can be used in all languages, but the canonical prefixes in languages other than English include Vorlage, Modèle, Sjabloon, with colon; the site WikiFAQ calls it Snippet).
A page in this namespace is called a '''template'''. The contents of such a page (either fixed or depending on [[w:en:Parameter (computer science)|parameters]] and/or [[Help:Variable|variable]]s) is designed to be useful for insertion in other pages ([[en:transclusion|transclusion]], creating a [[w:compound document|compound document]]).
The syntax for insertion of the page "Template:''name''" is <nowiki>{{</nowiki>''name''<nowiki>}}</nowiki>. This is called a template tag.
'''Example:''' <nowiki>abc{{tc}}def</nowiki>
{{ut1|tc}}
'''Gives:''' abc{{tc}}def.
This reference in the wikitext causes insertion of the template content when the referring page is rendered. Various equivalent terms are:
*[[w:call|call]]ing the template
*including the template
*transcluding the template
*using the template
Depending on context "including" can be used as opposed to ''substituting'' the template.
To create a hyperlink to a template page, use the usual wikilink syntax <nowiki>[[</nowiki>Template:''name'']].
If the page "Template:''name''" does not exist, then <nowiki>{{</nowiki>''<nowiki>name</nowiki>''<nowiki>}}</nowiki> works as <nowiki>[[</nowiki>Template:''name''<nowiki>]]</nowiki>, a link to a non-existing page, leading to the edit page. Thus, one way of making a template is putting the tag first, and then following the link.
If ''name'' starts with a valid namespace prefix, or a colon, then it is not prefixed with "Template:". This way any page can be used as template (a colon at the start refers to the main namespace).
Also it provides an alternative way of calling a template, which can be used in the case of a name conflict with a variable, e.g. [[Template:Ns:3]] and [[Template:PAGENAME]].
Using a page outside the template namespace as template can be useful for:
*quoting interface messages in the MediaWiki namespace
*templates for personal use in subpages of one's userpage, e.g. for experimenting
*inclusion of pages in other pages in the same namespace, e.g. having an extra page associated with each page, such as a to-do list, with a simple name correspondence; it can be created and included with <nowiki>{{{{NAMESPACE}}:{{PAGENAME}}/todo}}</nowiki>.
As usual the template name is case sensitive except (on most projects) for the first letter, and a blank space is equivalent with an underscore (as opposed to parameter names, see below: they are case-sensitive, even with respect to the first letter, and spaces are distinguished from underscores).
One can call a template that calls another template. If a template calls itself directly or indirectly, inclusion works only on one level for each template. However, with templates redirecting to the template that one would want to call recursively, one can achieve recursion without having to make copies of the whole template content, with the number of levels limited by the number of redirects. See also [[Help:Template#Repetition_within_a_page|Repetition within a page]] and {{tim|List of template calls}}.
A [[Help:Variable|variable]] in a template is evaluated after insertion in the referring page, i.e. if <nowiki>{{PAGENAME}}</nowiki> occurs in the wikitext of a template, it is rendered as the name of the referring page, not the name of the template.
A list of templates used on a page, all with links, is given on the edit page. Notes:
*even on a section edit page the whole list is given, i.e. also the templates used in other sections
*even when editing an old version of a page, the list is given for the current version
*templates for which the tags have not been saved yet are not shown, even on the preview page
(''Supported in version 1.4 and higher.'')
When saving a page without newline at the end, and going to the edit page again, there is a newline at the end. However, this does not seem to have effect, see the example above and the following example:
<pre>{{Numbered list demo}}{{Numbered list demo}}
{{Numbered list demo}}</pre>
using [[Template:Numbered list demo]], gives
{{Numbered list demo}}{{Numbered list demo}}
{{Numbered list demo}}
In a template name the character "#" and any characters after that are ignored: <nowiki>{{tc#abc}}</nowiki> gives {{tc#abc}}. This is used in {{tiw|en|hif}}.
==Use of templates across projects==
A template only works when referred to from a page in the same project. To use it in another project, one has to copy it to there. [[Bugzilla:1126]] is a request to lift that restriction, i.e. allow interwiki use of templates.
The adjustments needed in the copy, to work on the other project, can be reduced by using <nowiki>{{</nowiki>SERVER}}, localurl, and generic namespace names, and writing links in a way that works on multiple projects, e.g. [[m:Help:Table]]. For copying multiple templates, [[Help:Export|export]] and [[Help:Import|import]] can be helpful. It is also convenient if the template names can be the same on the other project. Therefore, when choosing template names, check which names are in use on other projects to which people might want to copy the templates. Note that if a template is referred to by something like abc<nowiki>{{</nowiki>NAMESPACE}}, a project with different namespace names requires a different template name, or a redirect.
Transclusion across projects, hence changing contents from a central place, is only possible:
*for content in the form of an image on Commons, to any page,
*for editable content on an image page on Commons, including content transcluded from other pages on Commons, to the corresponding local image pages, if there is no local image with the same name
Templates on the central Wikia work in the separate Wikia projects, provided that the prefix "wikia:" is used, see <span class="plainlinks">[http://www.wikia.com/wiki/Help:Shared_templates shared templates on wikia]</span>. (Please note that these external Wikia projects are NOT hosted and managed by the Wikimedia Foundation.)
==Parameters==
Template parameters are either named or numbered. They are [[w:en:Parameter (computer science)#Calling conventions|called-by-value]], and therefore input parameters only.
In the template ''definition'', the formal parameter (the [[w:en:Free variables and bound variables|placeholder]] for the parameter value) is a parameter name with three pairs of braces. So, for example, you would write {{{myVariableName}}} for a template parameter which you wanted to call ''myVariableName''.
{{MW 1.6|(for parameters defaults)}}
In the template ''call'', the syntax is either
*<tt><nowiki>{{templatename|parname1=parvalue1|parname2=parvalue2}}</nowiki></tt> with tags <tt><nowiki>{{{parname1|default}}}</nowiki></tt>, <tt><nowiki>{{{parname2|default}}}</nowiki></tt>, etc. in the template, or
*<tt><nowiki>{{templatename|parvalue1|parvalue2}}</nowiki></tt> with tags <tt><nowiki>{{{1|default}}}</nowiki></tt>, <tt><nowiki>{{{2|default}}}</nowiki></tt>, etc. in the template.
The default (optional, together with the pipe character in front) can be different for every occurrence, and applies if no value is specified when calling the template, not to be confused with the case that the empty value is specified.
A way to determine whether a parameter is defined is by comparing the results of two occurrences with different defaults, e.g. with <tt><nowiki>{{#ifeq: {{{a|+}}}|{{{a|-}}}|1|0}}</nowiki></tt>.
In the case of [[Help:Substitution|page substitution]] with an undefined parameter, not the default but the parameter itself with default is substituted into the wikitext.
===Example: named parameter substitution ===
Let us create a template called '''Name_example''' (that is, the template will be '''[[Template:Name_example]]'''), with a parameter for the first name which we will call ''firstName'', and a parameter for the last name which we will call ''lastName''.<br>
If we call the template ''without'' any parameters, i.e. if we write <nowiki>{{Name_example}}</nowiki>, then we will get:<br>
{{Name_example}}
However, now if we call it and pass parameters to it as follows:
<nowiki>{{Name_example | firstName=John | lastName=Smith}}</nowiki> the output will look like this:<br>
{{Name_example | firstName=John | lastName=Smith}}
===Example: numeric parameter substitution===
An example of how the contents of a parameter function.
====The simplest case====
Let us first ''define'' a template with the name ''t0'', which will
*expect a single argument
*give back the text of that argument, with no spaces before or after it, between "start-" and "-end":
:'''<nowiki>start-{{{1}}}-end</nowiki>'''
the definition of course being on page '''[[Template:t0]]'''.
#Calling the template named ''t0'' with a parameter value of "''a''", i.e.,
#::<nowiki>{{t0|a}}</nowiki>
#:gives
#::"{{t0|a}}"
#Calling the template named ''t0'' with a parameter which is blank, i.e.,
#::<nowiki>{{t0| }}</nowiki>
#:gives
#::"{{t0| }}" .
#Calling the template named ''t0'' with a null parameter, i.e.,
#::<nowiki>{{t0|}}</nowiki>
#:gives
#::"{{t0|}}".
#Calling the template named ''t0'' and not passing any parameters, i.e.,
#::<nowiki>{{t0}}</nowiki>
#:gives
#::"{{t0}}".
If something like '''{{{1}}}''' or in fact any '''{{{name}}}''' is visible on an ordinary page it typically indicates that a mandatory parameter of a template used on this page isn't defined.
====Declaring a default value====
Now let us ''define'' a similar template with the name ''t'' which contains a single numbered parameter 1 with a default of ''pqr''. The only difference between the effects of t and t0 appears when they are called with no parameter (and no "|"):
:'''<nowiki>start-{{{1|pqr}}}-end</nowiki>'''<br>
the definition of course being on page '''[[Template:t]]'''.<br>
#Calling the template named ''t'' with a parameter value of "''a''", i.e.,
#::<nowiki>{{t|a}}</nowiki>
#:gives
#::"{{t|a}}".
#Calling the template named ''t'' with a parameter which is blank, i.e.,
#::<nowiki>{{t| }}</nowiki>
#:gives
#::"{{t| }}".
#Calling the template named ''t'' with a null parameter, i.e.,
#::<nowiki>{{t|}}</nowiki>
#:gives
#::"{{t|}}".
#Calling the template named ''t'' with named parameter '''1=''', i.e.,
#::<nowiki>{{t|1=no surprise}}</nowiki>
#:gives
#::"{{t|1=no surprise}}".
#Calling the template named ''t'' with '''1=''' after an unnamed parameter, i.e.,
#::<nowiki>{{t|no|1=surprise}}</nowiki>
#:gives
#::"{{t|no|1=surprise}}".
#Calling the template named ''t'' with '''1=''' before an unnamed parameter, i.e.,
#::<nowiki>{{t|1=no|surprise}}</nowiki>
#:gives
#::"{{t|1=no|surprise}}".
#Calling the template named ''t'' and no parameter at all, i.e.,
#::<nowiki>{{t}}</nowiki>
#:gives
#::"{{t}}".
#Calling the template named ''t'' and no named or unnamed parameter '''1''', i.e.,
#::<nowiki>{{t|2=two}}</nowiki>
#:gives
#::"{{t|2=two}}".
===Template mechanism===
The template mechanism involves the following steps:
*In template calls, the template name, parameter names, and parameter values depending on variables are evaluated; the result of this step can be viewed by applying subst (possibly multiple times), saving and editing (see below). In the case of a variable depending on a variable, this may have to be done a second time.
*the values are assigned to the parameters from left to right (the result can not be directly viewed), meaning that a named or numbered parameter may be specified several times, the last one being the actual value that will be kept when passing the parameter during the final instantiation of the template below.
*template calls are replaced by template content, where the parameter tags with matching names are replaced by their values or defaults (the result of this step can be viewed by applying subst either again, or directly to the template and the variables).
The process is applied recursively until there are no more template calls to replace.
All whitespaces before and after a parameter value are usually ignored.
:<nowiki>"{{t| a}}"</nowiki> gives "{{t|a}}".
:<nowiki>"{{t|a }}"</nowiki> gives "{{t|a}}".
A parameter value can contain a piped link; the "|" in such a link is not taken as parameter separator.
:Example: <nowiki>"{{t|[[a|b]]}}"</nowiki> gives "{{t|[[a|b]]}}".
:Example: <nowiki>"{{t|[[a|b]] }}"</nowiki> gives "{{t|[[a|b]] }}".
(In this case the space ''has'' effect.)
On rendering, surplus parameter values (including named parameters that do not occur in the template) are ignored: <nowiki>{{t|a|b}}</nowiki> gives {{t|a|b}}.
Parameters without names (i.e. that don't contain an equal sign) are numbered implicitly starting by 1, from left to right.
:Example: <nowiki>"{{t|a|b|c}}"</nowiki> is equivalent to <nowiki>"{{t|1=a|2=b|3=c}}"</nowiki>
Numbered parameters can also be skipped or can be specified in alternate order by numbering them explicitly.
:Example: <nowiki>"{{t|3=c|2=b|1=a}}"</nowiki> is equivalent to <nowiki>"{{t|1=a|2=b|3=c}}"</nowiki>
:Example: <nowiki>"{{t|a|b|c|10=j}}"</nowiki> is equivalent to <nowiki>"{{t|1=a|2=b|3=c|10=j}}"</nowiki>
This also makes it possible to apply a default value for parameter 1, while specifying a value for parameter 2:
<nowiki>{{t2d|2=a}}</nowiki>, using {{timc|t2d}}, gives {{t2d|2=a}}.
Template calls can contain multiple occurrences of the same named parameter, and a numbered parameter can be specified multiple times by using its number explicitly. The effective value of parameters is the value of the last occurrence (from left to right) of the parameter with the same name, once the parameter names have been evaluated.
:Example: <nowiki>"{{t|a|b|c|2=B}}"</nowiki> is equivalent to <nowiki>"{{t|a|B|c}}"</nowiki>
The final list of formal parameters before the template itself is instantiated is then unordered because the list is indexed by the evaluated parameter named (this list is an [[:en:Associative array|associative array]]).
A useful example that uses of multiple assignments of parameters with the same name (and thus allowing parameter overrides) ''was'' performed in the [[:en:Template:If defined]] template used for example in English Wikipedia (and many other localized editions). This template, though, was deleted.
See also [[Help:Parameter default]].
==Restrictions on parameter values==
* If a parameter value contains an equals sign, the parameter name must be put explicitly, even when it is 1, 2, 3, etc.
* If the expression for a parameter value contains two consecutive closing braces which are not part of a variable or parameter tag, they have to be put in nowiki tags.
* If the expression for a parameter value contains a pipe character (|) which is not part of a piped link or within an image tag, it has to be put in nowiki tags: <nowiki><nowiki>|</nowiki></nowiki>.
* The expression for a parameter value cannot contain the variable localurl with query string, due to the pipe character.
*A parameter value can be quite long, see [[Help:Long parameter demo]].
*A parameter value can contain pairs of square brackets or text to be used within a link, but not a closing part of one link and an opening part of another link: <nowiki>{{</nowiki>tlis|abc]]def[[ghi}} using {{timc|tlis}} gives {{tlis|abc]]def[[ghi}}
==Mix of named and unnamed parameters==
In the case of a mix of named and unnamed parameters in a template tag, the unnamed parameters are numbered 1,2,3,.., so they are ''not'' numbered according to the position in the mixed list as a whole.
For example, <nowiki>{{</nowiki>t sup|3=1|2|1=3|4|5|6|7}} using {{tcw|t sup}} gives {{t sup|3=1|2|1=3|4|5|6|7}}.
== Template tag lay-out ==
To have a desired template tag lay-out, especially in the case of many parameters, additional dummy parameters can be used with a newline, spaces, and/or comments. In the case of unnamed parameters, this means that some parameter numbers are not used in the template content, so that their values, present in the template tag, do not affect the rendering of the template. For example, using {{ut|t3d}},
<pre>
{{t3d |a|b|c| 1
|d|e|f| 2
|g|h|i| 3
}}
</pre>
gives:
{{t3d |a|b|c| 1
|d|e|f| 2
|g|h|i| 3
}}
If parameters are named, dummy parameters can easily be inserted at any time. If they are unnamed, then, to avoid renumbering, one can insert named parameters: any text including an equals sign will do, if the text on the left is not one of the parameter numbers: this text is interpreted as the name of an unused parameter, hence ignored.
Example:
Using {{tim|Chess position}}
<pre>
{{chess position|=
8 |rd|nd|bd|qd|kd|bd|nd|rd|=
7 | |pd|pd|pd|pd|pd|pd|pd|=
6 |pd| | | | | | | |=
5 | | | | | | | | |=
4 | |pl| | | | |pl| |=
3 | | | | | | | |bl|=
2 |pl| |pl|pl|pl|pl| |pl|=
1 |rl|nl|bl|ql|kl| |nl|rl|=
a b c d e f g h
|30}}
</pre>
which gives
{{chess position|=
8 |rd|nd|bd|qd|kd|bd|nd|rd|=
7 | |pd|pd|pd|pd|pd|pd|pd|=
6 |pd| | | | | | | |=
5 | | | | | | | | |=
4 | |pl| | | | |pl| |=
3 | | | | | | | |bl|=
2 |pl| |pl|pl|pl|pl| |pl|=
1 |rl|nl|bl|ql|kl| |nl|rl|=
a b c d e f g h
|30}}
In this example, the 1 to 8 row numbers and a to h column numbers (as well as their surrounding spaces and newlines) in the parameter list are hidden within a dummy parameter (with an empty name before the equal sign).
The template generates a HTML table where each cell contains an image whose name is dynamically generated from the 64 first automatically numbered parameters (there are intermediate parameters within the numbered list, but as these dummy parameters are named explicitly, they don't increment the parameter number used to generate default parameter names. So the parameter with autogenerated name "1" effectively contains the "rd" value).
The template is then called with a parameter list that contains:
* 65 parameters automatically named from "1" to "65" (64 parameters for the cells content, and the 65th for the image width),
* and a dummy named parameter (whose name is an empty string, and whose effective value results from the last assignment with the chessboard column names a to h and the surrounding spaces and newlines), which is not used in the internal template content.
Note also that all parameters are trimmed, so the parameter with autogenerated name "9" (at position a7 on the chessboard above) is assigned the empty string value.
==Template page==
Without noinclude and includeonly parts the following are identical:
*the rendering of the wikitext of the template (this is on the template page, and would be the same in other pages with that wikitext, except for page-dependent variables)
*the rendering of the template on a page that calls it without parameters
Thus the defaults are shown, e.g. {{timc|T 1}} shows the same as <nowiki>{{</nowiki>t 1}}: "{{t 1}}".
===Template page if no defaults have been specified===
In simple cases this corresponds to treating the parameter tags as ordinary text, for example:
<nowiki>"{{t2}}"</nowiki> (using [[Template:t2]] containing {{msgnw:t2}}) gives {{t2}}
Similarly, if part of the parameters is undefined, only those show up as {{{''parameter number or name''}}}.
However, some templates produce an error message when called without parameters, so unless includeonly is used (see below) the template page also shows this message, suggesting that there is something wrong with the template. For example, a template that doubles parameter {{{1}}} with the wikitext <nowiki>{{#expr:2*{{{1}}}}}</nowiki> gives {{#expr:2*{{{1}}}}}. In such cases includeonly can be used to avoid uninformative and messy rendering on the template page, but either way a disadvantage during template development is that preview does not (meaningfully) reflect changes in the parts of the page outside the noinclude tags (i.e. the included parts). Therefore it can be useful to provide representative default values, even if that is only done temporarily during development of the template.
Note also that variable tags are not shown, but evaluated.
With noinclude an informative template page can be produced, containing more demos and test cases than just the single default set. Typically, the noinclude-part of a template page contains examples including or [[Help:substitution|substituting]] the template. Changes in the working of the template (i.e. changes outside the noinclude-part) are not yet effective in these examples in preview and, in the case of [[Help:substitution|substitution]], in "show changes".
However, they are reflected in the rendered page after saving.
Some formatting aspects may show up which are not readily seen from the edit box.
==A parameter value depending on parameters==
As mentioned above, if a parameter is left undefined, the triple braced parameter name in the result does not function as a parameter if the page is called as a template from another page; if it should, do not omit the parameter value but specify it in terms of a parameter of the calling page; compare:
*<nowiki>{{t2a|b}}</nowiki> (using [[Template:t2a]] containing {{msgnw:t2a}}) gives {{t2a|b}}
*<nowiki>{{t2a1|b}}</nowiki> (using [[Template:t2a1]] containing {{msgnw:t2a1}}) gives {{t2a1|b}}.
*Similarly, <nowiki>{{t2a2|b}}</nowiki> (using [[Template:t2a2]] containing {{msgnw:t2a2}}) gives {{t2a2|b}}. See also below.
The most straightforward way of expressing a parameter value in terms of a parameter is using the same name and making it equal, e.g. <nowiki>{{</nowiki>Lan is|lr=af|le={{{le}}}}} in [[Template:Lan is mr]].
==Empty vs. undefined==
<nowiki>{{t2||a}}</nowiki> makes the first parameter equal to the empty string rather than leaving it undefined: it gives {{t2||a}}. By treating the second unnamed parameter as a parameter with the name "2", the first unnamed parameter can be left undefined.
:Example: <nowiki>{{t2|2=a}}</nowiki> gives {{t2|2=a}}.
If the parameter is used in a tag like <nowiki><font size></nowiki> the default text may give invalid code that is conveniently ignored, rather than resulting in perhaps ugly code being rendered (but this may depend on the browser). See e.g. [[Template talk:Fontsize]].
Parameter names (as opposed to template names) are case-sensitive, even with respect to the first letter, and spaces are distinguished from underscores. The [[w:en:String_%28computer_science%29#Formal_theory|empty string]] is also a valid parameter name, see [[Template:T empty string as parameter name]].
Parameter 02 is distinguished from parameter 2: using {{timc|t pl0}}, <nowiki>{{</nowiki>t pl0|5|6|7}} gives {{t pl0|5|6|7}}
Note that parameters in the [[MediaWiki namespace]] (interface messages) are written differently: as $1, $2, etc., see [[Meta-Wikimedia:MediaWiki namespace text]].
==Equals sign in parameter value==
When a parameter value contains an equals sign, the parameter name must be stated explicitly, otherwise the part of the parameter value before the equals sign is taken as the parameter name. If that happens accidentally, this parameter is likely to be unused, hence harmless, but the intended implicit parameter 1, 2, or 3, etc., gets no value.
:Example: <nowiki>{{TTT|a=b|c}}</nowiki> {{ut1|TTT}} gives {{TTT|a=b|c}} (unused parameter a is assigned the value b, the first parameter value for which no parameter name is specified is assigned to the parameter with the implicit name "1" — it reads like so: Use template TTT, assign "b" to the parameter "a", use the value "c" for your first ''implicit'' parameter <nowiki>{{{1}}}</nowiki>.), while <nowiki>{{TTT|1=a=b|2=c}}</nowiki> gives {{TTT|1=a=b|2=c}} (the first parameter is assigned the value "a=b" — it reads like so: Use template TTT, assign "a=b" to the ''expressed'' parameter "1", assign "c" to the ''expressed'' parameter "2"). In this case, if template TTT had a parameter <nowiki>{{{3}}}, {{TTT|1=a=b|2=c|d}}</nowiki>, "d" would be assigned to "3" as first ''implicit'' parameter without a value.
The same applies if a parameter value depends on a parameter, and ''may'' therefore contain an equals sign. Compare:
*<nowiki>{{t2a1|1=x=y}}</nowiki> (using [[Template:t2a1]] containing {{msgnw:t2a1}}) gives {{t2a1|1=x=y}}.
*<nowiki>{{t2a2|1=x=y}}</nowiki> (using [[Template:t2a2]] containing {{msgnw:t2a2}}) gives {{t2a2|1=x=y}}.
==msgnw==
The prefix '''msgnw''' (short for "message, nowiki") is used to display the uninterpreted wikitext of a template (the same thing you'd see if you were editing the page) on another page. An exception is that numbered and unnumbered lists are rendered as such instead of displaying # or * signs. Parameter values are ignored.
With the introduction of noinclude and includeonly tags, allowing template pages to take care of definition as well as displaying information, msgnw displays no longer just the technical information about what the template does when included, but a mix of that with wikitext for rendering the template page itself, with the tags. This limits the usefulness of applying this.
Examples:
[[Template:en]]
*<nowiki>{{msgnw:en}}</nowiki> gives {{msgnw:en}}
*<nowiki>{{en}}</nowiki> gives {{en}}
{{tim|t}}
<nowiki>{{msgnw:t|a}}</nowiki> gives
*{{msgnw:t|a}}
<br />...which is much more than just the formal info about what the template does when included:
*<nowiki>start-{{{1|pqr}}}-end</nowiki>
[[Template:Numbered list demo]]
<nowiki>{{msgnw:Numbered list demo}}</nowiki> gives:
{{msgnw:Numbered list demo}}
<nowiki>{{Numbered list demo}}</nowiki> gives:
{{Numbered list demo}}
The edit box for [[Template:Numbered list demo]] shows:
<pre>
#Amsterdam
#Rotterdam
#The Hague
</pre>
==Usage==
Templates are useful for any text for which one wants a copy in two or more pages, and there is no need for each copy to be edited independently to adapt it to the page it is in. Since parameters can be used, versions may to that extent even be different, and parameter values can be edited independently for each. Templates are not only convenient, but they can also "force" a useful uniformity. See also [[w:en:Subroutine#Advantages_of_subprograms|advantages of subprograms]].
Typical applications are:
*[[w:Wikipedia:Template messages|template messages]] such as [[w:en:Template:stub]]
*[[w:Wikipedia:Navigational templates|navigational templates]] such as [[w:en:Template:europe]]
*[[w:Wikipedia:Infobox|infoboxes]], each time showing the same kind of information in the same format, but each time for a different case, using parameters, e.g. the [[w:en:Template:Infobox Countries]].
*provide cross-language portability of texts which are largely internationally the same, but contain some standard terms different in each language; the standard terms are replaced by {{..}} referring to template pages which have the same name but localised content in each language. This is [[m:Using templates in Wiktionary|applied in Wiktionaries]] for names of languages, terms like "noun", etc.; it is also [[w:Wikipedia:Template messages#Taxobox_components_.28Taxons.29|applied for taxoboxes]], see e.g. [[w:en:Template:Familia]] and [[w:fr:Modèle:Familia]].
*allow a composite image, composed of a table of images, to be specified by a template tag which itself has a corresponding table-like lay-out, see [[w:Template:Chess position]] and [[w:Template:Game of Go Position]] (the latter example also shows that the number of parameters a template can have is quite large, in this case 362). Newlines in parameter values provide the lay-out of the template tag, but they are not allowed in image names, therefore they are put within comment tags; alternatively, dummy parameters could be used on these positions.
Various combinations are possible. E.g., the last two could be combined, so that e.g. every language version of Wikipedia has the same Infobox Countries template (for each project a copy, because the template mechanism can not import across projects), which has parameters for the data, and refers to templates with translations of standard terms.
See also '''[[Help:Advanced templates]]'''.
===Composite pages===
The wikitext of a page may (partly or fully) consist of tags for the inclusion of component pages. The "templates" are not necessarily in the template namespace, and may be more or less self-contained pages by themselves. An advantage of putting the components in the template namespace is that they are then included in the list of templates used on the composite page, all with links, on its edit page. On the other hand, the component page may, by itself, be of a nature that it belongs in e.g. the main namespace, while the prefix would suggest otherwise; also the prefix would clutter the pagename.
Examples are:
*[[Help:A simple composite example]]
*[[w:Wikipedia:Village pump]] with six component pages, e.g. [[w:Wikipedia:Village pump (technical)]]
*daily a page like [[w:en:Wikipedia:Votes for deletion/Log/2005 May 31]] where each component page consists of the discussion on the deletion of one Wikipedia page, e.g. [[w:en:Wikipedia:Votes for deletion/Sp3tt]] (for this day there are 75 of such component pages)
*[[m:Meta:Translation/Coordination]], mainly containing
**[[m:Meta:Translation/Coordination/List/Meta]]
**[[m:Meta:Translation/Coordination/List/Main]]
*[http://en.wikipedia.org/w/index.php?title=2005_English_cricket_season_%281-14_June%29&diff=27917447&oldid=27330886] - rejection of composite page by w:en
This allows the user the choice between viewing the component pages separately or combinedly. One may want to view a page separately if one has a slow connection; also sometimes people have sentimental reasons for having a separate page about a topic, e.g. a village. Viewing the combined page is much more convenient if there are many small component pages; even if a subbtopic page does not provide additional info, if it exists anyway, and is linked to, it is useful to display the content (making clear that the whole content is shown), it saves the trouble of checking the contents by following the link.
In the latter case the search function of the browser can be used for searching in the combined page. Similar to the section editing feature, it allows editing a component page; however, there is no possibility to have the combined wikitext in the edit box.
Edit history, recent changes, watching pages, and "what links here" work separately for the component pages and the ''composition'' page, not for the ''composite'' page. Related changes works for the component pages and for the composite page. Page protection can be set for selected component pages and for the composition page, not directly for the composite page.
The talk page of a composition page is used to talk about the composition and the page in general, and could in addition be a composite page of the talk pages of the component pages.
A composite page has an integrated TOC and section numbering; the numbering is not reset for each component page.
Editing a section of a component page can be done directly from the composite page, see [[Help:Section#Editing sections of included templates|editing sections of included templates]]. After saving, one ends up at the page for the component page to which the section belongs.
On projects with the [[Help:Interwiki linking#Interlanguage_link_.28software_feature.29|interlanguage link feature]] the composite page shows the combined interlanguage links of all component pages, hence possibly multiple links for one language or even for one page.
For easy access to the component pages, links to them on the composite page (for example by self-links on the component pages) are convenient. Alternatively, access is through section editing, or, if the component pages are in the template namespace, through the list of templates called from the composite page, at its edit page.
Conversely, for easy access from a component page to the composite page(s) that call(s) it (if there are not too many of them) it is convenient to link the component page to them (on a composite page one becomes a self-link).
See also [[w:Wikipedia talk:Template namespace#transcluding prose]].
===Pages with a common section===
A section on the relationship of the subjects A and B, or a subtopic equally relevant for A as for B, can be put both in page A and in page B, by making it a separate page C, called as a template from A and B.
Remarks:
*For readers reading both A and B, it is useful that the duplication is indicated by a message or a special lay-out; otherwise it is confusing and inconvenient. One can for example use {{tim|cs}}: {{cs|Help:Template}}
*After saving one ends up viewing the template page. For easy access to the calling pages it is convenient to link the called page to them (on a calling page one becomes a self-link). This may well be integrated in the message about the duplication. It is even more convenient than the two-step link as shown above.
*The section level of the embedded section(s) has to be the same in each embedding page. The same applies to lay-out and style.
*Pages like C can be put in
**the same namespace as A and B; advantage:
***the kind of content of C is the same as that of A and B
**the template namespace; advantages:
***C appears in the list of templates called from A and B, at their edit pages
***C by itself may not satisfy the standards for pages in the namespace of A and B, such as providing context (however, the message about the duplication explains it)
Examples:
*{{pim|Help|Editing sections of included templates}}
*{{pim|Help|Alphabetic order}}
For various remarks which also apply here, see also the previous section.
===Repetition within a page===
For a succession of similar pieces of content, as in a list, a computer program would use a [[w:en:Conditional_branching#Loops|loop]]. What comes closest in MediaWiki is putting the loop body in a template, and calling the template repeatedly, usually with a varying parameter value. This is somewhat primitive for a loop:
*instead of "for i=1 to 5" we need to enumerate the values 1, 2, 3, 4, 5; on the other hand, this gives the versatility to use just as easily less regular values such as de, en, fr ([[w:en:Conditional_branching#Collection-controlled_loops|collection-controlled loop]]).
*repetition of code is minimized by the template, but the template call itself has to be repeated each time (but see also the next section)
Example:
*[[Template:List of Languages]] - the "loop body" is in [[Template:Lang def]]; compared with putting the content of the latter directly in the list the advantage is that the variable part (the value of "code") occurs once each time in the code, although it is used twice. This is important because the fixed part of the list item code can be copied while the variable part is typically inserted manually.
For repetition of the same text the loop body can be a parameter, e.g. [[Template:X5]] (see [http://meta.wikimedia.org/w/index.php?title=Special%3AAllpages&from=X&namespace=10 list]).
A double loop is achieved by a second template that repeatedly calls the first. Similarly for a triple loop, etc. See e.g. [[w:Template:Ld]], [[w:Template:L2d]], and [[w:Template:L3d]].
An example where the same template is used for different loop levels is [[Template:Loop 2]]. Because the software has a built-in protection against infinite nesting (if there is, there will be an error e.g. <code>Fatal error: Maximum execution time of 30 seconds exceeded in /w/includes/Parser.php on line 775</code>), a redirect is used for each level. The number of levels that the template can handle is determined by the number of parameters specified in the two template calls it comprises (because going up a level requires an extra parameter) and the number of existing redirects.
Similarly [[Template:Lanlp 1]] is used for both the inner and the outer of a double loop. Adding an item (here a language) adds to each row and also adds an extra row.
'''Providing effective repetition without repetition of similar template calls in the wikitext'''
A technique for repetition of similar template calls without actually putting this list in the wikitext is demonstrated in {{tim|foreach}} and {{tiw|en|foreach}} (on which the version on Meta is based; it is similar but different on details), and a more primitive method described below.
Example {{ut1|t2}}:
<nowiki>{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}</nowiki> gives:
{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}
Description of the old {{tim|List of template calls}}:
For an arbitrary template with up to three nameless parameters, it is equivalent to calling the template repeatedly, with the first parameter varying, and the possible other parameters fixed: one specifies the name, the values of the fixed parameters, and the list of values the first parameter should have in consecutive template calls, with a sequential number each, and ending with "end". The technique requires redirects like [[Template:List of template calls 3]], as many as the maximum number of repetitions.
If e.g. the second parameter varies, or two parameters, then a similar template can be made, but each version requires its own set of redirects.
Example {{ut1|t2}}:
<nowiki>{{List of template calls|t2|constant||abc|1|def|2|ghi|end|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}}</nowiki>
gives:
{{List of template calls|t2|constant||abc|1|def|2|ghi|end|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}}
Compare <nowiki>{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}</nowiki> giving:
{{foreach|call=t2|pc1=2|pc1v=constant|abc|def|ghi}}
The empty parameters are needed in accordance with the maximum number of parameters the repeated template can have (currently three) and the maximum number of repetitions. Putting more empty parameters than needed at the end does not matter, but the number of empty parameters in the beginning of the list should be correct.
==Internal links==
In the case of an internal link in a template with the target depending on a parameter, and in the case of a link with the target depending on a template, existence detection works as usual.
Examples:
*<nowiki>{{tli|insert in target|insert in label}}</nowiki> {{ut1|tli}} gives {{tli|insert in target|insert in label}}
*<nowiki>[[a{{tciit}}b|c{{tciil}}d]]</nowiki> {{ut2|tciit|tciil}} gives [[a{{tciit}}b|c{{tciil}}d]]
*<nowiki>{{tli|insert also in target|insert in label}}</nowiki> (using [[Template:tli]]) gives {{tli|insert also in target|insert in label}} {{ne}}
*<nowiki>[[a{{tciit}}e|c{{tciil}}d]]</nowiki> (using [[Template:tciit]] and [[Template:tciil]]) gives [[a{{tciit}}e|c{{tciil}}d]] {{ne}}
*<nowiki>{{la|Help:Tabl|tabl}}</nowiki> {{ut1|la}} gives: {{la|Help:Tabl|tabl}}
*[[Template:Pvn]]
==External links==
Examples:
*<nowiki>{{tle|insert in target|insert in label}}</nowiki> {{ut1|tle}}} gives {{tle|insert in target|insert in label}}
*<nowiki>{{tle2|insert in target}}</nowiki> {{ut1|tle2}}} gives "{{tle2|insert in target}}"; note that brackets separated from the http:// are not recognized as belonging together:
**<nowiki>[{{tle2|insert in target}} label]</nowiki> gives [{{tle2|insert in target}} label]
**<nowiki>[{{tle2|insert in target}}]</nowiki> gives [{{tle2|insert in target}}]
*<nowiki>{{tle3|insert in target}}</nowiki> {{ut1|tle3}}} gives {{tle3|insert in target}}
*<nowiki>[http://a{{tciit}}b c{{tciil}}d]</nowiki> gives [http://a{{tciit}}b c{{tciil}}d]
*<nowiki>http://a{{tciit}}b</nowiki> gives "http://a{{tciit}}b"
*<nowiki>[http://a{{tciit}}b]</nowiki> gives [http://a{{tciit}}b]
However, a URL can not be composed of:
*a first part in a parameter and a second part added by the template (although we have seen above that reversed it works: a second part in a parameter and a first part prefixed by the template)
*a first part in a template and a second part after the template (although we have seen above that reversed it works: the second part in a template and the first part before the template)
Example of first restriction:
*<nowiki>{{tae|http://www.}}</nowiki> {{ut1|tae}}} gives {{tae|http://www.}}
Examples of second restriction:
*<nowiki>[{{th}}add at the end]</nowiki> {{ut1|th}}} gives [{{th}}add at the end]
*"<nowiki>{{th}}add at the end</nowiki>" gives "{{th}}add at the end"
==Noinclude and includeonly==
This feature is not available before version 1.6.
Anything between <nowiki><noinclude> and </noinclude></nowiki> will be processed and displayed only when the page is being viewed directly; it will not be included or substituted.
Possible applications are:
# Categorising templates, see [[Help:template documentation|template documentation]].
# [[Help:Interwiki_linking|Interlanguage link]]s to similar templates in other languages.
# Pages in the [[Help:MediaWiki namespace|MediaWiki namespace]].
<nowiki>The converse is <includeonly>. Text between <includeonly> and
</includeonly></nowiki> will be processed and displayed only when the page is
being included. Applications include:
# Adding all pages containing a given template to a category.
# Avoiding messy rendering on the template page, e.g. {{evaldemo|#expr:{{{1}}}}}.
Note that spaces and newlines between the general content and the tagged part belong to the general content. If they are not desired the include tag should directly follow the content on the same line:
<noinclude>this is </noinclude>fine<includeonly>, closing tags are
</includeonly><noinclude>
less critical, but must be specified.</noinclude>
Attempts to nest split pairs of these tags, or the similar <tt><nowiki></tt> and <tt></nowiki></tt> pair, won't work as expected. If say <tt><nowiki></tt> begins within the general content,
or in a "noinclude" part, or in an "includeonly" part, then it also has to be closed within the same part.
The code <code><nowiki>~<includeonly>~</includeonly>~~</nowiki></code> will be displayed as <nowiki>~~~</nowiki> when the template is not included, <nowiki>~~~~</nowiki> when the template is included, and it will be expanded as the active user when the template is subst'd.
== Wiki markup at the begin of a template==
If the first included character of a template is one of the Wiki markup characters '''<tt>:;*#</tt>''', then it's interpreted as being at the begin of the line (even when the template call is not).
To avoid this effect use <tt><nowiki>#</nowiki></tt> or a [[w:en:Numeric character reference|NCR]] like say <tt>&#58;</tt> for a colon, this NCR is also useful in conjunction with definition [[Help:list|list]]s.
==Substitution==
:''Main article: '''[[Help:Substitution]]'''''
Putting "subst:" after the double opening braces causes an [[Help:Automatic conversion of wikitext|automatic conversion of wikitext]] when the referring page is saved: the subst tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters.
==Redirection==
When a page called for inclusion is a redirect page, the redirect target is included instead. As usual, a double redirect does not work.
A page that consists of nothing else than the inclusion of another page as a template, shows similarities to a redirect to that other page, but also many differences, including:
*the result has as header the name of the referring page
*there is no redirect message
*the buttons such as edit (for editing the whole page), watch, talk, history, "what links here", and "last modified", etc., refer to the referring page (to go to the target page, a section edit link can be used, from there the whole page can be accessed)
*the referring page is in the same categories as the target page
*"double redirects", with one or both being this kind of "pseudo-redirect", work.
Compare [[Help:Template namespace]] with [[Help:Template namespace - demo of alternative for redirect]].
In a page that in turn includes such a referring page there is no difference.
==Comparison of linking and embedding==
Linking and embedding have in common:
*if the page does not exist, a link to the edit page is shown; the links are identical if it is not a piped link; the link does not show parameters and does not show whether, after creation, the new page will be linked or embedded.
*the name can depend on variables and parameters
Note that embedding works for all pages where [[Help:Redirect|redirecting]] works, and it also fails for all pages where a redirect won't work.
==Predefined templates==
[[Help:Variable|Variable]]s, [[ParserFunctions|parser function]]s, and other "[[help:colon function|colon function]]s" can be considered predefined templates. They do not have a page that defines in terms of wikitext what they do, because their action is something extra that cannot be constructed from more basic wikitext functionality.
Some additional differences:
*Predefined templates with one or more parameters use ":" instead of the first "|".
*An edit page does not list predefined templates used on the page.
*No "What links here" is available for predefined templates, to find the pages where they are used.
*Apart from #switch, predefined templates don't allow you to define named parameters, and therefore equal signs generally have no special effect, comparison:
*:<tt>{{ #if: not blank | 2=abc }}</tt> gives '''{{ #if: not blank | 2=abc }}'''
*:<tt>{{ ifdef | not blank | 2=abc }}</tt> gives '''{{ ifdef | not blank | 2=abc }}'''
*It's however possible to corrupt the value shown for undefined parameters without default, for more examples see {{tim|ifeq demo}}:
*:<tt>{{ #if: {{ns:0}} | 2=xy | {{{2|blank}}} }}</tt> gives '''{{ #if: {{ns:0}} | 2=xy | {{{2|blank}}} }}'''
*:<tt>{{ #if: {{ns:0}} | 2=xy | {{{2 }}} }}</tt> gives '''{{ #if: {{ns:0}} | 2=xy | {{{2 }}} }}'''
*:<tt>{{ #if: {{ns:0}} | oops | {{{1|blank}}} }}</tt> gives '''{{ #if: {{ns:0}} | oops | {{{1|blank}}} }}'''
*:<tt>{{ #if: {{ns:0}} | oops | {{{1 }}} }}</tt> gives '''{{ #if: {{ns:0}} | oops | {{{1 }}} }}'''
Ordinary and predefined templates have in common that their parameter values can depend on ordinary and predefined templates. In the case of named parameters, the names used in the template call can also depend on ordinary and predefined templates. This applies also for the named parameters of #switch.
==Templates in different versions of MediaWiki==
===Version 1.6===
* More magic words and the [[Help:parameter default|parameter default]] mechanism were added.
* <tt><noinclude></tt> and <tt><includeonly></tt> (see above) were added.
===Version 1.5===
* More [[Help:magic words|magic words]] were added.
===Version 1.4===
*Including the same template more than five times in the same page works from version 1.4, now on all Wikimedia sites. Also new was that subst can be used with parameters.
===Version 1.3===
* See, among other things, [{{fullurl:m:Help:Template|oldid=86075}}#Default_values_of_parameters default values of parameters]. Starting with version 1.3 the prefix "msg:" is generally optional.
===Version 1.2.6===
* In MediaWiki version 1.2.6, make a page MediaWiki:mytemplate and refer to it with <nowiki>{{msg:mytemplate}}</nowiki>. In this version parameters are not possible.
* Compatibility with 1.3, in the sense of having the content in Template:mytemplate, is possible with a redirect from MediaWiki:mytemplate to Template:mytemplate on the 1.2.6 project.
== Revision history of pages containing templates ==
Pages in the stored [[Help:Page history|page history]] consist of wikitext with possible ''references'' to templates and images. When viewing an old version of a page, these refer to the current versions of the templates and images, if these still exist. Thus the former composite page is not reconstructed.
==See also==
*[[m:Help:Advanced templates|Help:Advanced templates]]<!-- m: not directly portable due to examples -->
*[[Help:Calculation]]<!-- maybe portable, test it -->
*[[Help:Colon function]]
*[[m:Help:Parameter default|Help:Parameter default]]<!-- m: -->
*[[Help:Magic words]]
*[[Help:Substitution]]<!-- maybe portable, test it -->
*[[Help:Template documentation]]
*[[Help:Variable]]
*[[Help:What links here]]
*[[m:Help:MediaWiki help policy|MediaWiki help templates]]
*[{{fullurl:Special:Allpages|namespace=10}} List of all templates on this server]
*[[Templates of Wikimedia projects]]
*[[w:Wikipedia:Template namespace]]
*[[DynamicFunctions##arg:]] - instead of including a page for given parameter values, this allows linking to a page for given parameter values. Syntax for parameter use: <nowiki>{{</nowiki>arg:''parameter name''|''default''}}. Syntax for linking: external link style with "&''parameter name''=''parameter value''" added to the URL.
{{h:f|enname=Template}}