Help:Template

From InfoAnarchy
Jump to: navigation, search

Help index Other languages : Template:SxablonHelpoAliajLingvoj - Summary of this page

Note: The same template can not be included more than five times in the same page.

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 reference in the wikitext causes insertion of the template content when the referring page is rendered.

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.

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 Template:T (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 are ignored: {{t|a|b}} gives Template:T. In the case of fewer parameter values, the result depends on the remaining parameters: {{t2|a}} (using Template:t2) gives Template:T2, and {{t2||a}} gives Template:T2. This can be applied if the referring page is itself also a template.

Parameters are case-sensitive, even with respect to the first letter, and a space is considered different from an underscore, even though the template page does not show these differences.

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:

:''See [[Wikipedia:{{PAGENAME}}]] for the usage of "{{PAGENAME}}" in Wikipedia.''

shows

See Wikipedia:In Wikipedia for the usage of "In Wikipedia" in Wikipedia.
  • parameters are rendered as if the inner two pairs of brackets are references to other templates, so {{{Parameter name}}} shows up as {Template:Parameter name}, unless the parameter name is equal to a template name; in that case the contents of that template is shown; for clarity, a template can be created with the name of the parameter and as contents that name in double braces and nowiki-tags (as long as that template name is not needed for a more important purpose). This has been done on Meta for parameters 1 and 2, see Template:1 and Template:2. Conversely, parameters could be chosen which are not in use as template name (except, of course, if used for the purpose just mentioned).

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.

The template page rendering gives for each parameter also the default rendering, shown when the parameter value is not specified.

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:

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.

Not recommended

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, is not recommended. It is confusing and irritating for the reader, especially if the repeated text can not be recognized as such by a special lay-out. 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.

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

Also, an attempt to use a template with a name dependent on the value of a parameter does not work: {{tt|tc}} using Template:Tt, could be equivalent to {{tc}} and produce "Template:Tc", but it produces Template:Tt, the parameter is not substituted.

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.

What does work is using a parameter in a parameter value: {{ttp|def}}, using Template:Ttp, gives Template:Ttp.

Images in templates

In a template, if the image code is in the template itself, with the image name depending on a parameter, then the image is not embedded, but the link to the image description page works. Demo: {{timg|tst.png}} (using Template:timg) gives Template:Timg

However, the caption can be a parameter: {{timgc|test}} (using Template:timgc) gives Template:Timgc


For an image in a template that should be different for each page in which the template occurs, there are 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}}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

Examples:

In both cases, a link in a template with the target depending on a parameter and a link with the target depending on a template, even though the target ainsert in targetb exists, the link is to the edit page.

This problem does not apply if only the label depends on the parameter or template:

If, inside the double square brackets, a template is called that produces a text with a pipe character, it is not interpreted as such, and a link is produced to a page with a name that is not allowed, e.g. [[help:li{{tp}}]] (using Template:tp) gives [[help:liTemplate:Tp]].

To avoid an edit link to an existing page, one can use a seemingly superfluous prefix to get interwiki link style for a link within the same project. This works in the case of a link target depending on a template, but for one depending on a parameter only if the prefix is in the template, not if it is in the parameter; the latter case gives a link to an invalid page name:

It is not possible if the prefix for an interwiki link to a project is the same as the prefix for the project namespace, like in Wiktionary, Wikibooks and Wikisource. In that case use a double prefix like m:Wiktionary:, a U-turn, going from Wiktionary to Wiktionary through Meta.

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:

  • [{{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

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 [[help:link|Template:Timgurl]] 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 (Template:Bug); e.g. {{t|a}} here gives Template:T (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:Cp. 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.

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.

A category specified in a template

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:MediaWiki User's Guide.

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.

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 (Template:Bug). 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.

Templates on Meta itself

Template:conflict - Template:news - Template:activeWikipedias - Template:MetaHomePages - Template:AccueilBabel - Template:Status - Template:POV

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