Help:Template: Difference between revisions

From JonnyBWiki
Jump to navigation Jump to search
mNo edit summary
 
No edit summary
 
Line 1: Line 1:
In MediaWiki, a  '''template''' is a page which can be inserted into another page via a process called [[wikipedia:transclusion|transclusion]]. It is comparable to a [[wikipedia:en:subroutine|subroutine]].
<languages/>
{{TNT|PD Help Page}}
<translate>
<!--T:1-->
If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play.</translate>
<translate>
<!--T:116-->
Unlike extensions and media files, there is no central repository for templates. Templates can be newly written or to save duplicating work already done, exported from another wiki e.g. Wikipedia, and then imported into the target wiki.


This page and its extension '''[[Help:Template names, variable names and parameters depending on a variable or parameter]]''' provide a reference manual on templates in MediaWiki. For an introduction, see ''[[m:Help:A quick guide to templates|A quick guide to templates]]''.
== Creation == <!--T:2-->


'''Notes:'''
<!--T:3-->
*The master version of this page, [[m:Help:Template]], is on Meta-Wikipedia; template demos work there, but may not work if you are viewing a copy of this page on another project.
Templates are standard wiki pages whose content is designed to be [[Special:MyLanguage/Transclusion|transcluded]] (embedded) inside other pages. Templates follow a convention that the name is prefixed with "<code>Template:</code>", assigning it to that [[Special:MyLanguage/Help:Namespaces|namespace]]; besides this, you can [[Special:MyLanguage/Help:Starting a new page|create them like any other wiki page]].
*Templates can be used to control (part of) the page body, but not the margins (logo, sidebars, etc.). These are controlled by the skin, the [[Help:user style|user style]], and part of the contents of the [[Help:MediaWiki namespace|MediaWiki namespace]].


==General==
<!--T:4-->
The '''template namespace''' is the [[Help:Namespace|namespace]] with prefix "'''Template:'''" (this prefix can be used in all languages, but the canonical prefixes in other languages than English include Vorlage, Modèle, Sjabloon, with colon; the site WikiFAQ calls it Snippet).
The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents:


A page in this namespace is called a '''template'''. The contents of such a page (either fixed or depending on [[wikipedia:en:Parameter (computer science)|parameters]] and/or [[variable]]s) is designed to be useful for insertion in other pages ([[wikipedia:en:transclusion|transclusion]], creating a [[wikipedia:en:compound document|compound document]]).
<!--T:5-->
Hello! Welcome to the wiki.


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.
<!--T:6-->
you'll have created your first template! If you then insert the code:


This reference in the wikitext causes insertion of the template content when the referring page is rendered. Various equivalent terms are:
<!--T:7-->
*[[wikipedia:call|call]]ing the template
<nowiki>{{Welcome}}</nowiki>
*referring to the template
*including the template
*transcluding the template
*using the template


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.
<!--T:8-->
in any other page, when that page is viewed the text "Hello! Welcome to the wiki." will appear instead of <code><nowiki>{{Welcome}}</nowiki></code>. The template content is "transcluded" into the other page, i.e. it is integrated in the page.


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).
<!--T:9-->
You can then insert <code><nowiki>{{Welcome}}</nowiki></code> at any point of any page where you wish to welcome someone. Suppose it is used in 100 pages. If you then change the template contents to:


However, a page in the template namespace has advantages:
<!--T:10-->
*they are listed on the edit page of pages that call the template
Hi there! Welcome to this wonderful wiki.
*when a page in the template namespace is changed, all the other pages that link to it automatically have their server caches (as opposed to browser cache) purged. With other namespaces, you need to purge the server cache manually.  


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]].
<!--T:11-->
and revisit any of the 100 pages where the template was used, you'll see the new text instead of the original one. In this way, you have changed the content of 100 pages without editing them, because the template is transcluded into these pages.


In the case of an image or category, it gives the description part. E.g. <nowiki>{{</nowiki>Image:tst.png}} gives {{Image:tst.png}}. However, if the image is on commons, the template call is treated as a link, which in turn is the embedding command, so the image is embedded: <nowiki>{{</nowiki>Image:Map South Holland.png}} gives:
<!--T:12-->
This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.


{{Image:Map South Holland.png}}
== Usage == <!--T:13-->


Using a page outside the template namespace as template can be useful for:
<!--T:14-->
*quoting interface messages in the MediaWiki namespace
Templates can be used in other pages in these ways:
*experimenting with templates in subpages of one's userpage
*[[Help:Image_page#Caption|using as the caption of an image the editable part of its image page]]
*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).
<!--T:15-->
* <code><nowiki>{{Name}}</nowiki></code> — as described above, this link will be dynamically replaced by the current content of <nowiki>[[Template:Name]]</nowiki> at the time the page with the template link is '''loaded'''. The link will remain unchanged in the page's source.
</translate>
<translate>
<!--T:16-->
* <code><nowiki>{{subst:Name}}</nowiki></code> — when this template link is used, it will be replaced once and for all with the content of <nowiki>[[Template:Name]]</nowiki> as of the time the page with the link is '''saved''': a copy of the contents of <nowiki>[[Template:Name]]</nowiki> will be ''substituted'' for the template link. The contents are then a part of the including page, and can be edited normally, separately from the original. ''Note'': changes to the source template page will ''not'' be propagated to the page with the template link.
</translate>
<translate>
<!--T:17-->
* <code><nowiki>{{safesubst:Name}}</nowiki></code> — this was introduced to allow for substitution that doesn't break transclusion, see [[w:en:Help:Substitution#The safesubst: modifier]].
</translate>
<translate>
<!--T:18-->
* <code><nowiki>{{msgnw:Name}}</nowiki></code> includes the template in a form that displays it as raw wiki syntax (the way <code><nowiki>&lt;nowiki&gt;</nowiki></code> does) when the page containing it is fetched.


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}}.
<!--T:19-->
In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so:


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.
<!--T:20-->
* <code><nowiki>{{Template:Pagename}}</nowiki></code> includes <code><nowiki>[[Template:Pagename]]</nowiki></code>
</translate>
<translate>
<!--T:21-->
* <code><nowiki>{{Foo:Pagename}}</nowiki></code> includes <code><nowiki>[[Foo:Pagename]]</nowiki></code>
</translate>
<translate>
<!--T:22-->
* <code><nowiki>{{:Pagename}}</nowiki></code> includes <code><nowiki>[[Pagename]]</nowiki></code>
</translate>
<translate>
<!--T:23-->
** <code><nowiki>{{subst::Pagename}}</nowiki></code> replaces itself with the contents of <code><nowiki>[[Pagename]]</nowiki></code>


A list of templates used on a page, all with links, is given on the edit page. Notes:
<!--T:24-->
*only those in the template namespace are given, ''not'' pages in other namespaces, transcluded in the page
If no such namespace exists, the full title is assumed to be a template:
*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.'')
<!--T:25-->
* <code><nowiki>{{Foo:Bar}}</nowiki></code> includes <code><nowiki>[[Template:Foo:Bar]]</nowiki></code>


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:
== Parameters == <!--T:26-->


<pre>{{Numbered list demo}}{{Numbered list demo}}
<!--T:27-->
{{Numbered list demo}}</pre>
To enrich the mechanism of transclusion, MediaWiki allows parameters to be passed to a template when it is transcluded. Parameters allow the template to produce different contents or have different behaviors.


using [[Template:Numbered list demo]], gives
<!--T:28-->
Suppose you wish to insert a little thank you note in the [[Special:MyLanguage/Help:Talk pages|talk page]] of other users, such as:


{{Numbered list demo}}{{Numbered list demo}}
<!--T:29-->
{{Numbered list demo}}
{{Thankyou|all your effort|Me}}


==Use of templates across projects==
<!--T:30-->
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 thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user be able to thank any other user, for any reason whatsoever.


Those on the central Wikicities work in the separate Wikicities projects, provided that the prefix "wikicities:" is used, see [[wikicities:Help:Shared templates]].
<!--T:31-->
So that the note will look similar everywhere it is used, you can define a template called [[Template:Thankyou]], for example. Although the note should look similar whenever a user thanks another user, its specific contents (i.e. the reason and the signature) will be different. For that reason, you should pass them as parameters. If we ignore the remaining elements to format the box and place the image, the core content of the template will be this:


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 and import|export and 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.
<!--T:32-->
<pre><nowiki>'''A little thank you...'''
for {{{1}}}.
hugs, {{{2}}}</nowiki></pre>


Transclusion across projects, hence changing contents from a central place, is only possible:
<!--T:33-->
*for content in the form of an image on Commons, to any page,
Notice the use of <tvar|1><code><nowiki>{{{1}}}</nowiki></code></> and <tvar|2><code><nowiki>{{{2}}}</nowiki></code></>. This is the way to identify, within templates, the parameters that will be passed in when the template is used. Note that, within the template, each parameter is surrounded by '''three''' braces: <tvar|braces><code><nowiki>{{{ }}}</nowiki></code></>. This is different from normal template name usage.
*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


==Parameters==
<!--T:34-->
When using the template on a page, you fill in the parameter values, separated by a "pipe" character (<tvar|pipe><code>|</code></>). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named.


Template parameters are either named or numbered. They are [[wikipedia:en:Parameter (computer science)#Calling conventions|called-by-value]], and therefore input parameters only.
=== Anonymous parameters === <!--T:35-->


The syntax is <tt><nowiki>{{templatename|parname1=parvalue1|parname2=parvalue2}}</nowiki></tt> with tags <tt><nowiki>{{{parname1}}}</nowiki></tt>, <tt><nowiki>{{{parname2}}}</nowiki></tt>, etc. in the template, or <tt><nowiki>{{templatename|parvalue1|parvalue2}}</nowiki></tt> with tags <tt><nowiki>{{{1}}}</nowiki></tt>, <tt><nowiki>{{{2}}}</nowiki></tt>, etc. in the template. Thus the formal parameter (the [[wikipedia:en:Free variables and bound variables|placeholder]] for the parameter value) is the parameter name with three pairs of braces.
<!--T:36-->
To pass in anonymous parameters, list the values of those parameters sequentially:


Thus the template mechanism involves the following steps:
<!--T:37-->
<nowiki>{{Thankyou|all your effort|Me}}</nowiki>


*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), save and edit, see below; in the case of a variable depending on a variable this may have to be done a second time;
<!--T:38-->
*the values are assigned to the parameters from left to right (the result can not be directly viewed)
In this case, the <code><nowiki>{{Thankyou}}</nowiki></code> template receives parameters <code><nowiki>{{{1}}}=all your effort</nowiki></code> and <code><nowiki>{{{2}}}=Me</nowiki></code>, producing:
*template calls are replaced by template content, where the parameter tags with matching names are replaced by their values (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.
<!--T:39-->
{{Thankyou|all your effort|Me}}


Spaces before or after a parameter value are normally ignored:
<!--T:40-->
:<nowiki>"{{t|a}}"</nowiki> {{ut1|t}} gives "{{t|a}}".
The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:
:<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.
<!--T:41-->
<nowiki>{{Thankyou|Me|all your effort}}</nowiki>


:Example: <nowiki>"{{t|[[a|b]]}}"</nowiki> gives "{{t|[[a|b]]}}".
<!--T:42-->
:Example: <nowiki>"{{t|[[a|b]] }}"</nowiki> gives "{{t|[[a|b]] }}".
would produce this result:


(In this case the space ''has'' effect.)
<!--T:43-->
{{Thankyou|Me|all your effort}}


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}}.
<!--T:44-->
'''Note:''' identifying parameters by order (with <code><nowiki>{{{1}}}</nowiki></code>, etc) works ''only'' with anonymous parameters. If your page identifies any parameter by number or name, as shown below, this method will no longer be available to the template which receives them.


==Restrictions on parameter values==
=== Numbered parameters === <!--T:45-->
* If a parameter value contains an equals sign, the parameter name must be put explicitly, even when it is 1, 2, 3, etc.


* [[Help:Template_names%2C_variable_names_and_parameters_depending_on_a_variable_or_parameter#Impossibility_to_include_the_content_of_a_template_in_a_template_name.2C_variable_name.2C_or_parameter_value|A parameter value cannot contain a template tag.]]
<!--T:46-->
To pass in parameters by number, identify each parameter when passing it:


* 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.
<!--T:47-->
<nowiki>{{Thankyou|2=Me|1=your friendship}}</nowiki>


* If the expression for a parameter value contains a pipe character (|) which is not part of a piped link and within an image tag, it has to be put in nowiki tags.
<!--T:48-->
This time, template <code><nowiki>{{Thankyou}}</nowiki></code> receives parameters <code><nowiki>{{{1}}}=your friendship</nowiki></code> and <code><nowiki>{{{2}}}=Me</nowiki></code>, though they have been supplied in inverse order, and produces:
{{Thankyou|2=Me|1=your friendship}}


* The expression for a parameter value cannot contain the variable localurl with query string, due to the pipe character.
=== Named parameters === <!--T:49-->


*A parameter value can be quite long, see [[Help:Long parameter demo]].
<!--T:50-->
The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:


==Named vs. numbered parameters==
<!--T:51-->
Advantage of numbering:
<pre><nowiki>'''A little thank you...'''
*in the template call no name or "=" is needed; this means less to type and also less to memorize or lookup (on the other hand, one has to memorize or lookup the order); an exception is when the value contains "=": then the other equals sign is needed, with the parameter number in front.
for {{{reason}}}.
*numbers are international, both in the template tags and in the parameter tags in the template, no parameter name has to be translated (alternatively, one can e.g. use English names, only editors would see them and need to understand them)
hugs, {{{signature}}}</nowiki></pre>


Advantages of naming:
<!--T:52-->
*the order of specifying the values does not matter
Within the template, we use <code><nowiki>{{{reason}}}</nowiki></code> and <code><nowiki>{{{signature}}}</nowiki></code> to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it:
**a dummy unnamed parameter for comment and for formatting the template call can be inserted at any time, without planning in advance or renumbering (in the case of unplanned insertion of a dummy parameter between numbered ones, the text has to contain an equals sign; this  works even if there is a newline somewhere before the "=", although a newline is not allowed in a parameter name).
*the template is easier to understand
*some special techniques use the fact that multiple assignments of values to the same parameter in the same template call is allowed and results in the last value being used; this implies that at least one parameter name is specified explicitly in the template call


==Mix of named and unnamed parameters==
<!--T:53-->
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.
<nowiki>{{Thankyou|signature=Me|reason=being who you are}}</nowiki>


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}}.
<!--T:54-->
In this case, template <code><nowiki>{{Thankyou}}</nowiki></code> receives parameters <code><nowiki>{{{reason}}}=being who you are</nowiki></code> and <code><nowiki>{{{signature}}}=Me</nowiki></code> and produces:


==Template tag lay-out==
<!--T:55-->
{{Thankyou|signature=Me|reason=being who you are}}
The advantage of using named parameters in your template, besides also being flexible in the order parameters can be passed, is that it makes the template code much easier to understand if there are many parameters.


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}},
=== Default values === <!--T:56-->


<pre>
<!--T:57-->
{{t3d  |a|b|c| 1
If you transclude a template that expects parameters, but do not provide them, in this way:
        |d|e|f| 2
        |g|h|i| 3
}}
</pre>


gives
<!--T:58-->
{{t3d  |a|b|c| 1
<nowiki>{{Thankyou}}</nowiki>
        |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.
<!--T:59-->
in the numbered parameters example above you would get the following:


Example:
<!--T:60-->
{{Thankyou}}


Using {{tim|Chess position}}
<!--T:61-->
Since no parameters were passed in, the template presents the parameters themselves, instead of their respective values. In these cases, it may be useful to define ''default'' values for the parameters, i.e. values that will be used if no value is passed in. For example, if the template contents are changed to:


<pre>
<!--T:62-->
{{chess position|=
<pre><nowiki>'''A little thank you...'''
for {{{reason|everything}}}.
hugs, {{{signature|Me}}}</nowiki></pre>


8 |rd|nd|bd|qd|kd|bd|nd|rd|=
<!--T:63-->
7 |pd|pd|pd|pd|pd|pd|pd|pd|=
then <code><nowiki>{{{reason|everything}}}</nowiki></code> defines that if no parameter <code>{{{reason}}}</code> is provided, then the value <code>everything</code> will be used. Similarly, <code><nowiki>{{{signature|Me}}}</nowiki></code>, defaults parameter <code>{{{signature}}}</code> to value <code>Me</code>. Now, transcluding the template again without passing any parameter, results in the following:
6 |  |  |  |  |  |  |  |  |=
</translate>
5 |  |  |  |  |  |  |  |  |=
<!-- strictly speaking, this example is false, because the template call was changed to obtain the desired effect - but the effect is the same if the template is defined as described -->
4 |  |  |  |  |  |  |  |  |=
<translate>
3 |  |  |  |  |  |  |  |  |=
<!--T:64-->
2 |pl|pl|pl|pl|pl|pl|pl|pl|=
{{Thankyou|reason=everything|signature=Me}}
1 |rl|nl|bl|ql|kl|bl|nl|rl|=
    a  b  c  d  e  f  g  h


|30}}
== Control template inclusion == <!--T:65-->
</pre>


which gives
<!--T:66-->
By default, a template's content is displayed in its entirety, both when viewed directly and when included in another page. However, you can control which parts of a template will be seen and included by the use of the <tvar|noinclude><code><nowiki><noinclude></nowiki></code></> and <tvar|includeonly><code><nowiki><includeonly></nowiki></code></> tags.


{{chess position|=
<!--T:67-->
Anything between <tvar|noinclude_start><code><nowiki><noinclude></nowiki></code></> and <tvar|noinclude_end><code><nowiki></noinclude></nowiki></code></> will be seen only when the template's page is being viewed directly, but not when it is included in another page. This is useful when you want to include text or code in a template that you do not want to propagate to any pages which include it, such as:


8 |rd|nd|bd|qd|kd|bd|nd|rd|=
<!--T:68-->
7 |pd|pd|pd|pd|pd|pd|pd|pd|=
* [[Special:MyLanguage/Help:Categories|Category]] links when categorizing the template itself
6 |  |  |  |  |  |  |  |  |=
</translate>
5 |  |  |  |  |  |  |  |  |=
<translate>
4 | |  |  |  |  |  |  |  |=
<!--T:69-->
3 |  |  |  |  |  |  |  |  |=
* [[Special:MyLanguage/Help:Links#Interlanguage links|interlanguage links]] to similar templates in other languages
2 |pl|pl|pl|pl|pl|pl|pl|pl|=
</translate>
1 |rl|nl|bl|ql|kl|bl|nl|rl|=
<translate>
    a  b  c  d  e  f  g  h
<!--T:70-->
* Explanatory text about how to use the template


|30}}
<!--T:71-->
Likewise, anything between <tvar|includeonly_start><code><nowiki><includeonly></nowiki></code></> and <tvar|includeonly_end><code><nowiki></includeonly></nowiki></code></> will be processed and displayed only when the page is being included, but not when the template page is being viewed directly, and is useful in situations such as:


==Effect of an undefined parameter==
<!--T:72-->
Suppose that template B contains a tag {{{p}}} and is called by page A without specifying a value for p. Thus, after substitution of the specified parameter values, the tag is still there. Then the possibilities include:
* [[Special:MyLanguage/Help:Categories|Categorizing]] pages which include the template. ''Note:'' when changing the categories applied by a template in this fashion, the categorization of the pages which include that template may not be updated until some time later: this is handled by the {{mediawiki|Manual:Job queue|job queue}}. To force the re-categorization of a particular page, open that page for editing and save it without changes.
</translate>
<translate>
<!--T:73-->
* Ensuring that the template's code is not executed when viewing the template page itself. Typically this is because it expects parameters, and its execution without parameters has an undesired result.


*The tag is not inside a template tag
<!--T:74-->
**the tag is included in A as part of B, but in A it is not interpreted as parameter (in the case that A is called by another page)
Everything outside <tvar|noinclude><code><nowiki><noinclude></nowiki></code></> and <tvar|includeonly><code><nowiki><includeonly></nowiki></code></> tags is processed and displayed normally; that is, both when the template page is being viewed directly and when the template is included in another page.
*The tag is in a parameter value expression, say the first parameter tag which, after substitution of specified parameter values of B is still in the tag of template C
**the first two consecutive closing braces in a template tag are taken to be those for the template tag, in this case the first two of the three closing braces of the  parameter tag; the third closing brace of the parameter tag and the rest of the template tag, including the two closing braces which were intended to be those of the template tag, and possible further parameter names and values, equals signs, and pipe characters, are now interpreted as outside the template tag. Examples (where {{tcw|t2}} is an example of C, and the expressions are the result of  substitution of specified parameter values of B):
***<nowiki>{{t2|a|{{{1}}}}}</nowiki> gives {{t2|a|{{{1}}}}}, i.e., the second parameter is taken to be "{{{1", and "}}}" is taken to be text after the template call.
***<nowiki>{{t2|{{{1}}}|a}}</nowiki> gives {{t2|{{{1}}}|a}}, i.e., the first parameter is taken to be "{{{1", the second parameter is undefined, and "}|a}}" is taken to be text after the template call.
***<nowiki>{{t2|{{{1}}}|{{{2}}}}}</nowiki> gives {{t2|{{{1}}}|{{{2}}}}}, similar to the previous case.


Consider the same examples, with t2 replaced by {{tcw|tc}} (without parameters)
== Organizing templates == <!--T:75-->
*<nowiki>{{tc|a|{{{1}}}}}</nowiki> gives {{tc|a|{{{1}}}}}
*<nowiki>{{tc|{{{1}}}|a}}</nowiki> gives {{tc|{{{1}}}|a}}
*<nowiki>{{tc|{{{1}}}|{{{2}}}}}</nowiki> gives {{tc|{{{1}}}|{{{2}}}}}


This is an example of a case where it matters that a parameter value is defined, even though the value does not make any difference in the result. Such parameters are typically assigned the [[wikipedia:en:String_%28computer_science%29#Formal_theory|empty string]]  or a comment.
<!--T:76-->
For templates to be effective, users need to find them, and find out how to use them.  


To avoid braced parameter names in a page, or even worse, messy results  as demonstrated, one may want to specify the parameter even if it is not applicable, etc., with a tag like <nowiki>{{ab|langs=}}</nowiki>, <nowiki>{{ab|langs=-}}</nowiki>, or <nowiki>{{ab|langs='''N.A.'''}}</nowiki>.
<!--T:77-->
To find them, users can:
# Click '''{{int:specialpages}}''' > '''{{int:allpages}}'''
# In the '''{{int:namespace}}''' list, choose '''Template''' and click '''{{int:allpagessubmit}}'''.


[[Help:Template_names, variable_names_and_parameters_depending_on_a_variable_or_parameter#A_parameter_name_depending_on_a_parameter|Branching techniques]] can be applied to handle special cases such as an empty string as parameter value, but with the current software there is no way to control the result in the case of an undefined parameter.
<!--T:78-->
To give usage information, include an example like this one on the template page:


==Template page==
<pre><nowiki>
The following are identical:
<noinclude>
*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)
== Usage == <!--T:79-->
*the rendering of the template on a page that calls it without parameters
Welcome users:
{{Thankyou|reason=your reason|signature=your signature}}
</noinclude>
</nowiki></pre>


In simple cases this corresponds to treating the parameter tags as ordinary text, for example:
<!--T:80-->
Then, an editor can simply copy and paste the example to use the template.


<nowiki>"{{t2}}"</nowiki> (using [[Template:t2]] containing {{msgnw:t2}}) gives {{t2}}
== Copying from one wiki to another == <!--T:81-->


Similarly, if part of the parameters is undefined, only those show up as {{{''parameter number or name''}}}.  
<!--T:82-->
Templates often require [[m:Help:CSS|CSS]] or other templates, so users frequently have trouble copying templates from one wiki to another. The steps below should work for most templates.


However, if a parameter is in another template call, then, as a result of what was explained in the previous section, the template page seemingly demonstrates an error in the wikitext: it is confusing, as a result of parsing the code differently from when it is used, i.e. called with a parameter value.
=== MediaWiki code === <!--T:83-->


E.g., the template page of {{tcw|t2a1}} shows {{t2a1}}, and that of {{tcw|t2a3}} shows {{t2a3}}.
<!--T:84-->
If you have import rights (Specifically importupload) on the new wiki:
</translate>
<translate>
<!--T:85-->
# Go to [[Special:Export]] on the original wiki, and download an .xml file with the complete history of all necessary templates, as follows:
</translate>
<translate>
<!--T:86-->
#* Enter the name of the template in the big text box, e.g. "Template:Welcome". Pay special attention to capitalization and special characters — if the template name isn't exactly correct, the export may still occur but the .xml file will not have the expected data.
</translate>
<translate>
<!--T:87-->
#* Check the box "{{int:export-templates}}".
</translate>
<translate>
<!--T:88-->
#* '''Uncheck''' the box "{{int:exportcuronly}}".
</translate>
<translate>
<!--T:89-->
#* Click "{{int:export-submit}}".
</translate>
<translate>
<!--T:90-->
# Go to [[Special:Import]] on the new wiki and upload the .xml file.


Thus, for getting informed about the contents of a template, just looking at the template page is insufficient (because it is ambiguous) and misleading. Note also that variable tags are not shown, but evaluated. However, some formatting aspects may show up which are not readily seen from the edit box.
<!--T:91-->
If you don't have import rights on the new wiki:


==Template talk page==
<!--T:114-->
The template page is of a different nature than other pages. It is not for explaining anything to the reader, but for defining the template for the system (this is done by the wikitext, the rendered page is just a by-product). Therefore the template talk page has an especially important role. It typically consists of two parts:
# Go to the template you want to copy from the original wiki. Go to the edit page, and copy all the wikitext
*the first part explaining the template and its parameters
# On the new wiki, go to the page with the same name as the template you copied. Hit create/edit and paste the wikitext you copied. In the edit summary of each template, link to the original page for attribution.
**preferably examples are given of template calls (put them in nowiki tags) and the results (put the template call without nowiki tags in the wikitext)
# Back in the original wiki at the edit window, below the edit box, look at the list of "Templates used on this page". For each template listed follow these instructions. Also do that for any template used by any of these templates, and so on.
*the second part used for the ordinary talk page function


In complicated cases, "subst:" can he very helpful in the explanation, see below.
<!--T:115-->
This will copy the entire code necessary, and will suffice for some templates.</translate>
<translate>
<!--T:117-->
If it doesn't work also check for red links listed under "Pages transcluded onto the current version of this page:", below the edit box. If there are any repeat the above steps for these as well.


==A parameter value depending on parameters==
<!--T:118-->
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:
After sucessfully importing the template and all its linked templates from the other wiki, edit it to change customisations to suit your wiki. For example to change a logo, remove redundant categories or red links.
*<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]].
=== Extensions === <!--T:100-->


==Remarks==
<!--T:101-->
<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.
An extension often used in templates is ParserFunctions. Visit page {{mediawiki|Help:Extension:ParserFunctions|ParserFunctions}} and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the {{mediawiki|Extension:ParserFunctions|ParserFunctions extension}}. To install it, you'll need system admin access to the server of your MediaWiki installation.


:Example: <nowiki>{{t2|2=a}}</nowiki> gives {{t2|2=a}}.
<!--T:102-->
Another dependency that may be used in templates, especially those on Wikipedia, is Lua. Having <tvar|invoke><code><nowiki>{{#invoke: }}</nowiki></code></> in template code is a good sign for it. In case it's used, you need to install the {{mediawiki|Extension:Scribunto|Scribunto extension}} and system admin access is required too. See that page for more instructions about installing and using the extension.


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]].
=== CSS and JavaScript code === <!--T:103-->


Parameter names (as opposed to template names) are case-sensitive, even with respect to the first letter, and spaces are distinguished from underscores. The [[wikipedia:en:String_%28computer_science%29#Formal_theory|empty string]] is also a valid parameter name, see [[Template:T empty string as parameter name]].
<!--T:104-->
Besides MediaWiki code, many templates make use of CSS and some rely on JavaScript to work fully. If the copied templates are not behaving as expected, this may be the cause. To copy the required CSS and JavaScript to your wiki you'll normally need to have admin privileges, because you'll be editing system messages in the "MediaWiki:" namespace.


Parameter 02 is distinguished from parameter 2: using {{timc|t pl0}}, <nowiki>{{</nowiki>t pl0|5|6|7}} gives {{t pl0|5|6|7}}
<!--T:105-->
# Look for the use of CSS classes (text like <code>class="foobar"</code>) in the template text. If those classes appear in "MediaWiki:Common.css" or "MediaWiki:Monobook.css" on the original wiki, copy those classes to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.</translate>
<translate>
<!--T:106-->
# If the copied template is still not working as expected, check if there is code in "MediaWiki:Common.js" or "MediaWiki:Monobook.js" on the original wiki. If so, you can try copying it to "MediaWiki:Common.js" on the new wiki. Normally, it is a good idea to only copy code from trusted sources, and first browsing the code to identify and select the parts that are relevant. You may find comments that can serve as clues to identify the functionality of each part.


Note that parameters in the [[MediaWiki namespace]] (interface messages) are written differently: as $1, $2, etc., see [[Meta-Wikimedia:MediaWiki namespace text]].
== See also == <!--T:107-->


==Equals sign in parameter value==
<!--T:108-->
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 accidently, this parameter is likely to be unused, hence harmless, but the intended implicit parameter 1, 2, or 3, etc., gets no value.  
* <tvar|external>{{ll|Help:External searches}}</> – a template special use case example
</translate>
<translate>
<!--T:109-->
* <tvar|magic>{{ll|Help:Magic words}}</> – fancy stuff you may find in some templates
</translate>
* {{ll|Help:Parser functions in templates}}
<translate>
<!--T:111-->
* [[<tvar|meta>m:Special:MyLanguage/Help:Template</>|m:Help:Template]] – contains a much more thorough manual on how exactly templates function
</translate>
<translate>
<!--T:112-->
** [[<tvar|meta>m:Special:MyLanguage/Help:Advanced templates</>|m:Help:Advanced templates]] – describes even more advanced techniques such as dynamic template calls and variable variables
</translate>
<translate>
<!--T:113-->
* [[<tvar|meta>m:Help:Embed page</>|m:Help:Embed page]] – embedding pages from [[<tvar|ns>Special:MyLanguage/Help:Namespaces</>|namespaces]] other than <code>Template:</code>.
</translate>
<translate>
<!--T:119-->
* [[Manual:Importing Wikipedia infoboxes tutorial|Importing Wikipedia infoboxes tutorial]]
</translate>


: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" &mdash;  it reads like so: Use template TTT, assign "b" to the perameter "a", use the value "c" for your first ''implicit'' perameter <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" &mdash; 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 perameter <nowiki>{{{3}}}, {{TTT|1=a=b|2=c|d}}</nowiki>, "d" would be assigned to "3" as first ''implicit'' parameter without a value.
[[Category:Help{{translation}}|Templates]]
 
[[Category:Template{{translation}}]]
The same applies if a parameter value depends on a parameter, and ''may'' therefore contain an equals sign. Compare:
[[Category:MediaWiki for site admins{{translation}}]]
 
*<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.
 
Examples:
 
[[Template:en]]
*<nowiki>{{msgnw:en}}</nowiki> gives {{msgnw:en}}
*<nowiki>{{en}}</nowiki> gives {{en}}
 
[[Template:t2]]
*<nowiki>{{msgnw:t2|a|b}}</nowiki> gives {{msgnw:t2|a|b}}
*<nowiki>{{t2|a|b}}</nowiki> gives {{t2|a|b}}
 
[[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 shows:
<pre>
#Amsterdam
#Rotterdam
#The Hague
</pre>
 
==Link for editing a template==
 
An edit page shows template tags in the edit box, not the wikitext of the included templates. A list of links is provided to the templates used on the page. To edit a template, one can follow the link, and go to the edit page of the template.
 
More convenient is to have a special link for editing the template on the calling page. This link can be put in the template itself. Since these edit links are similar for various templates, it can be made a template itself, with the name of the template as parameter. See e.g. [[wikipedia:en:template:edit]] and [[wikipedia:en:Train]].
 
On Meta there are:
 
For editing a template ({{{1}}} is the name without prefix):
*{{timc|Ed}}
*{{timc|Edi}}
*{{timc|Eda}}
*{{timc|Edde}}
 
For editing any file ({{{1}}} is the full page name):
*{{timc|Edit}}
 
For a template containing only one paragraph or less, without links or images, one can also make the whole contents link to the edit page, see {{timc|ec}}.
 
This is e.g. convenient for editing an individual entry in a table, without having to search in the wikitext of the table.
 
{{Help:Editing sections of included templates}}
 
==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 [[wikipedia:en:Subroutine#Advantages_of_subprograms|advantages of subprograms]].
 
Typical applications are:
*[[wikipedia:en:Wikipedia:Template messages|template messages]] such as [[wikipedia:en:Template:stub]]
*[[wikipedia:en:Wikipedia:Navigational templates|navigational templates]] such as [[wikipedia:en:Template:europe]]
*[[wikipedia:en: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 [[wikipedia: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 [[wikipedia:en:Wikipedia:Template_messages#Taxobox_components_.28Taxons.29|applied for taxoboxes]], see e.g. [[wikipedia:en:Template:Familia]] and [[wikipedia: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 [[wikipedia:en:Template:Chess position]] and [[wikipedia:en: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:Template names, variable names and parameters depending on a variable or parameter]]'''.
 
===Navigational templates===
 
A common, simple type of navigational template in a hierarchy of pages is one with the names, with links, of a parent page and child pages. It is placed on all the pages it lists. Thus a page that is not at the top or bottom of the hierarchy has two templates, one with its parent and siblings, and one with its children.
 
Variations also occur, such as pages having two parents, etc.
 
[[Self link]]s do not give any complication: they are neatly displayed in [[{{NAMESPACE}}:{{PAGENAME}}|bold]] and not as link. However, templates should have the correct page names and not rely on redirecting, since the feature does not work in that case.
 
A navigational template may also just allow going forward and backward in a linear sequence, e.g. [[wikipedia:en:Template:Pope]]. For the predecessor of the first and the successor of the last a message like "none" can be used. This is not applicable in the case of a cycle, such as the days of the week.
 
===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:
*[[wikipedia:en:Wikipedia:Village pump]] with six component pages, e.g. [[wikipedia:en:Wikipedia:Village pump (technical)]]
*daily a page like [[wikipedia: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. [[wikipedia: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]]
*{{pi|w:en|en.wikipedia.org||2005 English cricket season (1-14 June)}}
**{{pi w|w:en|en.wikipedia.org|Wikipedia|WikiProject_Cricket/Cambridge_UCCE_v_Middlesex_1-3_June_2005}}
 
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 [[wikipedia:en: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 [[wikipedia: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 ([[wikipedia: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. [[wikipedia:en:Template:Ld]], [[wikipedia:en:Template:L2d]], and [[wikipedia:en: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, 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 [[m:Template: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|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||}}
 
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.
 
==Images in templates==
 
'''New:''' In a template, image code parameters can contain template parameters, e.g.
 
<nowiki>{{timgi|wiki|50|small}} {{timgi|wiki|100|medium}} {{timgi|wiki|200|large}}</nowiki><br>
<nowiki>{{timg1|st|50|pqr}} {{timg1|st|100|st}}</nowiki>
 
using {{tcw|Timgi}} and {{tcw|Timg1}}
 
give
 
{{timgi|wiki|50|small}} {{timgi|wiki|100|medium}} {{timgi|wiki|200|large}}
{{timg1|st|50|pqr}} {{timg1|st|100|st}}
{{clr}}
For cases for which an image is not applicable or available, use a dummy image such as [[wikipedia:en:Image:No image.png]].
 
For an image in a template that should be different for each page in which the template occurs, there are also the following possibilities:
 
*a parameter value can contain the whole image code: <nowiki>{{t|[[Image:Tst.png|50px|bullet]]}}</nowiki> gives {{t|[[Image:Tst.png|50px|bullet]]}}
*in projects where external images can be embedded, the url of the image can be a parameter: <nowiki>{{t|http://meta.wikimedia.org/upload/2/27/Tst.png}}</nowiki> gives {{t|http://meta.wikimedia.org/upload/2/27/Tst.png}}
 
*the image is given a name of the form ''prefix''<nowiki>{{PAGENAME}}</nowiki>''suffix'' (with a suffix, consisting of the file name extension and possibly more, and possibly also a prefix). See examples below. Note that there is little flexibility: changing the name of a page in which the template occurs requires uploading the image again, with the corresponding name.
 
==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]]
 
===Interwiki links===
 
Templates can contain interwiki links; this normally means all pages using the template will have the interwiki, which is usually a bad thing unless some fancy footwork is done. To link ''just the templates'' across wikis, use the <tt><nowiki><noinclude></nowiki></tt> tag. For example:
 
<tt><nowiki><noinclude></nowiki><br><nowiki>
[[de:Vorlage:Korrekter Titel]]</nowiki><br><nowiki>
[[wikipedia:en:Template:Wrongtitle]]</nowiki><br><nowiki>
[[es:Plantilla:Título erróneo]]</nowiki><br><nowiki>
[[fr:Modèle:titre incorrect]]</nowiki><br><nowiki>
</noinclude></nowiki></tt>
 
See also below.
 
==External links==
 
With external link style much is possible. A blank space in a parameter value or template content that is substituted in a URL is not taken as ending the URL, but converted to %20.
 
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"
 
==Navigational images==
 
The following applies in projects where external images can be embedded.
 
Examples where [[Help:Navigational image|the label contains an image]] and depends on the parameter or template:
 
*<nowiki>{{tlil|http://meta.wikimedia.org/upload/2/27/Tst.png}}</nowiki> (using [[Template:tlil]]) gives {{tlil|http://meta.wikimedia.org/upload/2/27/Tst.png}}
*<nowiki>[[help:link|c{{timgurl}}d]]</nowiki> gives [[help:link|c{{timgurl}}d]]
 
With external link style:
*<nowiki>{{tle|insert in target|http://meta.wikimedia.org/upload/2/27/Tst.png}}</nowiki> (using [[Template:tle]]) gives {{tle|insert in target|http://meta.wikimedia.org/upload/2/27/Tst.png}}
*<nowiki>[http://en.wikipedia.org c{{timgurl}}d]</nowiki> gives [http://en.wikipedia.org c{{timgurl}}d]
 
Notes:
*the c and d are used to show the more general case, without them the label ''is'' instead of ''contains'' an image, with perhaps <nowiki>[[help:link|{{timgurl}}]]</nowiki> giving [[help:link|{{timgurl}}]] the more useful kind.
*the target can be made variable like above.
 
 
 
==EasyTimeline code with a template parameter is not allowed==
 
{{Template talk:Etl}}
 
 
 
==Noinclude and includeonly==
 
Anything between <nowiki><noinclude> and </noinclude></nowiki> will be processed and
displayed only when the page is being viewed directly, not included.
Possible applications are:
 
*categorising templates
* [[Help:Interwiki_linking#Interlanguage_link_.28software_feature.29|interlanguage link]]s to similar templates in other languages (Otherwise such a link appears in the edge of the page that includes the template, giving the impression that the link is to a version in the other language of the referring page instead of the template. The link label depends only on the sister project that is linked to, not on the page; it is set in the configuration of the project, typically  it is the name of the language in that language. The target is only shown in the hover box and the status bar, depending on the browser.) - see also above.
*pages in the [[MediaWiki namespace]]
*explanatory text about how to use the template
 
<nowiki>The converse is <includeonly>. Text between <includeonly> and
</includeonly></nowiki> will be processed and displayed only when the page is
being included. The obvious application is:
 
* Adding all pages containing a given template to a category
 
Note that the usual update problems apply -- if you change the
categories inside a template, the categories of the referring pages
won't be updated until those pages are edited.
 
==A category tag in a template; caching problem==
 
If a template contains the code indicating that it is in a [[help:category|category]], this does not only put that template in the category, but also the pages that include the template. Thus this is typically not applied to categorize templates, but to conveniently put pages in a category, based on the template they use. For example, this page includes [[Template:H:h]] and as a result is in [[:Category:English documentation]].
 
The page that contains the template correctly lists the categories to which it belongs. However, adding or deleting a category tag in the template does not add or delete the listings on the category page of pages that use the template, until some edit is made in the page that uses the template.
 
In other words:
 
*lists of categories a page is in, are up-to-date
*lists of pages in a category are based on the situation just after the last edit of the pages
 
Since adding or removing a category or template tag is obviously an edit there is only a complication when a page is indirectly added to or deleted from a category, through a change in a template the page uses.
 
To refresh category pages with respect to the listing of a particular page (adding or deleting the entry), a [[Help:Editing#Dummy_edit|null edit]] can be applied to that page: just applying section edit and saving without changes.
 
Thus, a basic principle of templates, that changing the contents of a template automatically changes the pages that contain the template, does not extend to these pages being correctly listed on category pages. It still works, with a delay, for sets of pages which are all frequently edited anyway, for other purposes.
 
In other cases the principle is violated. What remains is that just making null edits is somewhat less cumbersome than changing category tags in each page.
 
In conclusion, adding or deleting a category tag in a template that is already in use should be avoided unless one is willing to make the null edits, or is willing to except the (possibly very long) delay during which there is no consistency between the category pages and the category listing on the pages in the categories.
 
==Newline in a template==
A single newline works like a space, except in a list, there it breaks the list. Since templates containing a single paragraph or less could be called from a list, such templates should avoid single newlines.
 
==Indentation or list code at the beginning of template content==
Each of the characters :;*# at the beginning of template content is interpreted as if it is at the beginning of a line, unless nowiki tags are used:
<pre>
abc{{p1|def}}ghi
 
abc{{p1|:def}}ghi
 
abc{{p1|;def}}ghi
 
abc{{p1|*def}}ghi
 
abc{{p1|#def}}ghi
</pre>
give:
 
abc{{p1|def}}ghi
 
abc{{p1|:def}}ghi
 
abc{{p1|;def}}ghi
 
abc{{p1|*def}}ghi
 
abc{{p1|#def}}ghi
 
==Caching==
 
When a page in the Template namespace is edited, this automatically clears the cache of any pages which use that template. However, there are cases of indirect dependence where a page may still not be up-to-date, even after pressing Ctrl-F5 or similar. This is e.g. the case for pages (typically template talk pages) referring to [[wikipedia:en:Template:Tc]], containing <nowiki>{{msgnw:{{PAGENAME}}}}</nowiki>, probably because the template page name referred to depends on a variable.
 
In order to update a page in such cases, use "action=purge", in a URL like {{SERVER}}{{localurl:{{NAMESPACE}}:{{PAGENAMEE}}|action=purge}}
 
==Server efficiency==
When a template is edited, this automatically clears the cache of any pages which use that template. When a page is viewed, and the template in it has changed since last time the page was viewed, the HTML has to be reconstructed. (For logged-in users part of the HTML is different for each one, but if pages are mostly viewed by users who are not logged in, we can focus on them).
 
If a template is called from m pages and edited with a frequency f, while the pages are viewed with a frequency g, then the relative amount of work for rebuilding the page (as opposed to taking it from the cache) is per unit time:
**if f<g: mf (for each time the template is edited, all pages have to be rebuilt)
**if f>g: mg (each time a page is viewed, it has to be rebuilt)
 
If the frequency of changing the template varies, then for each time interval the amount of work has to be considered separately, and added. For example, if a template is called from 1000 pages, which are each viewed once a day, and if each month four edits in a row are made to the template, then the first causes a lot of work (1000 page refreshings) and the other three very little. Compare this with having the four edits spread over the month, i.e. every week a single edit is made to the template, this causes 1000 page refreshings every week.
 
==subst==
 
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. Subst can also be applied to a variable.
 
Example: <nowiki>{{subst:t2|[[a]]|[[image:tst.png]]}}</nowiki> {{ut1|t2}} will give the wikitext <nowiki>start-[[a]]-middle-[[image:tst.png]]-end</nowiki>, which is rendered just like <nowiki>{{t2|[[a]]|[[image:tst.png]]}}</nowiki>: {{t2|[[a]]|[[image:tst.png]]}}.
 
The replacement of a template tag by wikitext does not automatically work recursively: if the template calls another template, that tag is not replaced. One can subsequently add "subst:" again, thus doing manual recursion. This stepwise replacement can be useful for analyzing and explaining the working of templates calling other templates. See e.g. [[Template talk:Lop]]. It can also be used to create a page independent of templates, as a way to archive the rendered page and allow a permalink to it. See also [[bugzilla:2777]], a request for a version of subst that automatically works recursively.
 
If you want to use a modified version of the resulting wikitext, you have to save the page with the unmodified wikitext first, then edit it, because when applying preview the subst code is not yet replaced in the edit box. In the edit summary of the temporary edit you can put "tmp" to indicate that you have not made an error but are still working on the page.
 
In the absence of parameters, an alternative that clutters the edit history less is, of course, copying the text from the edit box of the template page: then it can be edited before saving. Another method is using msgnw without saving, and copying the preview result into the edit box.
 
When subst is applied to a template:
*a [[variable]] in the template is copied as such in the wikitext.
*if the template call contains a variable, subst has to be applied to that variable as well, otherwise the first two closing braces of the parameter are interpreted as closing braces of the template.
 
Applying subst to a variable works like applying it to a template. E.g. a timestamp:
 
<nowiki>{{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}, &nbsp;&nbsp;&nbsp;{{subst:CURRENTTIME}} (UTC)</nowiki>
 
giving:
10 March 2005, &nbsp;&nbsp;&nbsp;08:23 (UTC)
 
One might be tempted to put this in a template, but then it would display the time of creation of the template; on the other hand, without the "subst:", it would give the time of loading the page with the timestamp. Neither is  suitable for a timestamp, which should give the time of saving the new referral to the template (like <nowiki>~~~~~</nowiki> does).
 
Usage of a template through "subst:" does not show up in page histories, backlinks, etc.
 
===Input box===
Similar to applying "subst:" to include page A in page B, in the case that B does not exist yet and that A does not have parameters, is using an [[Help:inputbox|inputbox]] to create B, preloading A. A difference is that the wikitext can be edited before saving.
 
==Rewriting a template that calls other templates in a self-contained form==
If a template calls other templates one may want to rewrite it in a self-contained form (i.e., which does not need other templates) in order to use a copy of it in another project, without copying more templates.
 
For example, {{tcw|t2a1}}, calling {{tcw|t2}}, could be written "start-a-middle-{{{1}}}-end".
 
To do this semi-automatically, one cannot use subst without specifying a parameter, or with {{{1}}} as parameter: the result of that is like the template page t2a1 shows: {{t2a1}}. In fact this is how the confusing rendering of a template page can be analysed.
 
Instead, one can apply subst with parameter "{{{1}.}.}." and obtain {{t2|a|{{{1}.}.}.}}. If we now remove the three dots again, we have the content of t2a1 in a self-contained form: start-a-middle-{{{1}}}-end.
 
===Case where a template name depends on a variable or parameter===
If template A calls a template whose name depends on a variable or parameter, subst can only be applied for specific values of that variable or parameter, each resulting in a separate new version of template A, not for the general case. Saving e.g. {{subst:H:f <nowiki>{{</nowiki>NAMESPACE}}|langs=|enname=Template}} does not change the wikitext, which is rendered as just {{subst:H:f {{NAMESPACE}}|langs=|enname=Template}}, because the assumed template name contains braces, therefore the system concludes that the template does not exist.
 
==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.
 
==Math tags==
MediaWiki templates, variables and parameters cannot be used within math tags, see [[Template talk:Demo of attempt to use parameters within TeX]].
 
==A template looked upon as a subroutine==
The wikitext of a page can be looked upon as a list of [[wikipedia:en:Statement (programming)|statements]], in which case a template is a statement-type [[wikipedia:en:subroutine|subroutine]]. Alternatively the wikitext is a [[wikipedia:en:String (computer science)|string]] [[wikipedia:en:Expression (programming)|expression]], in which case a template is a string function.
 
In the first view, a<nowiki>{{</nowiki>b}}c is a sequence of three statements, in the second a [[wikipedia:en:concatenation|concatenation]] of three strings.
 
In both views, the b in <nowiki>{{</nowiki>a|b}} is a string expression: the value of an input parameter of type string; however, the expression involves only concatenation, not (as one might think in the second view) any string function.
 
A subroutine is selected based on evaluating the string expression for its name (so this is done at "run-time"), so the process seems like that of an [[wikipedia:en:Interpreter (computing)|interpreter]].
 
The subroutine is the only [[wikipedia:en:Category:Programming constructs|programming construct]] / [[wikipedia:en:control flow|flow control]] construct.
 
There are no [[wikipedia:en:variable|variables]]/constants or [[wikipedia:en:Assignment (computer science)|assignment statements]], except in the framework of subroutine parameters. The only operator (see [[wikipedia:en:operator|operator]] and [[wikipedia:en:Operator (programming)|operator (programming)]]) is concatenation.
 
A template itself can also be considered an operator, with the number of parameters being the [[wikipedia:en:arity|arity]].
 
==Simple computations==
Simple additions and multiplications can be performed based on concatenation, using the [[wikipedia:en:unary numeral system|unary numeral system]]: when 2 is represented by 11 and 3 by 111, then concatenation gives 11111, which can be converted  to 5, see {{tim|Add}} and {{tim|Multiply}}. An additional parameter allows not only to display the result 5, but also to call a template of choice with parameter 5.
 
Required auxiliary templates:
*for every value ''m'' used as first operand a template calc''m''  containing <nowiki>{{</nowiki>calc{{{2}}}{{{1}}}|''m digits "1"'' |{{{3}}}}}, see e.g. {{tim|Calc2}}
*for every value ''n'' used as second operand for addition a template calcA''n''  containing <nowiki>{{</nowiki>count''n digits "1"''{{{1}}}|{{{2}}}}}, see e.g. {{tim|CalcA3}}
*for every value ''n'' used as second operand for multiplication a template calcB''n''  containing <nowiki>{{</nowiki>count''n times {{{1}}}''|{{{2}}}}}, see e.g. {{tim|CalcM3}}
*for every result r a template count''r digits "1"'' containing {{{{{1}}}|''r'' }}, see e.g. {{tim|Count11111}}
*the template that one wants to call with the result as parameter; if one just wants to display the result, or use it in a link target, or in an image tag, one uses {{tim|X1}}
 
In view of the maximum length of 255 for page names, the maximum result can be 250.
 
Note that for any computation one can prepare an array with all results, but the amount of work to do that is proportional to the number of pairs (m,n) that one wants to use, while above the amount of work is only proportional to the sum of the number of values of m, n, and r.
 
==Linking to a template==
 
Unfortunately, when ''linking'' to a template instead of embedding it, transfer of parameters is not possible. This would be useful e.g. for the following:
*for a given image, allow links to pages each showing the image in a particular size, without having prepared a separate page for each size
*for given geo-coordinates, link to a page such as [[wikipedia:Template:Mapeu]] with links to maps of that location; this would be like the [[wikipedia:Wikipedia:WikiProject_Geographical_coordinates#Special_page|Special:Mapsources feature]], e.g. http://kvaleberg.com/wiki/index.php/Special:Mapsources/59_55_N_10_44_E,  proposed as an extension of MediaWiki, except that no computations are possible, but, on the other hand, it would enable multiple editable "Map sources pages" in the form of templates.
*[[Template:Table of Wikimedia projects]] provides a table of links to similar pages on all Wikimedia projects, depending on a parameter, e.g. all main pages, or all lists of templates. A page would be convenient with links to various of such tables, made on the fly.
 
Coming close to this is a page with links like [http://meta.wikimedia.org/w/index.php?title=Templates_of_Wikimedia_projects&action=edit&section=1 Template  pages] and
[http://meta.wikimedia.org/w/index.php?title=Templates_of_Wikimedia_projects&action=edit&section=2 Template talk pages]. These section edit links would each give access to one table through the preview. The whole page could be very large in rendered form, because the whole page would never have to be rendered (although on saving the system would attempt to do so even if it not desired). As far as the variation of the parameter for the last part of the URLs is easy, such as just changing the namespace number, not all tables need to be present, one can change the parameter in the edit box and get the desired table on the fly by the preview.
 
==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
 
Extra possibilities of linking:
*the name can depend on the content of templates; this is not possible for the name of an embedded page, not even indirectly: the name can depend on a parameter, but a parameter value or name can also not depend on the content of a template
*the page referred to can be outside the project (interwiki and external linking)
 
Extra possibility of embedding:
*there can be parameters, i.e. a single page can have different rendered content; this is not possible with a link; note that embedding a page with a link still does not allow the linked page to depend on parameters (see also the previous section)
 
Other differences:
*the syntax {{..}} or {{..|..|..}}, etc., versus [[..]] or [[..|..]], with the pipe character having a different meaning
*obviously the difference between having the pages rendered combinedly or separately
*for embedding the default namespace is the template namespace (for the main namespace prefix a colon), for linking the default is the main namespace; prefixing a colon is also allowed, so one can use <nowiki>{{</nowiki>NAMESPACE}}:...
 
==Developing templates and template tags==
When adding a parameter to a template that is in use, one can first adjust the tags, and then the template. During the work in progress, the template keeps working properly, but the new parameter becomes effective only at the end.
 
Alternatively, a new template name is used for the new version, and while adjusting template tags, the template name is also changed (for convenience, this is typically a small change, e.g. one letter or digit is added or changed). The advantage is that during the work in progress, the new parameter becomes effective on a page as soon as the tag has been adjusted. Also, the backlinks of both templates show the pages that are done and that still have to be done.
 
To accommodate future expansion of a template, involving more parameters, one can in advance add parameters with the empty string as their value to the template tags. If unnamed parameters are used, one does to have to plan in advance the meaning of the new parameters.
 
==Backlinks==
The backlinks feature in principle shows which pages call a given template. However:
*Not more than 500 are listed; as a workaround, the set can be divided in subsets of not more than 500, each calling  an auxiliary page which either calls or redirects to the main template; the backlinks of the auxiliary templates are together the pages that use the main template; one has to record the names of the auxiliary templates, because they are also part of the large set of backlinks of the main template, of which only 500 are shown. Alternatively, if it has advantages to use different versions of a template anyway, this also helps staying under the limit of 500.
 
For example, [[wikipedia:en:Template:Void]] has more than the 500 backlinks it shows. Thus it is not clear what templates use it (in fact, [[wikipedia:en:Template:Infobox CVG]] is one of them; that alone has more than 500 backlinks).
 
==Templates in different versions of MediaWiki==
 
===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, [http://meta.wikimedia.org/w/index.php?title=Help:Template&oldid=86075#Default_values_of_parameters].
 
===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.
 
In versions 1.3 and 1.4 the prefix "msg:" is optional and ignored.
 
'''From version 1.5 the tag <nowiki>{{</nowiki>msg:abc}} refers to  Template:Msg:abc !'''
 
== Revision history of pages containing templates ==
 
Pages in the stored [[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.
 
 
 
==Reserved template names==
According to the [[m:Help:MediaWiki help policy|MediaWiki help policy]] some template names are reserved for use in the Help namespace. This allows copying without modification of the wikitext of help pages across projects. This wikitext contains template tags of the form "<nowiki>{{</nowiki>h:''rest of name ''}}" which allow project-specific content within help pages, using project-specific templates.
 
Template names referred to by such tags are reserved, follow the link  [[Template:h:f]] to see the full template name in this project in the case that ''rest of name '' is "f ":
*for English projects with [[m:Help:Page_name#Case-sensitivity_of_the_first_character|case-insensitivity of the first character]]: template names starting with "Template:H:".
*for other projects with case-insensitivity of the first character: template names starting with "Vorlage:H:" and "Modèle:H:", etc.
*for projects with case-sensitivity of the first character: template names starting with "Template:h:", "Vorlage:h:", "Modèle:h:", etc.
*for Hebrew "H:''rest of name '':" comes to the left of the Hebrew word for template תבנית , see [[:he:Template:h:f]], except that if ''rest of name '' is the empty string than ":H:" instead of "H::" comes to the left of תבנית , see [[:he:Template:h:]]. The reserved names are those of which the canonical form of the URL starts with "<nowiki>http://</nowiki>he.wikipedia.org/wiki/%D7%AA%D7%91%D7%A0%D7%99%D7%AA:H:" and similar for the other Hebrew projects.
 
Note that "case-insensitivity of the first character" refers to the first character after the first colon, not after the second.
 
 
 
==Examples==
 
*{{tiw|en|Stub}} - message
*{{tiw|en|Europe}}
*{{tiw|en|JuneCalendar}}
* templates with many parameters and images depending on <nowiki>{{PAGENAME}}</nowiki>:
**{{tiw|en|Infobox Countries}}
***<nowiki>{{PAGENAME}}</nowiki>_flag_large.png
***<nowiki>{{PAGENAME}}</nowiki>_coat_of_arms_large.png
***Location<nowiki>{{PAGENAME}}</nowiki>.png
**{{tiw|fr|Tableau pays}}
***<nowiki>{{PAGENAME}}</nowiki>_flag_medium.png
***<nowiki>{{PAGENAME}}</nowiki>_armoirie.png
***Location<nowiki>{{PAGENAME}}</nowiki>.png
**{{tiw|cy|Dosbarthiad_biolegol}}
*{{tiw|eo|El}} - small, often used image
*{{tiw|fr|Familia}} - translation
*{{tiw|fr|Peru region table}} - intermediate template for translating the parameter names, containing e.g. "superficie = {{{area}}}"
*{{tib|en|GeneralChemTOC}}
*{{tib|en|New}}
*{{tim|H:f}} - calls one of several templates, dependent on namespace
*{{tiw|en|H:f}} - calls one of many templates, depending on pagename.
*{{tiq|en|Qotd}} - quote of the day - the content of the template is changed daily
*{{tic|Potd}} - picture of the day - the pictures can be prepared in advance, because the template shows an image, depending on <nowiki>{{CURRENTYEAR}}, {{CURRENTMONTH}}, and {{CURRENTDAY}}</nowiki>, and, for the caption, calls one of many templates, depending on these and also on a parameter for the language; unlike the quote of the day above, the rendered image is changed at the fixed time of 0:00 UTC.
*article of the day on the English Wikipedia: see [http://en.wikipedia.org/w/index.php?title=Special%3AAllpages&from=Today&namespace=4 list] - included as a template on the main page.
 
==See also==
*[[Help:Template names, variable names and parameters depending on a variable or parameter]]
*[[Help:Inputbox]] - preload a "template" when creating a new page.
*[{{SERVER}}{{localurl:Special:Allpages|namespace=10}} Templates in this project]
*[[Templates of Wikimedia projects]]
*[[wikipedia:Wikipedia:Template namespace]]
*[[Help:What links here]]
*[[MediaWiki namespace]] (includes the use of templates in versions before 1.3)
*[[MediaWiki_1.3_comments_and_bug_reports#Bugs_related_to_templates]]
*[[Extended template syntax]] (proposal)
*[[Message substitution]] - Earlier technical discussions
*[http://bugzilla.wikimedia.org/show_bug.cgi?id=531 Bug 531] - use of  templates to link to a talk page, even in languages where one can not just concatenate the equivalent of "talk" to the namespace name; create a new variable NAMESPACE_TALK.
*[[wikipedia:en:Wikipedia:Transclusion costs and benefits|Transclusion costs and benefits]] - explanation and opinion
*[[wikipedia:en:Wikipedia:Avoid using meta-templates|The cost of editing a  template called by many pages]] - explanation and opinion
*[[Template talk:Include demo]]

Latest revision as of 23:21, 9 October 2014

<languages/> {{#invoke:Template translation|renderTranslatedTemplate|uselang=|namespace=|template={{#if:|{{{template}}}|PD Help Page}}|noshift=}} <translate> If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play.</translate> <translate> Unlike extensions and media files, there is no central repository for templates. Templates can be newly written or to save duplicating work already done, exported from another wiki e.g. Wikipedia, and then imported into the target wiki.

Creation

Templates are standard wiki pages whose content is designed to be transcluded (embedded) inside other pages. Templates follow a convention that the name is prefixed with "Template:", assigning it to that namespace; besides this, you can create them like any other wiki page.

The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents:

Hello! Welcome to the wiki.

you'll have created your first template! If you then insert the code:

{{Welcome}}

in any other page, when that page is viewed the text "Hello! Welcome to the wiki." will appear instead of {{Welcome}}. The template content is "transcluded" into the other page, i.e. it is integrated in the page.

You can then insert {{Welcome}} at any point of any page where you wish to welcome someone. Suppose it is used in 100 pages. If you then change the template contents to:

Hi there! Welcome to this wonderful wiki.

and revisit any of the 100 pages where the template was used, you'll see the new text instead of the original one. In this way, you have changed the content of 100 pages without editing them, because the template is transcluded into these pages.

This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.

Usage

Templates can be used in other pages in these ways:

  • {{Name}} — as described above, this link will be dynamically replaced by the current content of [[Template:Name]] at the time the page with the template link is loaded. The link will remain unchanged in the page's source.

</translate> <translate>

  • {{subst:Name}} — when this template link is used, it will be replaced once and for all with the content of [[Template:Name]] as of the time the page with the link is saved: a copy of the contents of [[Template:Name]] will be substituted for the template link. The contents are then a part of the including page, and can be edited normally, separately from the original. Note: changes to the source template page will not be propagated to the page with the template link.

</translate> <translate>

</translate> <translate>

  • {{msgnw:Name}} includes the template in a form that displays it as raw wiki syntax (the way <nowiki> does) when the page containing it is fetched.

In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so:

  • {{Template:Pagename}} includes [[Template:Pagename]]

</translate> <translate>

  • {{Foo:Pagename}} includes [[Foo:Pagename]]

</translate> <translate>

  • {{:Pagename}} includes [[Pagename]]

</translate> <translate>

    • {{subst::Pagename}} replaces itself with the contents of [[Pagename]]

If no such namespace exists, the full title is assumed to be a template:

  • {{Foo:Bar}} includes [[Template:Foo:Bar]]

Parameters

To enrich the mechanism of transclusion, MediaWiki allows parameters to be passed to a template when it is transcluded. Parameters allow the template to produce different contents or have different behaviors.

Suppose you wish to insert a little thank you note in the talk page of other users, such as:

Template:Thankyou

The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user be able to thank any other user, for any reason whatsoever.

So that the note will look similar everywhere it is used, you can define a template called Template:Thankyou, for example. Although the note should look similar whenever a user thanks another user, its specific contents (i.e. the reason and the signature) will be different. For that reason, you should pass them as parameters. If we ignore the remaining elements to format the box and place the image, the core content of the template will be this:

'''A little thank you...'''
for {{{1}}}.
hugs, {{{2}}}

Notice the use of <tvar|1>{{{1}}}</> and <tvar|2>{{{2}}}</>. This is the way to identify, within templates, the parameters that will be passed in when the template is used. Note that, within the template, each parameter is surrounded by three braces: <tvar|braces>{{{ }}}</>. This is different from normal template name usage.

When using the template on a page, you fill in the parameter values, separated by a "pipe" character (<tvar|pipe>|</>). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named.

Anonymous parameters

To pass in anonymous parameters, list the values of those parameters sequentially:

{{Thankyou|all your effort|Me}}

In this case, the {{Thankyou}} template receives parameters {{{1}}}=all your effort and {{{2}}}=Me, producing:

Template:Thankyou

The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:

{{Thankyou|Me|all your effort}}

would produce this result:

Template:Thankyou

Note: identifying parameters by order (with {{{1}}}, etc) works only with anonymous parameters. If your page identifies any parameter by number or name, as shown below, this method will no longer be available to the template which receives them.

Numbered parameters

To pass in parameters by number, identify each parameter when passing it:

{{Thankyou|2=Me|1=your friendship}}

This time, template {{Thankyou}} receives parameters {{{1}}}=your friendship and {{{2}}}=Me, though they have been supplied in inverse order, and produces: Template:Thankyou

Named parameters

The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:

'''A little thank you...'''
for {{{reason}}}.
hugs, {{{signature}}}

Within the template, we use {{{reason}}} and {{{signature}}} to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it:

{{Thankyou|signature=Me|reason=being who you are}}

In this case, template {{Thankyou}} receives parameters {{{reason}}}=being who you are and {{{signature}}}=Me and produces:

Template:Thankyou The advantage of using named parameters in your template, besides also being flexible in the order parameters can be passed, is that it makes the template code much easier to understand if there are many parameters.

Default values

If you transclude a template that expects parameters, but do not provide them, in this way:

{{Thankyou}}

in the numbered parameters example above you would get the following:

Template:Thankyou

Since no parameters were passed in, the template presents the parameters themselves, instead of their respective values. In these cases, it may be useful to define default values for the parameters, i.e. values that will be used if no value is passed in. For example, if the template contents are changed to:

'''A little thank you...'''
for {{{reason|everything}}}.
hugs, {{{signature|Me}}}

then {{{reason|everything}}} defines that if no parameter {{{reason}}} is provided, then the value everything will be used. Similarly, {{{signature|Me}}}, defaults parameter {{{signature}}} to value Me. Now, transcluding the template again without passing any parameter, results in the following: </translate> <translate> Template:Thankyou

Control template inclusion

By default, a template's content is displayed in its entirety, both when viewed directly and when included in another page. However, you can control which parts of a template will be seen and included by the use of the <tvar|noinclude><noinclude></> and <tvar|includeonly><includeonly></> tags.

Anything between <tvar|noinclude_start><noinclude></> and <tvar|noinclude_end></noinclude></> will be seen only when the template's page is being viewed directly, but not when it is included in another page. This is useful when you want to include text or code in a template that you do not want to propagate to any pages which include it, such as:

  • Category links when categorizing the template itself

</translate> <translate>

</translate> <translate>

  • Explanatory text about how to use the template

Likewise, anything between <tvar|includeonly_start><includeonly></> and <tvar|includeonly_end></includeonly></> will be processed and displayed only when the page is being included, but not when the template page is being viewed directly, and is useful in situations such as:

  • Categorizing pages which include the template. Note: when changing the categories applied by a template in this fashion, the categorization of the pages which include that template may not be updated until some time later: this is handled by the {{ #ifeq:
 JonnyBWiki

| MediaWiki | job queue | job queue }}. To force the re-categorization of a particular page, open that page for editing and save it without changes. </translate> <translate>

  • Ensuring that the template's code is not executed when viewing the template page itself. Typically this is because it expects parameters, and its execution without parameters has an undesired result.

Everything outside <tvar|noinclude><noinclude></> and <tvar|includeonly><includeonly></> tags is processed and displayed normally; that is, both when the template page is being viewed directly and when the template is included in another page.

Organizing templates

For templates to be effective, users need to find them, and find out how to use them.

To find them, users can:

  1. Click Special pages > All pages
  2. In the Namespace: list, choose Template and click Go.

To give usage information, include an example like this one on the template page:

<noinclude>
== Usage == <!--T:79-->
Welcome users:
{{Thankyou|reason=your reason|signature=your signature}}
</noinclude>

Then, an editor can simply copy and paste the example to use the template.

Copying from one wiki to another

Templates often require CSS or other templates, so users frequently have trouble copying templates from one wiki to another. The steps below should work for most templates.

MediaWiki code

If you have import rights (Specifically importupload) on the new wiki: </translate> <translate>

  1. Go to Special:Export on the original wiki, and download an .xml file with the complete history of all necessary templates, as follows:

</translate> <translate>

    • Enter the name of the template in the big text box, e.g. "Template:Welcome". Pay special attention to capitalization and special characters — if the template name isn't exactly correct, the export may still occur but the .xml file will not have the expected data.

</translate> <translate>

    • Check the box "Include templates".

</translate> <translate>

    • Uncheck the box "Include only the current revision, not the full history".

</translate> <translate>

    • Click "Export".

</translate> <translate>

  1. Go to Special:Import on the new wiki and upload the .xml file.

If you don't have import rights on the new wiki:

  1. Go to the template you want to copy from the original wiki. Go to the edit page, and copy all the wikitext
  2. On the new wiki, go to the page with the same name as the template you copied. Hit create/edit and paste the wikitext you copied. In the edit summary of each template, link to the original page for attribution.
  3. Back in the original wiki at the edit window, below the edit box, look at the list of "Templates used on this page". For each template listed follow these instructions. Also do that for any template used by any of these templates, and so on.

This will copy the entire code necessary, and will suffice for some templates.</translate> <translate> If it doesn't work also check for red links listed under "Pages transcluded onto the current version of this page:", below the edit box. If there are any repeat the above steps for these as well.

After sucessfully importing the template and all its linked templates from the other wiki, edit it to change customisations to suit your wiki. For example to change a logo, remove redundant categories or red links.

Extensions

An extension often used in templates is ParserFunctions. Visit page {{ #ifeq:

 JonnyBWiki

| MediaWiki | ParserFunctions | ParserFunctions }} and check if any of the functions listed there are used in the templates you've copied. If so, you have to install the {{ #ifeq:

 JonnyBWiki

| MediaWiki | ParserFunctions extension | ParserFunctions extension }}. To install it, you'll need system admin access to the server of your MediaWiki installation.

Another dependency that may be used in templates, especially those on Wikipedia, is Lua. Having <tvar|invoke>{{#invoke: }}</> in template code is a good sign for it. In case it's used, you need to install the {{ #ifeq:

 JonnyBWiki

| MediaWiki | Scribunto extension | Scribunto extension }} and system admin access is required too. See that page for more instructions about installing and using the extension.

CSS and JavaScript code

Besides MediaWiki code, many templates make use of CSS and some rely on JavaScript to work fully. If the copied templates are not behaving as expected, this may be the cause. To copy the required CSS and JavaScript to your wiki you'll normally need to have admin privileges, because you'll be editing system messages in the "MediaWiki:" namespace.

  1. Look for the use of CSS classes (text like class="foobar") in the template text. If those classes appear in "MediaWiki:Common.css" or "MediaWiki:Monobook.css" on the original wiki, copy those classes to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.</translate>

<translate>

  1. If the copied template is still not working as expected, check if there is code in "MediaWiki:Common.js" or "MediaWiki:Monobook.js" on the original wiki. If so, you can try copying it to "MediaWiki:Common.js" on the new wiki. Normally, it is a good idea to only copy code from trusted sources, and first browsing the code to identify and select the parts that are relevant. You may find comments that can serve as clues to identify the functionality of each part.

See also

  • <tvar|external>Template:Ll</> – a template special use case example

</translate> <translate>

  • <tvar|magic>Template:Ll</> – fancy stuff you may find in some templates

</translate>

<translate>

  • [[<tvar|meta>m:Special:MyLanguage/Help:Template</>|m:Help:Template]] – contains a much more thorough manual on how exactly templates function

</translate> <translate>

    • [[<tvar|meta>m:Special:MyLanguage/Help:Advanced templates</>|m:Help:Advanced templates]] – describes even more advanced techniques such as dynamic template calls and variable variables

</translate> <translate>

  • [[<tvar|meta>m:Help:Embed page</>|m:Help:Embed page]] – embedding pages from [[<tvar|ns>Special:MyLanguage/Help:Namespaces</>|namespaces]] other than Template:.

</translate> <translate>

</translate>

[[Category:Help{{#ifeq:

 {{#titleparts:help:template|1|-1}}

| {{#titleparts:help:template|1|-1}} | | /{{#titleparts:Help:Template|1|-1}} }}|Templates]] [[Category:Template{{#ifeq:

 {{#titleparts:help:template|1|-1}}

| {{#titleparts:help:template|1|-1}} | | /{{#titleparts:Help:Template|1|-1}} }}]] [[Category:MediaWiki for site admins{{#ifeq:

 {{#titleparts:help:template|1|-1}}

| {{#titleparts:help:template|1|-1}} | | /{{#titleparts:Help:Template|1|-1}} }}]]