Help:Template
Template:HhSummary of this page
Notes:
- Including the same template more than five times in the same page works from version 1.4, now on (almost?) all Wikimedia sites.
- 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 that page on another project.
Contents
- 1 General
- 2 Parameters
- 3 Viewing the contents of a template
- 4 msgnw
- 5 Link for editing a template
- 6 Usage
- 7 Restrictions and possibilities
- 8 Piped link with parameters
- 9 Interlanguage link (software feature)
- 10 A category tag in a template; caching problem
- 11 Caching
- 12 subst
- 13 Redirection
- 14 Linking to a template
- 15 MediaWiki version 1.2.6
- 16 Revision history of pages containing templates
- 17 Template producing the name of the talk namespace of the calling page
- 18 See also
- 19 Examples
General
The template namespace is the namespace with prefix "Template:".
A page in this namespace is called a template. The contents of such a page (either fixed or depending on parameters and/or variables) is designed to be useful for insertion in other pages (transclusion, creating a compound document).
The syntax for insertion of the page "Template:name" is {{name}}. This is called a template tag.
This reference in the wikitext causes insertion of the template content when the referring page is rendered. Various equivalent terms are:
- calling the template
- referring to the template
- including the template
- using the template
If the page "Template:name" does not exist, then {{name}} works as [[Template:name]], a link to a non-existing page, leading to the edit page. Thus, one way of making a template is putting the tag first, and then following the link.
If name starts with a valid namespace prefix, or a colon, then it is not prefixed with "Template:". This way any page can be used as template (a colon at the start refers to the main namespace). In the case of an image or category, it gives the description part. Using a page outside the template namespace as template can be useful for:
- quoting interface messages in the MediaWiki namespace
- experimenting with templates in subpages of one's userpage
- using the description of an image for a caption
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.
As usual the name is case sensitive except (on most projects) for the first letter, and a blank space is equivalent with an underscore.
Recursive inclusion is possible.
A variable in a template is evaluated after insertion in the referring page, i.e. if {{PAGENAME}} occurs in the wikitext of a template, it is rendered as the name of the referring page, not the name of the template.
Supported in version 1.4 and higher:
A list of templates used on a page, all with links, is given on the edit page. Even on a section edit page the whole list is given, i.e. also the templates used in other sections.
Parameters
(supported in versions 1.3 and higher)
Template parameters are either named or numbered. The syntax is {{templatename|parname1=parvalue1|parname2=parvalue2}} with {{{parname}}} tags in the template, or {{templatename|parvalue1|parvalue2}} with {{{1}}}, {{{2}}}, etc. tags in the template. (Note that parameter tags in the template have three braces rather than two.)
Thus the template mechanism involves two kinds of replacements:
- template calls (double braced) are replaced by template content
- parameters (triple braced) are replaced by parameter values
From the Aug 2004 version: a parameter value can contain a piped link; the "|" in such a link is not taken as parameter separator.
Example: "{{t| [[a|b]] }}" (using Template:t) gives "Template:T".
On rendering, surplus parameter values (including named parameters that do not occur in the template) are ignored: {{t|a|b}} gives Template:T.
In the case no value is specified for a parameter, the result still depends on that parameter: {{t2|a}} (using Template:t2) gives Template:T2. This can be applied if the referring page is itself also a template.
E.g., if a template {{hc}} is put in wikitext with its parameter "langs" unspecified, it shows {{{langs}}}. To avoid this one may want to specify the parameter anyway like {{hc|langs=}}, {{hc|langs=-}}, and {{hc|langs='''N.A.'''}}.
{{t2||a}} makes the first parameter equal to the empty string rather than leaving it undefined: it gives Template:T2. By treating the second unnamed parameter as a parameter with the name "2", the first unnamed parameter can be left undefined: {{t2|2=a}} gives Template:T2.
Another case where the implicit name of an unnamed parameter has to be given explicitly, is when its value contains an equals sign: Template:TTT with {{TTT|a=b|c}} gives Template:TTT (non-existing parameter a is assigned the value b), while {{TTT|1=a=b|2=c}} gives Template:TTT (the first parameter is assigned the value "a=b").
Parameter names are case-sensitive, even with respect to the first letter, even though the template page renders them starting with a capital, and spaces and underscores are not allowed in them.
Note that parameters in the MediaWiki namespace (interface messages) are written differently: as $1, $2, etc., see Meta-Wikimedia:MediaWiki namespace text.
Viewing the contents of a template
To see the contents of a template, one needs to look in the edit box and not just on the page itself, because on the template page:
- not the variables themselves, but their values are shown; e.g. the page en:Template:Otheruses with the content
- : <span class="dablink">''For other uses, see [[{{PAGENAME}} (disambiguation)]].''</span>
- shows
- For other uses, see Otheruses (disambiguation).
In fact, the rendering on the template page of the wikitext may show more conversions, which can make it confusing and less useful to consider.
msgnw
In wikitext, use "msgnw:" after the opening braces to display the contents. E.g. {{en}} and {{msgnw:en}} show Template:En and [[:Template:En]], respectively.
Link for editing a template
The edit link on each page does not allow editing of template text appearing on that page. Therefore it can be desirable to have a special link for that purpose. 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. en:template:edit and en:Train.
Sometimes an edit link is not really needed, e.g. in a template containing a list of provinces in a country, which has been carefully checked for errors and has a format that is not likely to require any change for a long time.
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.
Typical applications are:
- template messages such as en:Template:stub
- navigational templates such as en:Template:europe
- 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 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 applied in Wiktionaries for names of languages, terms like "noun", etc.; it is also applied for taxoboxes, see e.g. en:Template:Familia and fr:Modèle:Familia.
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.
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 links do not give any complication: they are neatly displayed in 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. 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.
Duplication of content
Duplication of content (whether using a template or not), e.g. putting a section on the relationship of A and B both in the page about A and in that about B, may, depending on the project, not be recommended. It may be confusing and irritating for the reader, especially if the repeated text can not be recognized as such by a special lay-out. It may be better to use links instead, with possible duplication limited to only a short remark about the relationship.
The recommended usages mentioned above do not tend to have these disadvantages.
It is argued that in more cases the nuisances can be mitigated, and that there are substantial benefits in some circumstances. See Help_talk:Template#Duplication of content
Restrictions and possibilities
A pair of double or triple braces inside a pair of double braces
An attempt to use a template with a name dependent on the content of another template does not work: {{{{tctc}}}}, using Template:Tctc, gives the text {{{{tctc}}}}, not the content of {{tc}}, which is "Template:Tc".
An attempt to use a template with a parameter value dependent on the content of another template does not work: {{t|{{tc}}}}, using Template:T, gives Template:T, the end braces of template tc are taken as end braces of t.
A template name and a parameter value can both depend on a parameter or variable: {{tt|t|efg}} using Template:Tt, produces "Template:Tt", {{{{NAMESPACE}}}}, using Template:Help, gives "{{{{NAMESPACE}}}}".
With a template name depending on a parameter a kind of "case statement" is possible, where data used in multiple cases need to be put only once. With a list of similar case statements, line content that is common to all lines and just differs by case also needs to be put only once for each case, i.e. not for each line separately.
Example
For example, a template can be made that, depending on a parameter, produces either just a standard text or also an additional text. This is done by having it call another template, of which the name is a parameter; one version of the other template contains the additional text, the other version is blank.
(Note that a blank page cannot be created directly; first create a non-blank page, e.g. with just one character, then edit the page to blank it.)
Example:
{{variable text demo|full_demo}} gives the long version of a text: Template:Variable text demo
while {{variable text demo|short_demo}} gives the short version: Template:Variable text demo
This construction uses the following templates:
- Template:Variable text demo (see wikitext) - a template that produces a list, with, depending on a parameter ("comprehensiveness toggle"), either long versions of all items, or short versions; it contains all data that is not common to all lines
- Template:Variable line demo - defines the pattern of a list item, including which parts are only in the full version and which also in the short version; it contains the text that is common to all lines
- the difference between the two versions is obtained with two templates, one for each value of the toggle, and with its name:
- Template:Full demo - just reproduces the parameter
- Template:Short demo - ignores the parameter and produces nothing
Images in templates
New: In a template, image code parameters can contain template parameters, e.g.
{{timgi|wiki|50|small}} {{timgi|wiki|100|medium}} {{timgi|wiki|200|large}}
{{timg1|st|50|pqr}} {{timg1|st|100|st}}
using Template:Timgi and Template:Timg1
gives
Template:Timgi Template:Timgi Template:Timgi Template:Timg1 Template:Timg1 Template:Clr For an image in a template that should be different for each page in which the template occurs, there are also the following possibilities:
- the whole image code can be a parameter: {{t|[[Image:Tst.png|thumb|right|50px|bullet]]}} gives Template:T
- in projects where external images can be embedded, the url of the image can be a parameter: {{t1|http://meta.wikimedia.org/upload/2/27/Tst.png}} gives Template:T1
- the image is given a name of the form prefix{{PAGENAME}}suffix (with a suffix, consisting of the file name extension and possibly more, and possibly also a prefix), because variables are allowed in image names, but parameters are not. See examples below. Note that changing the name of a page in which the template occurs requires uploading the image again, with the corresponding name.
Templates will break the "Image links" section of the image description page. The methods discussed above will generally fail to show any page in the "Image links". If the specific image is hardcoded into the template, as in Template:timgc, the template will be listed in the "Image links".
Thus for Image:Tst.png the "Image links" section lists Template:Timgc, but neither Template:T nor Template:T1,
Internal links
New: 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:
- {{tli|insert in target|insert in label}} (using Template:tli) gives Template:Tli
- [[a{{tciit}}b|c{{tciil}}d]] (using Template:tciit and Template:tciil) gives [[aTemplate:Tciitb|cTemplate:Tciild]]
- {{tli|insert also in target|insert in label}} (using Template:tli) gives Template:Tli
- [[a{{tciit}}e|c{{tciil}}d]] (using Template:tciit and Template:tciil) gives [[aTemplate:Tciite|cTemplate:Tciild]]
- {{la|Help:Tabl|tabl}} (using Template:La) gives: Template:La
New: If, inside the double square brackets, a template is called that produces a text with a pipe character, it is interpreted as such, e.g. [[help:li{{tp}}]] (using Template:tp) gives [[help:liTemplate:Tp]].
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:
- {{tle|insert in target|insert in label}} (using Template:tle) gives Template:Tle
- {{tle2|insert in target}} (using Template:tle2) gives "Template:Tle2"; note that brackets separated from the http:// are not recognized as belonging together:
- [{{tle2|insert in target}} label] gives [[[:Template:Tle2]] label]
- [{{tle2|insert in target}}] gives [[[:Template:Tle2]]]
- {{tle3|insert in target}} (using Template:tle3) gives Template:Tle3
- [http://a{{tciit}}b c{{tciil}}d] gives Template:Tciitb cTemplate:Tciild
- http://a{{tciit}}b gives "http://aTemplate:Tciitb"
- [http://a{{tciit}}b] gives Template:Tciitb
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:
- {{tae|http://www.}} (using Template:tae) gives Template:Tae
Examples of second restriction:
- [{{th}}add at the end] (using Template:th) gives [[[:Template:Th]]add at the end]
- "{{th}}add at the end" gives "Template:Thadd at the end"
The following applies in projects where external images can be embedded.
Examples where the label contains an image and depends on the parameter or template:
- {{tlil|http://meta.wikimedia.org/upload/2/27/Tst.png}} (using Template:tlil) gives Template:Tlil
- [[help:link|c{{timgurl}}d]] gives [[help:link|cTemplate:Timgurld]]
With external link style:
- {{tle|insert in target|http://meta.wikimedia.org/upload/2/27/Tst.png}} (using Template:tle) gives Template:Tle
- [http://en.wikipedia.org c{{timgurl}}d] gives cTemplate:Timgurld
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 [[help:link|{{timgurl}}]] giving [[help:link|Template:Timgurl]] the more useful kind.
- the target can be made variable like above.
EasyTimeline code with a template parameter is not allowed
Piped link with parameters
Taking into account the restrictions above, and using Template:Pl, a convenient way of making a piped link with parameters is {{pl|target page|label}}, giving Template:Pl. It is equivalent with [[target page|label]], giving label, which only allows parameters in the label and not in the target page name (but one can use the variable PAGENAME there).
In particular target page and label can each be a parameter.
For a really external piped link (also to other Wikimedia projects), use the ordinary [http://target_web_page label], giving label.
If you want to use internal link style but need more flexibility than is possible with the variable PAGENAME, make redirects from the initial target pages to the real ones, e.g. en:Current head of state of Netherlands, resulting from the pagename Netherlands of en:Netherlands.
Interlanguage link (software feature)
A template is not very suitable to put an interlanguage link in: 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.
For the same reason pages in the MediaWiki namespace are not very suitable to put an interlanguage link in.
A category tag in a template; caching problem
If a template contains the code indicating that it is in a 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:Hh 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.
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 en:Template:Tc, containing {{msgnw:{{PAGENAME}}}}, probably because the template page name referred to depends on a variable.
In order to update a page in such cases (and also after creation of a category which was referenced but not defined before), use "action=purge", in a URL like https://wiki.midrange.com/index.php?title=Help:Template&action=purge
subst
Putting "subst:" after the double opening braces causes this code in the wikitext to be replaced by the wikitext of the template when the referring page is saved.
Example: {{subst:rcnote}}
From version 1.4 subst can have parameters.
If you want to use a modified version of the wikitext in the template, 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.
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.
Subst can also be applied to a variable itself; this works like applying it to a template. E.g. a timestamp:
{{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}, {{subst:CURRENTTIME}} (UTC)
giving:
25 July 2004, 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 ~~~~~ does).
Alternatives for subst
- Use {{..}}, then preview, and copy the result to the edit box, replacing {{..}}. One may prepare a template specially for this usage.
- Similar, with msgnw.
Redirection
When a page called for inclusion is a redirect page, the redirect target is included instead.
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
- the buttons such as edit, watch, talk, history, "what links here", etc., refer to the referring page. There is no button to go to the target page
- 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 w:Plantanos with w:Plantanos - demo of alternative for redirect.
Linking to a template
Unfortunately, when linking to a template instead of embedding it, transfer of parameters is not possible. This would have been useful e.g. to link to an enlarged image.
MediaWiki version 1.2.6
In MediaWiki version 1.2.6, make a page MediaWiki:mytemplate and refer to it with {{msg:mytemplate}}.
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. The prefix "msg:" in 1.3 is optional and ignored.
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.
Template producing the name of the talk namespace of the calling page
In English language projects the name of a talk namespace is usually simply "{{NAMESPACE}} talk", here giving Help talk. With this a link can be produced: [[{{NAMESPACE}} talk:{{PAGENAME}}]] giving Help talk:Template
In other languages the same or something similar may not be possible. For such cases, see Bug 531 on using templates to effectively create a new variable NAMESPACE_TALK.
See also
- Templates in this project
- w:Wikipedia:Template namespace
- Help:What links here
- MediaWiki namespace (not up-to-date)
- MediaWiki_1.3_comments_and_bug_reports#Bugs_related_to_templates
- Extended template syntax (proposal)
- Message substitution - Earlier technical discussions
- Bug 531 How to use templates to create a new magic variable NAMESPACE_TALK.
Examples
(On each template page, click "What links here" to see the list of pages that use the template.)
- en:Template:Stub - message
- en:Template:Europe
- en:Template:JuneCalendar
- templates with many parameters and images depending on {{PAGENAME}}:
- en:Template:Infobox Countries
- {{PAGENAME}}_flag_large.png
- {{PAGENAME}}_coat_of_arms_large.png
- Location{{PAGENAME}}.png
- fr:Modèle:Tableau pays
- {{PAGENAME}}_flag_medium.png
- {{PAGENAME}}_armoirie.png
- Location{{PAGENAME}}.png
- cy:Template:Dosbarthiad_biolegol
- en:Template:Infobox Countries
- eo:Ŝablono:El - small, often used image
- fr:Modèle:Familia - translation
- http://wikibooks.org/wiki/Template:GeneralChemTOC
- http://wikibooks.org/wiki/Template:New
- Quote of the day - daily changed content