Content deleted Content added
Dinoguy1000 (talk | contribs) →Help pages: update links - most o the Meta pages were moved to MediaWikiWiki earlier this year by Pppery |
mNo edit summary |
||
(32 intermediate revisions by 19 users not shown) | |||
Line 1:
{{
{{Redirect-multi|2|H:T|Wikipedia:Template|help regarding tables|Help:Table|content guidelines|Wikipedia:Template namespace}}▼
{{For-multi|lists of commonly used templates|Wikipedia:Template index|a quick introduction to templates|Help:A quick guide to templates}}
▲{{Redirect|Wikipedia:Template|content guidelines|Wikipedia:Template namespace}}
{{Pp-semi-indef}}
{{Wikipedia how to|H:T|WP:TEMPLATE}}
{{beginner version|Help:A quick guide to templates}}
{{Wiki markup}}
A '''template''' is a Wikipedia page created to be included in other pages. Templates usually contain repetitive material that might need to show up on a larger number of articles or pages. They are commonly used for [[WP:Glossary#Boilerplate text|boilerplate]] messages, standardized warnings or notices, [[Help:Infobox|infoboxes]], [[WP:Navbox|navigational boxes]], and similar purposes. Templates can have parameters so that the same page generates different text on multiple pages that include it.
Line 11 ⟶ 10:
The most common method of inclusion is called [[Help:Transclusion|transclusion]], where the wiki source of the target page contains a reference to the template, using the {{tnull|{{var|Template name}}}} syntax. Another method is [[Help:Substitution|substitution]], where the content of the template is copied into the wiki source of the target page, just once, when it is saved.
[[Help:A quick guide to templates]] gives a brief introduction to the subject. There is further help from MediaWiki
==General description==
Line 26 ⟶ 25:
==Using templates==
===General===
Using a template is
Calling a template causes it to be either ''transcluded'' or ''substituted'' in the page that calls it (
[[Wikipedia:Transclusion|Transcluding]] a template means that when MediaWiki <em>displays</em> the page, it treats the template as if it were directly in the
and subsequent editors cannot tell that it originated from a template.
When you update a template, every page that transcludes it automatically reflects the update when the page is displayed in the future, whereas updating a template has no effect on pages that have previously been saved with the template substituted.
To
The [[number sign]] <code>#</code>
The template namespace is the default, so you can leave out the namespace <code>Template:</code> in the template name, and it is conventional to do so. However, you must specify the namespace prefix for templates in other namespaces, such as <code>User:</code>. To transclude a page in [[WP:MAINSPACE|mainspace]], precede its
▲Whereas MediaWiki variable names are all uppercase, template names have the same basic features and [[WP:NCHASHTAG|limitations]] as all [[Help:Page name|page names]]: they are [[Case sensitivity|case-sensitive]] (except for the first character); [[underscore]]s are parsed as spaces; and they cannot contain any of these characters: <code><nowiki># < > [ ] | { }</nowiki></code>. This is because those are reserved for [[wiki markup]].
{{A note}} Attempting to transclude a template that does not exist produces a [[WP:Red link|red link]], just like linking to any other nonexistent page. Following the link allows one to create that particular template. It is not possible to transclude
▲The [[number sign]] <code>#</code> denotes a [[URI fragment|fragment identifier]], which identifies a {{em|fragment}} or section of a document (such as a [[Help:Section|section]] in a Wikipedia article). Although you can use it in a [[WP:LINK|link]] to link to a section of a template page (like [[Template:Portal#Example]]), fragment identifiers have no meaning in a template call and are ignored. For example, {{tnull|Portal{{var|#Location}}|Books}} is the same as {{tnull|Portal|Books}}.
=== Parameters ===▼
▲The template namespace is the default, so you can leave out the namespace <code>Template:</code> in the template name, and it is conventional to do so. However, you must specify the namespace prefix for templates in other namespaces, such as <code>User:</code>. To transclude a page in [[WP:MAINSPACE|mainspace]], precede its title with a colon, as <code>{<nowiki />{:{{var|Page name}}}}</code>.
<!-- [[Help:Parameters]] redirects here, so: -->▼
▲{{A note}} Attempting to transclude a template that does not exist produces a [[WP:Red link|red link]], just like linking to any other nonexistent page. Following the link allows one to create that particular template. It is not possible to transclude pages between projects (such as different-language Wikipedias or MediaWiki)—to use a template on another language project, a copy of the template must be created in that project.
▲===Parameters===
▲<!--[[Help:Parameters]] redirects here, so: -->
{{about|template parameters|search parameters|Help:Searching#Parameters|section=yes}}
{{Further|Help:Transclusion#Template parameters}}
{{shortcut|H:PARAMETER|WP:PARAMETER}}
The basic transclusion syntax for a template can be further controlled using [[parameters]], which allow you to customize a template’s output. The syntax is <code><nowiki>{{Template name|</nowiki>''parameter''<nowiki>|</nowiki>''parameter''<nowiki>|...}}</nowiki></code>, where <code>{{var|Template name}}</code> is the template’s name, and each <code>{{var|parameter}}</code> can be either a simple value (known as an ''{{dfn|unnamed parameter}}'') or in the form <code>{{var|name}}={{var|value}}</code> (known as a ''{{dfn|named parameter}}''). The first, second, third, etc., unnamed parameters are named <code>1</code>, <code>2</code>, <code>3</code>, etc., so using <code><nowiki>{{Template name|1=value1|2=value2}}</nowiki></code> is equivalent to <code><nowiki>{{Template name|value1|value2}}</nowiki></code>.
Each template has specific parameters it can accept, as defined within its code. You can list named parameters in any order in a template call. Extra or misnamed parameters have no effect, while missing parameters cause a default value to be used. If a parameter is repeated, the last defined parameter value is used.
Whitespace characters (spaces, tabs, returns) are stripped from the beginnings and ends of {{em|named}} parameter names and values, but not from the middle: thus <code><nowiki>{{ ... | myparam = this is a test }}</nowiki></code> has the same effect as <code><nowiki>{{ ... |myparam=this is a test}}</nowiki></code>. This does not apply to {{em|unnamed}} parameters, where all whitespace characters are preserved.▼
The value of a parameter can be
If a template call specifies a parameter which is not defined in the template, it has no effect. Editors sometime specify a parameter they know is not defined in the template — for example, editors sometimes include a parameter like {{tag|reason|open}} to add a brief explanation within the source as a [[Wikipedia:Manual of Style/Hidden text|hidden comment]]. (But some templates, such as [[Template:Requested move|Requested move]], are programmed to show the reason parameter if provided). Certain templates, especially complex ones like [[Wikipedia:Infobox|infoboxes]], may use the [[Module:Check for unknown parameters|check for unknown parameters module]] to alert editors about any [[:Category:Unknown parameters|unrecognized parameters]] they code by mistake.
▲The value of a parameter can be the empty string, such as when the pipe or equals sign is followed immediately by the next pipe or the closing braces. This is different from not specifying the parameter at all, which results in a default value, although templates are often coded so as to behave the same in both cases.
==== Whitespace handling ====
▲Leading and trailing [[Wikipedia:Whitespace
The collapsing of line breaks around parameters can be used to [[Wikipedia:Template namespace#Readability of the code|improve the readability of a template call]] with many parameters by placing each parameter specification in its own line.
==== Variable length parameter lists ====
In MediaWiki, templates cannot automatically handle an unknown or unlimited number of parameters. Each possible parameter usually has to be predefined in the template code. For example, a template might be set up to use three specific parameters, such as <code>1</code>, <code>2</code>, and <code>3</code>. If someone includes additional parameters beyond those, they will not affect expansion of the template.
However, there are some ways to work around this:
*Setting a limit: You can write the template to handle a fixed number of parameters by manually specifying each one (e.g., up to 10 or 20 parameters).
*Using templates or [[Wikipedia:Lua|modules]]: For cases needing a flexible number of inputs, templates can use Lua modules or helper templates (like {{mfl|separated entries|main}} or {{tl|separated entries}}). These helper tools provide more advanced handling, such as counting or iterating through parameters, enabling the use of multiple inputs without setting an exact number.
However, modules can bypass this limitation. For simpler cases, the separated entries module expands all sequential parameters and lets you set custom delimiters. For more advanced usage, the {{mfl|params}} module enables counting, listing, mapping, filtering, and handling a variable number of parameters without prior knowledge of the exact number.
Templates that accepts an open number of parameters are often collected under [[:Category:Variadic templates]].
===Examples===
Line 148 ⟶ 157:
If a parameter is not specified in the template call, then the parameter reference is not replaced with anything -- it is expanded literally; this means that if the template call does not specify the parameter "xxx", the wikitext <code><nowiki>{{{xxx}}}</nowiki></code> inside the template expands to literally ''<nowiki>{{{xxx}}}</nowiki>'' (not the null string you may have expected). You can get a more useful behavior by specifying a default value in the parameter reference. Do this with the ''pipe syntax'': <code><nowiki>{{{xxx|dflt}}}</nowiki></code> specifies the default value <code>dflt</code> for the named parameter "xxx", and <code><nowiki>{{{1|dflt}}}</nowiki></code> specifies the default value <code>dflt</code> for the first unnamed parameter. Most often, one specifies a null default value, such as <code><nowiki>{{{1|}}}</nowiki></code> or <code><nowiki>{{{xxx|}}}</nowiki></code>.
If a call sets a parameter to the empty string like <code><nowiki>{{Template name|xxx=}}</nowiki></code> then <code><nowiki>{{{xxx|OK}}}</nowiki></code> will produce the empty string and not the default value "OK". Many users will expect that an empty parameter gives the same result as omitting the parameter. You can achieve this with: <code><nowiki>{{#if:{{{xxx|}}}|{{{xxx}}}|OK}}</nowiki></code>. This says: If <code>xxx</code> is assigned a non-empty value then use <code>xxx</code>, otherwise use "OK". If the default value is the empty string then you only have to write <code><nowiki>{{{xxx|}}}</nowiki></code>.
You can use default parameter values to effect a parameter alias:
Line 191 ⟶ 202:
===System variables and conditional logic===
Template code often makes use of the variables and parser functions described at [[Help:Magic words]] to make the template's behavior depend on the environment in which it is included (such as the current time or namespace). Parser functions can be used for some arithmetic calculations and string manipulations on variables and parameter values, but certain standard programming features such as loops and variable assignment are not available. Full string manipulation is not available; some templates providing such function have been created, but they are inefficient and imperfect.
Some of the most frequently used variables and functions are listed below. For more, see [[Help:Magic words]] and the fuller documentation at the MediaWiki pages [[mw:Help:Magic words]] and [[mw:Help:Extension:ParserFunctions]].
Line 222 ⟶ 233:
! width="30%" | Description !! width="40%" | Wiki source !! width="30%" | Displayed text
|-
| rowspan="2" | Testing for equality between two strings (or parameters). If the first two parameters are equal, the third parameter is returned, otherwise the fourth parameter is returned.
| <syntaxhighlight lang="wikitext" inline>{{#ifeq: yes | yes | Hooray...! | Darn...! }}</syntaxhighlight>
| {{#ifeq: yes | yes | Hooray...! | Darn...! }}
Line 229 ⟶ 240:
| {{#ifeq: yes | no | Hooray...! | Darn...! }}
|-
| Testing whether a string (or parameter) contains anything (other than whitespace). If it does, the second parameter is returned, otherwise the third parameter is returned.
| <syntaxhighlight lang="wikitext" inline>{{#if: {{{param|}}} | Hooray...! | Darn...! }}</syntaxhighlight>
| {{#if: {{{param|}}} | Hooray...! | Darn...! }}
Line 237 ⟶ 248:
| {{#expr: ( pi * 4 ^ 2 ) round 3 }}
|-
| [[Help:Calculation|Testing the result of a calculation]]. If the expression is true or non-zero, the second parameter is returned, otherwise the third parameter is returned.<br />[is 1230 even or odd?]
| <syntaxhighlight lang="wikitext" inline>{{#ifexpr: 1.23E+3 mod 2 | Odd | Even }}</syntaxhighlight>
| {{#ifexpr: 1.23E+3 mod 2 | Odd | Even }}
Line 269 ⟶ 280:
| {{CURRENTVERSION}}
|-
| Timestamp of last page revision
| <syntaxhighlight lang="wikitext" inline>{{REVISIONTIMESTAMP}}</syntaxhighlight>
| {{REVISIONTIMESTAMP}}
Line 282 ⟶ 293:
While fairly straightforward in application, it involves some noteworthy quirks and tricks.
To pass a parameter value from a template call
;Example{{colon}}
:Template:A contains <syntaxhighlight lang="wikitext" inline>"the quick brown {{B|{{{3}}} }} jumps over..."</syntaxhighlight>. Template:B (a subtemplate) contains <syntaxhighlight lang="moin" inline>'''{{{1}}}'''</syntaxhighlight>. Page X calls A with <syntaxhighlight lang="wikitext" inline>{{A|apple|pear|fox}}</syntaxhighlight> This expands to <syntaxhighlight lang="wikitext" inline>"the quick brown '''fox''' jumps over..."</syntaxhighlight>. The third unnamed parameter passed to Template:A gets passes as the first unnamed parameter to subtemplate B.
Line 295 ⟶ 306:
When a subtemplate contains unmatched braces—as in <syntaxhighlight lang="text" inline>{{lb}}}</syntaxhighlight>—the unmatched braces are treated as text during processing—they do not affect the parsing of braces in the calling template. But where the template is substituted, the unmatched braces will be parsed as braces when the page is subsequently displayed. This has little practical use, but can occasionally introduce unexpected errors.
See [[
===<span id="Noinclude, includeonly, and onlyinclude"></span>Inclusion control: noinclude, includeonly, and onlyinclude===
Line 309 ⟶ 320:
! What is included {{em|there}} (calling page)
|-
|
|
|
|-
|
|
|
|-
|
|
|
|-
|{{tag|onlyinclude|content={{tag|includeonly|content= {{bxt|text1}} }} }} {{mxtn|text2}}
|
|
|}
Line 354 ⟶ 365:
===Categorization===
{{See also|
====Categorize pages by template inclusion====
Some templates generate category declarations in their expansion, since the template is intended to place calling pages in particular categories. This is often done with maintenance categories. Placing articles into ordinary content categories in this way is discouraged. When doing this, you may have to use {{tag|includeonly}} tags to keep the template itself out of the category. While developing, testing, sandboxing, or demonstrating a template intended to apply a category, either temporarily replace each category with a test category (starting with [[:Category:X1|X1]], [[:Category:X2|X2]], or [[:Category:X3|X3]]) or suppress categorization (see [[WP:CATSUP|category suppression in templates]]).
Line 412 ⟶ 423:
The displayed page content generated by a template call (which is the rendering of the expansion of the called template) is the ''template result''. The template result generated by the template call <syntaxhighlight lang="wikitext" inline>{{sic|constellation prize}}</syntaxhighlight> is "{{sic|constellation prize}}".
The ''name'' of a template is the name of the
Some template calls perform a tagging function; such a template call is often called a ''tag'', as in, "If you are in the middle of a major edit, place an {{tl|in use}} tag at the top of the page." This is one of many ways the term "tag" is used in Wikipedia.
|