Content deleted Content added
Waldyrious (talk | contribs) →Whitespace handling: link to whitespace collapsing and to section in another page describing the use of vertical display of template parameters for readability |
mNo edit summary |
||
(22 intermediate revisions by 15 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 at [[mw:Help:Templates]] and [[mw:
==General description==
Line 30 ⟶ 29:
Calling a template causes it to be either ''transcluded'' or ''substituted'' in the page that calls it (the ''target page'').
[[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 page’s source. [[
and subsequent editors cannot tell that it originated from a template.
Line 57 ⟶ 56:
The value of a parameter can be an [[empty string]], such as when the pipe (<code>|</code>) or equals sign is followed immediately by the next pipe or the closing braces. For example, <code><nowiki>{{Template name|1=|2=test}}</nowiki></code> produces the same output as <code><nowiki>{{Template name||2=test}}</nowiki></code>. 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.
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
==== Whitespace handling ====
Line 66 ⟶ 62:
Leading and trailing [[Wikipedia:Whitespace|whitespace]] (including line breaks) around {{em|named}} parameters and values is [[Whitespace collapsing|collapsed]] automatically, but spaces in the middle of the value are kept. For instance, <code><nowiki>{{ ... | myparam = this is a test }}</nowiki></code> has the same output as <code><nowiki>{{ ... |myparam=this is a test}}</nowiki></code>. However, with {{em|unnamed}} parameters, all whitespace is retained as preserved in the output.
The
==== Variable length parameter lists ====
Line 75 ⟶ 71:
*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
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 161 ⟶ 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 295 ⟶ 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 308 ⟶ 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 322 ⟶ 320:
! What is included {{em|there}} (calling page)
|-
|
|
|
|-
|
|
|
|-
|
|
|
|-
|{{tag|onlyinclude|content={{tag|includeonly|content= {{bxt|text1}} }} }} {{mxtn|text2}}
|
|
|}
Line 367 ⟶ 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 425 ⟶ 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.
|