Jump to content

Help:Template: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
... and that's all, folks
Tag: Replaced
 
Line 1: Line 1:
{{MovedToMediaWiki}}
{{hi}}<small> '''Other languages''' : {{SxablonHelpoAliajLingvoj}} </small> - ''[[Help:A quick guide to templates|Summary of this page]]''

'''''Note: The same template can not be included more than five times in the same page.'''''
__TOC__
The '''template namespace''' is the [[MediaWiki User's Guide: Namespaces|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 [[variable]]s) is designed to be useful for insertion in other pages ([[en:transclusion|transclusion]], creating a [[en:compound document|compound document]]).

The syntax for insertion of the page "Template:''name''<nowiki>" is {{</nowiki>''name''<nowiki>}}</nowiki>.

This reference in the wikitext causes insertion of the template content when the referring page is rendered.

If the page "Template:''name''<nowiki>" does not exist, then {{</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.

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 [[MediaWiki User's Guide: Image description page|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 [[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.

==Parameters==

''(supported in '''versions 1.3 and higher''')''

Template parameters are either named or numbered. The syntax is <tt><nowiki>{{templatename|parname1=parvalue1|parname2=parvalue2}}</nowiki></tt> with <tt><nowiki>{{{parname}}}</nowiki></tt> tags in the template, or <tt><nowiki>{{templatename|parvalue1|parvalue2}}</nowiki></tt> with <tt><nowiki>{{{1}}}</nowiki></tt>, <tt><nowiki>{{{2}}}</nowiki></tt>, 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: <nowiki>{{t|[[a|b]]}}</nowiki> (using [[Template:t]]) gives {{t|[[a|b]]}} (compare <nowiki>start[[a|b]]end</nowiki> giving start[[a|b]]end: with the template the text after the link is not made part of the link label).

On rendering, surplus parameter values are ignored: <nowiki>{{t|a|b}}</nowiki> gives {{t|a|b}}. In the case of fewer parameter values, the result depends on the remaining parameters: <nowiki>{{t2|a}}</nowiki> (using [[Template:t2]]) gives {{t2|a}}, and <nowiki>{{t2||a}}</nowiki> gives {{t2||a}}. 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:

*not the variables themselves, but their values are shown; e.g. the page [[:en:Template:In Wikipedia]] with the content

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

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 <nowiki>{{{Parameter name}}}</nowiki> 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. <nowiki>{{en}} and {{msgnw:en}}</nowiki> show {{en}} and {{msgnw: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:
*[[en:Wikipedia:Template messages|template messages]] such as [[en:Template:stub]]
*[[en:Wikipedia:Navigational templates|navigational templates]] such as [[en:Template:europe]]
*[[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 [[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 [[en:Wikipedia:Template_messages#Taxobox_components_.28Taxons.29|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.

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

===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: <nowiki>{{{{tctc}}}}</nowiki>, using [[Template:Tctc]], gives the text {{{{tctc}}}}, not the content of <nowiki>{{tc}}</nowiki>, which is "{{tc}}".

Also, an attempt to use a template with a name dependent on the value of a parameter does not work: <nowiki>{{tt|tc}}</nowiki> using [[Template:Tt]], could be equivalent to <nowiki>{{tc}}</nowiki> and produce "{{tc}}", but it produces {{tt|tc}}, 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: <nowiki>{{t|{{tc}}}}</nowiki>, using [[Template:T]], gives {{t|{{tc}}}}, the end braces of template tc are taken as end braces of t.

What does work is using a parameter in a parameter value: <nowiki>{{ttp|def}}</nowiki>, using [[Template:Ttp]], gives {{ttp|def}}.

===Links and images===

In a template, the name of an embedded image can not depend on a parameter. If one tries this, then the image is not embedded, but the link to the image description page works. Demo: <nowiki>{{timg|tst.png}}</nowiki> (using [[Template:timg]]) gives {{timg|tst.png}}

However, the following is possible:

*the caption can be a parameter: <nowiki>{{timgc|test}}</nowiki> (using [[Template:timgc]]) gives {{timgc|test}}

<br style="clear:all;" />

*the whole image code can be a parameter: <nowiki>{{t|[[Image:Tst.png|thumb|right|50px|bullet]]}}</nowiki> gives {{t|[[Image:Tst.png|thumb|right|50px|bullet]]}}

<br style="clear:all;" />

*in projects where external images can be embedded, the url of the image can be a parameter: <nowiki>{{t1|http://meta.wikimedia.org/upload/2/27/Tst.png}}</nowiki> gives {{t1|http://meta.wikimedia.org/upload/2/27/Tst.png}}

Furthermore, the name of an image can depend on a variable, see below.

===Internal links===

Examples:
*<nowiki>{{tli|insert in target|insert in label}}</nowiki> (using [[Template:tli]]) gives {{tli|insert in target|insert in label}}
*<nowiki>[[a{{tciit}}b|c{{tciil}}d]]</nowiki> (using [[Template:tciit]] and [[Template:tciil]]) gives [[a{{tciit}}b|c{{tciil}}d]]

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:

*<nowiki>{{tlil|insert in label}}</nowiki> (using [[Template:tlil]]) gives {{tlil|insert in label}}
*<nowiki>[[help:link|c{{tciil}}d]]</nowiki> gives [[help:link|c{{tciil}}d]]

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. <nowiki>[[help:li{{tp}}]]</nowiki> (using [[Template:tp]]) gives [[help:li{{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:

*<nowiki>{{tlm|insert in target|insert in label}}</nowiki> (using [[Template:tlm]]) gives {{tlm|insert in target|insert in label}}
*<nowiki>{{tl|m:help:link}}</nowiki> (using [[Template:tl]]) gives {{tl|m:help:link}} (does not give the correct link)
*<nowiki>[[m:a{{tciit}}b|c{{tciil}}d]]</nowiki> gives [[m:a{{tciit}}b|c{{tciil}}d]]

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:
*<nowiki>{{tle|insert in target|insert in label}}</nowiki> (using [[Template:tle]]) gives {{tle|insert in target|insert in label}}
*<nowiki>{{tle2|insert in target}}</nowiki> (using [[Template: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> (using [[Template: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> (using [[Template:tae]]) gives {{tae|http://www.}}

Examples of second restriction:
*<nowiki>[{{th}}add at the end]</nowiki> (using [[Template:th]]) gives [{{th}}add at the end]
*<nowiki>{{th}}add at the end</nowiki> gives {{th}}add at the end

===Multiple inclusion of the same template in a page===

'''The same template can not be included more than five times in the same page'''; e.g. <nowiki>{{t|a}}</nowiki> here gives {{t|a}} (this is [[Template:T]] when viewing the page, and startaend when [[Help:Section#Preview|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 <nowiki>{{pl|target page|label}}</nowiki>, giving {{pl|target page|label}}. It is equivalent with <nowiki>[[target page|label]]</nowiki>, giving [[target page|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 <nowiki>[http://target_web_page label]</nowiki>, giving [http://target_web_page 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]].

==Images in templates==

For an image in a template that should be different for each page in which the template occurs, there are two possibilities:

*the image is given a name of the form ''prefix''<nowiki>{{PAGENAME}}</nowiki>''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.

*the whole image syntax is a parameter value

If the image syntax is in the template itself, with the ''image name'' depending on a parameter, the image is not shown in the referring page, but there is the usual link to the proper image description page.

==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 <nowiki>{{msgnw:{{PAGENAME}}}}</nowiki>, 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 [[Help:Automatic conversion of wikitext|wikitext to be replaced]] by the wikitext of the template when the referring page is saved.

Example: <nowiki>{{subst:rcnote}}</nowiki>

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 <nowiki>{{subst:t2|R|s}}</nowiki> is accepted as subst-syntax, but the parameter values are ignored. 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:

<nowiki>{{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}, &nbsp;&nbsp;&nbsp;{{subst:CURRENTTIME}} (UTC)</nowiki>

giving:
25 July 2004, &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).

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

==MediaWiki version 1.2.6==

In MediaWiki version 1.2.6, make a page MediaWiki:mytemplate and refer to it with <nowiki>{{msg:mytemplate}}</nowiki>.

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

*[[en: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)
*[[MediaWiki_roadmap#Template_syntax]] - about the changes that occurred earlier
*[[Message substitution]] - Earlier technical discussions
*http://test.wikipedia.org/wiki/Parser_and_template_issues
*http://test.wikipedia.org/wiki/Template:Images

==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 <nowiki>{{PAGENAME}}</nowiki>:
**[[en:Template:Infobox Countries]]
***<nowiki>{{PAGENAME}}</nowiki>_flag_large.png
***<nowiki>{{PAGENAME}}</nowiki>_coat_of_arms_large.png
***Location<nowiki>{{PAGENAME}}</nowiki>.png
**[[fr:Modèle:Tableau pays]]
***<nowiki>{{PAGENAME}}</nowiki>_flag_medium.png
***<nowiki>{{PAGENAME}}</nowiki>_armoirie.png
***Location<nowiki>{{PAGENAME}}</nowiki>.png
**[[cy:Template:Dosbarthiad_biolegol]]
*[[eo:Ŝablono:El]] - small, often used image
*[[fr:Modèle:Familia]] - translation
*http://wikibooks.org/wiki/Template:GeneralChemTOC
*http://wikibooks.org/wiki/Template:New
*[http://wikiquote.org/w/wiki.phtml?title=Template:Qotd Quote of the day] - daily changed content

{{hc}}

[[es:Help:Plantilla]]

Latest revision as of 17:43, 30 May 2024