Help:Template: Difference between revisions

Content deleted Content added
Parameters: c/e, stil mcuh oto be done, willl come back
Copy edit for clarity and grammar and correct some corruptions from recent edits
Line 26:
==Using templates==
===General===
Using a template is similar to calling a [[Function (computer programming)|function]] in programming. When called, it returns a value. For a template, calling is (known as a ''template call''), itand returnsthe areturn result,value is known as the ''expansion'' of the template, which becomesis treated as part of the calling page's source. Like functions, some templates can take parameters that modifyaffect their output.
 
WhenCalling a template is called,causes it canto be either ''transcluded'' or ''substituted'' in the page that calls it (the ''target page'').
 
[[Wikipedia:Transclusion|TransclusionTranscluding]] 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. This method[[Substitutionary ensuresatonement|Substituting]] a template differs in that updatesit toinserts the content of the template automaticallypermanently reflectinto inthe everytarget page wherewhen it’sit used.is <em>saved</em>
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 a updating a template has no effect on pages that have previously been saved with the template substituted.
[[Substitutionary atonement|Substitution]] differs in that it inserts the content of the template permanently into the target page when it is <em>saves</em>. This means subsequent editors cannot tell that it originated from a template.
 
To transclude a template, insert {{tnull|{{var|Template&nbsp;name}}}} at the point where it should appear in the page source. To substitute it, use {{tnull|subst:{{var|Template&nbsp;name}}}}. This wikitext is called a ''template call''.
 
In [[MediaWiki]], the [[wiki software]] that Wikipedia uses, [[Help:Magic words#Variables|variables]] are distinct from templates, butthough both are identifiedreferenced by double [[Curly brackets|curly braces]] <code>{{ }}</code>, butand they both return a value. These references differ: in that variables in MediaWiki are always uppercase, while template names follow the same conventions and have the same [[WP:NCHASHTAG|limitations]] as [[Help:Page names|page names]]. For example, includingthey are [[Case sensitivity|case-sensitivitysensitive]] (except for the first character);, [[underscore]]s are parsed as spaces;, and template names cannot contain certain characters: and limitedthe characters <code><nowiki># < > [ ] | { }</nowiki></code>. This is because thosethey are reserved for [[wiki markup]].
 
The [[number sign]] <code>#</code> is used as a [[URI fragment|fragment identifier]], which identifies a {{em|fragment}} or [[Help:Section|section]] of a page or Wikipedia article. Although you can use it in a [[WP:LINK|link]] to link to a section of a template page (e.g., [[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}}.
 
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 page name with a colon, as <code>{<nowiki />{:{{var|Page name}}}}</code>.
 
{{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 templates 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 ===
Line 52 ⟶ 53:
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 arehave ignoredno effect, while missing parameters will usecause a default value to be used. If a parameter is repeated, the last defined parameter value will beis used.
[[Parameter|Parameters]] allow customization of a template’s output. The syntax is <code><nowiki><{{Template name|parameter|parameter|...}}</nowiki></code>, where <nowiki><code>Template name</code></nowiki> is the template’s name. Each <nowiki><code>parameter</code></nowiki> can be a simple value (an ''unnamed parameter'') or a <nowiki><code>name=value</code></nowiki> pair (a ''named parameter''). Unnamed parameters are referred to by numbers like 1, 2, 3, etc., so <nowiki><code>{{Template name|1=value1|2=value2}}</nowiki><nowiki></code></nowiki> is equivalent to <nowiki><code>{{Template name|value1|value2}}</nowiki><nowiki></code></nowiki>.
 
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.
==== Whitespace handling ====
 
UnusedIf parametersa havetemplate call specifies a parameter which is not defined in the template, it has no impacteffect. SometimesEditors 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.]]. However,(But some templates, likesuch 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 check for [[Module:Check for unknown parameters|check for unknown parameters module]] to alert editors about any [[:Category:Unknown parameters|unrecognized parameters]] to help avoid unintentional errors.
{{Nutshell|1=Leading and trailing [[Wikipedia:Whitespace|whitespace]] around {{em|named}} parameters and values is automatically removed, but spaces within the middle of the value are kept. For example, <code><nowiki>{{ ... | myparam = this is a test }}</nowiki></code> gives the same output as <code><nowiki>{{ ... |myparam=this is a test}}</nowiki></code>.|2=However, with {{em|unnamed}} parameters, all whitespace are preserved in the output.}}
 
<!-- Leading and trailing [[Wikipedia:Whitespace|whitespace]] around {{em|named}} parameters and values is trimmed and removed automatically, but spaces within the maddle of the value itself 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 are retained as preserved in the output. -->
 
If a parameter is intentionallyspecified leftas blank (e.g., {{para|1}|open}}}), it is treated as an empty string, which differs from leaving it out altogether (in which case the default value applies). However, templates are often coded to act the same in both cases.
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 are ignored, while missing parameters will use a default value. If a parameter is repeated, the last defined parameter value will be used.
 
==== Whitespace handling ====
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.
 
{{Nutshell|1=Leading and trailing [[Wikipedia:Whitespace|whitespace]] around {{em|named}} parameters and values is automaticallytrimmed and removed automatically, but spaces withinin the middle of the value are kept. For exampleinstance, <code><nowiki>{{ ... | myparam = this is a test }}</nowiki></code> giveshas the same output as <code><nowiki>{{ ... |myparam=this is a test}}</nowiki></code>.|2= However, with {{em|unnamed}} parameters, all whitespace areis retained as preserved in the output.}}
Unused parameters have no impact. Sometimes, editors 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.]] However, some templates, like [[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 check for [[Module:Check for unknown parameters|unknown parameters module]] to alert editors about any [[:Category:Unknown parameters|unrecognized parameters]] to help avoid unintentional errors.
 
==== HandlingVariable variable-length variablesparameter lists ====
In MediaWiki, templates can’tcannot 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 wilwill not beaffect recognizedexpansion or processed byof the template.
 
However, there are some ways to work around this:
 
1. *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).
 
2. *Using templates or [[Wikipedia:Lua|modules]]: For cases needing a flexible number of inputs, templates can use Lua modules or helper templates (like <nowiki>{{separated entries}}</nowiki>). 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 params module enables counting, listing, mapping, filtering, and handling a variable number of parameters without prior knowledge of the exact number.
 
This workaround allows templatesit to ''<em>appear''</em> as if theya handletemplate handles a variable number of parameters bybecause the module processingprocesses each one iteratively up to a certain point, but true infinite flexibility is not built into the core MediaWiki system.
 
If a parameter is intentionally left blank (e.g., {{para|1}|open}}}), it is treated as an empty string, which differs from leaving it out altogether (in which case the default value applies). However, templates are often coded to act the same in both cases.
 
Sometimes, editors may add parameters that aren't. not recognized by the template. This can be useful, for instance, when using reason to add a brief explanation directly in the source. Certain templates use [[:Category:Error-handling modules|error-checking modules]] like check for unknown parameters to notify the editor of any unrecognized parameters, mainly in complex templates like infoboxes, where such errors are common.
 
 
 
If a template call specifies a parameter which is not defined in the template, it has no effect. Sometimes editors do this on purpose: For example, {{para|reason}} is frequently used as a pseudo-parameter to explain briefly in the wiki source why the template call was put there (a form of a [[Wikipedia:Manual of Style/Hidden text|hidden comment]]).{{efn|Some templates, such as {{tlx|Requested move}}, have code to display {{para|reason}} as visible output; whether to do so is determined on a template-by-template basis.}} Some templates use {{mlx|check for unknown parameters|check|...}} to warn the editor if a parameter is being used that is not accounted for in the template's code; this is mostly used for [[Help:Infobox|infoboxes]] and other templates with a large number of complicated parameters, where the presence of an unknown one is usually an unintentional error. If you update such a template to define a new parameter, its call to the [[Wikipedia:Module|module]] must also be updated to include the new parameter.
 
Wikitext syntax has limits and does not natively support templates with an unlimited [[variadic template|variadic parameters]]. However, this restriction can be circumvented using dedicated Lua modules. For simpler cases, separated entries can handle {{mlx|separated entries|main}} and add custom delimiters. For more advanced use, {{mlx|[params}} can dynamically count, list, or map all provided parameters without needing to know their count in advance.
 
===Examples===