Personal tools

Help:Template

From MozillaWiki

Jump to: navigation, search

MozillaWiki Handbook
Software help for MozillaWiki — see page histories for older versions.
Handbook Contents | Other help

Summary of this page

Notes:

  • Including the same template more than five times in the same page works from version 1.4, now on all Wikimedia sites.
  • The master version of this page, m:Help:Template, is on Meta-Wikipedia; template demo's work there, but may not work if you are viewing a copy of that page on another project.

Contents

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

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.

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 startbend (compare start[[a|b]]end giving startbend: with the template the text after the link is not made part of the link label).

On rendering, surplus parameter values (including named parameters that do not occur in the template) are ignored: {{t|a|b}} gives startaend. In the case no value is specified for a parameter, the result still depends on that parameter: {{t2|a}} (using Template:t2) gives startamiddle{{{2}}}end, This can be applied if the referring page is itself also a template. However, {{t2||a}} makes the first parameter equal to the empty string rather than leaving it undefined: it gives startmiddleaend. Thus, to allow the first parameter to remain a parameter in the result while the value of the second parameter is specified, the first parameter has to have a name.

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.

Default values of parameters

The default value for a parameter, i.e. what is produced if the template is called with no parameter value specified, is as follows: it is rendered as if the inner two pairs of braces are references to a template with the name of the parameter.

Thus {{{Parameter name}}} shows up as follows:

  • if Template:Parameter name does not exist, as a link to the edit page
  • if it exists, in accordance with the usual inclusion mechanism.

To avoid confusing default values, use parameter names which are not in use as the name of an unrelated template, and create for each parameter name a corresponding template; the default value of a parameter will then be the contents of that template enclosed in a single pair of braces.

Examples:

  • the contents could be the name in double braces and nowiki-tags. This can also be applied for numbered parameters: it has been done on Meta for parameters 1 and 2, see Template:1 and Template:2
  • Template:langs contains "-", giving the default "{-}"

Note that if the same parameter name is used in multiple templates, the default values are the same. In some cases this may be convenient, but it may also be a reason to choose unique parameter names.

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:

:''See [[Wikipedia:{{PAGENAME}}]] for the usage of "{{PAGENAME}}" in Wikipedia.''
shows
See Wikipedia:In Wikipedia for the usage of "In Wikipedia" in Wikipedia.
  • not the parameters themselves, but their default values are shown.

In fact, the rendering on the template page of the wikitext may show even more conversions, which can make it confusing and less useful to consider. This does not affect the working.

msgnw

In wikitext, use "msgnw:" after the opening braces to display the contents. E.g. {{en}} and {{msgnw:en}} show English and [[English]], 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:

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.

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 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 "in".

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 startinend, the end braces of template tc are taken as end braces of t.

New: A template name and a parameter value can both depend on a parameter or variable: {{tt|t|efg}} using Template:Tt, produces "startaefgbefgcefgdend", {{{{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.

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, than edit the page to blank it. )

Example:

{{variable text demo|full_demo}} gives the long version of a text:

  • With a speed of 30 metre/second a distance of 600 metres is covered in 20 seconds because 600 / 30 = 20.
  • With a speed of 10 metre/second a distance of 500 metres is covered in 50 seconds because 500 / 10 = 50.

while {{variable text demo|short_demo}} gives the short version:

  • With a speed of 30 m/s a distance of 600 m is covered in 20 s because 600 / 30 = 20.
  • With a speed of 10 m/s a distance of 500 m is covered in 50 s because 500 / 10 = 50.

This construction uses the following templates:

  • Template:Variable text demo - 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 demo1 and Template:Short demo1 are copies as workaround for the limitation that the same template can not occur more than five times in the same page (see below).

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

small version medium version large version


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 startend


  • 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 starthttp://meta.wikimedia.org/upload/2/27/Tst.pngend
  • the image is given a name of the form prefix{{PAGENAME}}postfix (with a postfix, 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 this page nor Template:T or 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:

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 a.

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:

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:

Examples of second restriction:

Navigational images

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:

With external link style:

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 http://meta.wikimedia.org/upload/2/27/Tst.png the more useful kind.
  • the target can be made variable like above.

Multiple inclusion of the same template in a page

Revision 1.210 of Parser.php is operational on test.wikipedia.org [1] and will be in version 1.4. It involves a change in the template inclusion code to detect loops, to limit the text length, and to drop the following restriction, which for 1.3 still applies (compare Template:Rec with [2]):

The same template can not be included more than five times in the same page (bug 95); e.g. {{t|a}} here gives startaend (this is Template:T when viewing the page, and startaend when previewing during section editing), because template t has already been used five times on this page. This makes this mechanism less suitable for special characters, e.g. wikibooks:Template:Flat and repeatedly occurring pieces of code, e.g. Template:sub. If necessary, make copies of the template. E.g, on this page Template:T1 is used, a copy Template:T.

The restriction, although this may not be the main purpose and not the best way, currently protects against infinite recursion, see e.g. en:Template:hc. The contents of a recursive template is rendered six times on the page of the template itself, and five times on a referring page: the limit of five includes indirect referrals.

There is no such limit for multiple occurrences of a parameter in a page, there can be more than five. This can make a rendered page very long if the parameter value is a long text. This effect can cumulate with the five- or six-fold rendering mentioned above, see Template:Z in combination with Template:Y.

EasyTimeline code with a template parameter is not allowed

Template talk:Etl

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 label. 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:Hi 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.mozilla.org/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}}

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.

An attempt to specify parameter values, as in {{subst:t2|R|s}} is accepted as subst-syntax, but the parameter values are ignored (bug 89). If the template has parameters, the result has them too.

The same template can not be substituted more than five times in one edit. However, in any subsequent edit five more are substituted. Thus one can press Edit and Save a few times just for this purpose, or forget about it and make other edits, which also rectifies the situation, even when doing section edits on other sections.

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.

See also

Examples

(On each template page, click "What links here" to see the list of pages that use the template.)

Help contents

Reading: Go | Search | URL | Namespace | Page name | Section | Link | Backlinks | Piped link | Interwiki link | Redirect | Variable | Category
Tracking changes: Recent | (enhanced) | Related | Watching pages | Page history | Diff | User contributions | Edit summary | Minor edit
Logging in and preferences: Logging in | Preferences | User style
Editing: Overview | New page | Images/files | Image description page | Special characters | Formula | Table | EasyTimeline | Template | Renaming (moving) a page | Automatic conversion of wikitext | Talk page | Testing
Edit this template