Changes
m
→Creating and editing templates: typo
{{for|lists of commonly used templates|Wikipedia:Template messages}}
{{Pp-semi-indef}}
{{Wikipedia how to|H:T|WP:TEMPLATE}}
{{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 any number of articles or pages. They are commonly used for [[Wikipedia:Glossary#Boilerplate text|boilerplate]] messages, standard warnings or notices, [[Help:Infobox|infoboxes]], [[WP:Navbox|navigational boxes]], and similar purposes.
The most common method of inclusion is called [[Wikipedia:Transclusion|transclusion]], where the [[WP:wikitext|wikitext]] 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 wikitext 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 and Wikimedia at [[mw:Help:Templates]], [[m:Help:Template]], and [[m:Help:Advanced templates]].
==General description==
[[File:Introduction to templates on Wikipedia.webm|thumb|A basic overview of how templates work (8-minute video)]]
Most templates are pages in the [[WP:Template namespace|Template namespace]], which means that they have titles in the form "Template:{{var|XXXX}}". It is possible, however, to transclude and substitute from any namespace,{{efn|Namespaces from which transclusion is not allowed are specified on a wiki by the variable <code>[[mw:Manual:$wgNonincludableNamespaces|$wgNonincludableNamespaces]]</code>.}} and so some template pages are placed in other namespaces, such as the [[WP:User namespace|User namespace]]. Template pages have associated [[WP:talk page|talk pages]].
Templates can contain any desired [[WP:Wikitext|wikitext]], including calls to other templates. They have some limited programming capacities: customizable values (called [[#Parameters|parameters]]); calculation and branchings (using [[Help:Parser functions|parser functions]]); and access to wiki-specific variables ([[Help:Magic words|magic words]]), such as dates, times, and page names. They may also contain tags which define which parts of the wikitext are to be included when the template is transcluded or substituted. This means that the appearance of the template page itself need not be the same as that of the transcluded content (for example, it can contain documentation, categories, etc. for the template).
{{strong|How to do it:}} To transclude a template into an article or page, type {{tnull|{{var|Template name}}}} in the wikitext at the place where the template is to appear. The first letter may be indifferently lower- or upper-case.
The prefix <code>Template:</code> before the template name is the default one and need not be included. However, for templates stored in other namespaces, the prefix, such as <code>User:</code>, must be specified. To transclude a page in [[WP:mainspace#Namespace|mainspace]], precede its title with a colon, as <code>{<nowiki />{:{{var|Page name}}}}</code>.
{{A note}} Attempting to transclude a template that does not exist produces a [[WP:Redlink|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.
==Usage syntax==
===Parameters===
{{about|template parameters|search parameters|Help:Searching#Parameters|section=yes}}
{{further|m:Help:Templates#Parameters}}
{{shortcut|H:PARAMETER|WP:PARAMETER}}
<!--[[Help:Parameters]] redirects here.-->
The basic transclusion syntax given above can be extended by the addition of [[parameter]]s, which are used to control the template's output. The syntax for this is
{{tnull|{{var|Template name}}|{{var|parameter}}|{{var|parameter}}|{{var|...}}}}
where <code>{{var|Template name}}</code> is the name of the template, and each <code>{{var|parameter}}</code> may either contain just a value (these are called ''{{dfn|unnamed parameters}}'') or be of the form <code>{{var|name}}={{var|value}}</code> (''{{dfn|named parameters}}''). The first, second, third, etc. unnamed parameters will be given the names <code>1</code>, <code>2</code>, <code>3</code>, etc.
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.
Which parameters (if any) can or should be passed to a template and how they are to be named depends on the coding of that template. Named parameters can be defined in any order. Superfluous or misnamed parameters will be ignored; undefined parameters will be assigned default values. If a parameter is defined more than once, the last value takes effect.
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 omitting the parameter altogether, which leaves it undefined, although templates are often coded so as to behave the same in both cases.
Parameters can be specified (and will do nothing) even if not represented in the template's code. For example, {{para|reason}} is frequently used as a pseudo-parameter to explain briefly in the wikisource why the template was placed.{{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 call [[Module:Check for unknown parameters]] 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 [[WP: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 include a new parameter, its call to the module must also be updated to include the new parameter.
===Calling===
{{Further|mw:Help:Magic words}}
Calling a template is not unlike calling a [[Variable (computer science)|variable]] or a [[Function (computer science)|function]] in a programming language: call it with a reference and it returns a value (the [[Input/output|output]]). Like functions, some templates accept parameters that change the output.
In [[MediaWiki]], the [[wiki software]] that Wikipedia uses, [[Help:Magic words#Variables|variables]] have a more specific meaning that distinguishes them from templates, but they are both identified by double [[Curly brackets|braces]] <code><nowiki>{{ }}</nowiki></code> and they both return a value.
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-sensitive (except for the first character); underscores 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]] and [[HTML]].
The [[number sign]] <code>#</code> is called a [[fragment identifier]] because it denotes a {{em|fragment}} or section of a document (such as a [[Help:Section|section]] in a Wikipedia article). Although it [[WP:ANCHOR|can be used]] to link to a section of a template page (like [[Template:Portal#Example]]), there is no reason to put a fragment identifier or fragment name in a template reference. In {{tnull|Portal{{var|#Location}}|Books}}, for example, the string <code>{{var|#Location}}</code> has no purpose and is thus ignored.
===Substitution===
{{Further|Help:Substitution}}
When a template is substituted, its content is [[hard-coded]] in the page rather than [[WP:Transclusion|transcluded]]. To learn how and [[Help:Substitution#When to use substitution|when]] to substitute a template, see [[Help:Substitution]].
==Basic template usage examples==
{{A note}} If you wish to experiment with any of these, please use either the [[Template:Template sandbox|template sandbox]], or your user page or sandbox.
An example of a very simple template can be found at [[Template:Lambda]], which expands out to place the lambda symbol (λ) at that point in the text. A programmer would say that the template {{em|returns}} the lambda symbol (λ).
Click on [[Template:Lambda]], then click on the "Edit source" tab to see the template code (its wikitext). The "active" part of that code, called the {{dfn|expansion}} of the template, is the single word <code>&lambda;</code>.{{efn|<code>&lambda;</code> is the [[List of XML and HTML character entity references|HTML character entity]] for the Greek letter (λ).}} The remainder of the wikitext is enclosed between {{xtag|noinclude}} tags so it is displayed on the template page itself but will not be displayed when the template is used (or called) on another page.
To transclude Template:Lambda onto another page (i.e., to use it on another page), type {{tlx|lambda}} (or {{tlx|Lambda}}—the case of the first letter is not significant) into the wikitext of the target page and press {{Button|Show preview}}. The page will be displayed with the {{dfn|template call}} replaced by the expansion of the template, as if the wikitext actually contained <code>&lambda;</code> at that point. The displayed page will therefore contain the text "λ".
For example, type <code>The 11th letter of the Greek alphabet is the lambda ({{tl|lambda}})</code> and you will see "The 11th letter of the Greek alphabet is the lambda ({{lambda}})" when previewing the page or after saving the change. You can use templates without knowing the details of their code; you only need to remember what result they produce, which is usually described on the template page.
Another way to use this template is to [[Help:Substitution|substitute]] it. If you type <code>The 11th letter of the Greek alphabet is the lambda ({{tls|lambda}})</code> and preview or save the page, you will again see "The 11th letter of the Greek alphabet is the lambda (λ)". If you look again at the saved wikitext,{{efn|Since the new wikitext itself needs to be reviewed, and new wikitext itself cannot be previewed during source editing, previewing the page will not illustrate the result of the template substitution {{em|in the wikitext}}.}} however, you will see that the template calls really were replaced by the expansion of the template when you saved the page. The link between the output text and the template is now broken, and the output will not be affected by future changes to the template (as it would be in the case of transclusion).
==={{anchor|Parameters}} Examples with parameters===
An example of a template that takes parameters is the template {{tlx|about}}. Try typing {{tnull|about|how to use templates|how to use modules|Help:Module}} in the sandbox—it will produce the following text:
{{about|how to use templates|how to use modules|Help:Module}}
The template {{tlx|about}} uses three unnamed parameters in the example above, but the same template can also be used with different numbers of parameters to give slightly different results, as explained in [[Template:About/doc|the template's documentation]]. For example, {{tnull|about||how to use modules|Help:Module}}. Note the usage of an empty parameter—in this instance, the consecutive pipes mean that the first parameter that was "passed" to the template is an empty string, which in this template will cause it to omit the initial "about" sentence. This produces: {{about||how to use modules|Help:Module}}
Other templates, particularly more complex ones, take named parameters or a mixture of named and unnamed ones. A simple example is [[Template:Payoff matrix]], used to generate a 2-by-2 grid. For example:
{{Markup|<nowiki>{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}</nowiki>|{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}}}
See the [[Template:Payoff matrix|template page]] for more possibilities. Notice that the template is used here without defining all its possible parameters—undefined parameters are given default values.
The spaces around the equal signs and before and after the parameters are used only for clarity—they are not needed and are ignored when the template is evaluated (although this is not the case with unnamed parameters). Parameter names {{em|are}} fully case-sensitive, though; for example, it is not possible to replace <code>DR</code> with <code>dr</code> or <code>dR</code> in the above example. Parameters with names that are not used by the template are simply ignored.
Examining the source code of the template shows the standard table mark up with some extra triple bracket entities representing the parameters:
<syntaxhighlight lang="moin">
{| id="Payoff matrix" style="background:white; float: {{{Float|right}}}; clear:right; text-align:center;" align={{{Float|right}}} cellspacing=0 cellpadding=8 width={{{Width|225}}}
|-
|style="width:33%; "|
|style="width:33%; border-bottom: solid black 1px;"| {{{2L|Left}}}
|style="width:33%; border-bottom: solid black 1px;"| {{{2R|Right}}}
|-
|style="border-right: solid black 1px; text-align: right; "| {{{1U|Up}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{ULc|white}}}; font-size:120%; "| {{{UL|0, 0}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{URc|white}}}; font-size:120%; "| {{{UR|0, 0}}}
|-
|style="border-right: solid black 1px; text-align: right; "| {{{1D|Down}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{DLc|white}}}; font-size:120%; "| {{{DL|0, 0}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{DRc|white}}}; font-size:120%; "| {{{DR|0, 0}}}
|-
|style="font-size: 90%;" colspan=3 |''{{{Name|{{PAGENAME}}}}}''
|}</syntaxhighlight>
The entity <syntaxhighlight lang="moin" inline>{{{2L|Left}}}</syntaxhighlight> instructs the template to use the named parameter <code>2L</code> or the text <code>Left</code> if the parameter is not present in the call.
==Usage hints and workarounds==
The following points may be worth noting when using templates:
* An unnamed parameter cannot contain an ordinary equals sign, as this would be interpreted as setting off a named parameter.{{efn|This does not apply if the equals sign comes within another template call or other item which the parser handles separately.}} To pass an equals sign in an unnamed parameter (for example in a [[URL]] with [[attribute–value pair]]s), replace the equals sign with the special template {{tlx|{{=}}}}, which returns an equals sign that will not be interpreted. Another method is to replace the unnamed parameter (and any subsequent unnamed parameters) with named parameters—the first unnamed parameter is equivalent to a named parameter {{para|1}} and so on. To call template {{tlx|done}} with <code>a=b</code> as the literal value for the first parameter, type either <code><nowiki>{{done|a{{=}}b}}</nowiki></code> or <code><nowiki>{{done|1=a=b}}</nowiki></code>.
* Similarly, it is not possible to use an ordinary pipe character <code>|</code> in template parameters, as it will be interpreted as a separator.{{efn|Again, this does not apply if it comes within another separately parsed item, such as a piped wikilink.}} This time, the problem can be solved by using the [[mw:Help:Magic words#Other|magic word]] {{tlx|!}} in place of the pipe, or—if the pipe is not intended to be parsed at a higher level—using the [[List of XML and HTML character entity references|HTML entity]] <code>&#124;</code>.
* Remember that whitespace characters (spaces, tabs, carriage returns, and line feeds) are not automatically stripped from the start and end of unnamed parameters, unlike with named parameters. Including such characters (or any other non-visible characters in any parameters) may in some cases affect the template's behaviour in unexpected ways. (Template designers can use {{tlx|Strip whitespace}} to remove unwanted whitespace in unnamed parameters.)
* In documentation and discussions, it is often convenient to be able to produce the template call syntax with a link to the template in question, but without actually calling the template. This can be done easily using the {{tlx|tl}} template (the "'''t'''emplate '''l'''ink" template). For example, {{tnull|tl|Example}} produces {{tl|Example}}. There are multiple other [[Template:Template-linking templates|template-linking templates]] available with different functionalities.
* When a template is changed (when the template or one of its subtemplates is edited), the change will be reflected on all pages on which the template is transcluded. However, the change may not become visible on all pages immediately; a previously cached version of a page, based on the previous version of the template, may continue to be displayed for some time. Use the [[Help:Purge|purge]] function to force a page to be displayed using the latest versions of templates—including on the template page itself, if it contains usage examples.
* When viewing old versions of pages, remember that templates will be transcluded as they are now, not necessarily as they were when the old page version was active.
* To list all pages onto which a template is transcluded, use [[Help:What links here|What links here]] on the template page. This will {{em|not}} include pages where the template has been substituted.
* To get a list of templates transcluded on a page, click "Edit" and find the list below the edit window. This list also includes the subtemplates used by the templates that are directly transcluded. To get such a list for a page section, an old version of the page,{{efn|For an old version, the subtemplate tree will be constructed according to the templates' current state.}} or your newly edited version prior to saving, click {{Button|Show preview}} on the appropriate edit page.
* There are limits to the number and complexity of the templates that an article may have. See the "[[#Expand limits|§ Expand limits]]" section for help in resolving this.
* If you want the template to leave a [[WP:~|time stamp or signature]], you can write <code><noinclude><nowiki></noinclude><nowiki>~~~~~</nowiki><noinclude></nowiki></noinclude></code>, but this will only work if you substitute the template. If you transclude it, you'll just get <nowiki>~~~~~</nowiki>.
* To improve readability, usually programmers like to split the code with newlines and indent it. Unfortunately, [[MediaWiki]] software does not allow this functionality; in many instances, these purpose-built newlines are treated by the software as content. One possible workaround is to add <code><!--</code> before each newline character and <code>--></code> after it, which produces an [[HTML comment]].
==Creating and editing templates==
<!--[[Help:Subtemplates]] redirects here.-->
Templates are created and edited in much the same way as any other page: choose an appropriate name, navigate to that page, then click the "Edit" tab or create a new page as needed. As mentioned above, templates are normally placed in the [[WP:Template namespace|Template namespace]], though templates intended for your own personal use or for experimentation can be created in your own [[WP:user space|user space]]. Anything that can be included on a normal page or article can be included on a template, including other templates (called ''{{dfn|subtemplates}}''). Templates often make use of programming features—parameters, parser functions, and other [[Help:Magic words|magic words]]—which allow the transcluded content to vary depending on context. There are also special tags to control which information is transcluded and which is not.
Before creating a template, do a quick search for existing templates (such as by exploring [[:Category:Wikipedia templates]]) to see if there is already a template that does what you want or a similar template whose code can be copied and modified (or left in place and expanded). Look for generic templates on which the new template can be based; for example, [[WP:Navbox|navbox]] templates can be easily created by calling the generic [[Template:Navbox]].
There is no hard rule about what name to choose for a template—make it short but reasonably descriptive. If similar templates exist, try to follow a consistent naming pattern. Templates can be renamed without breaking existing transclusions, provided a [[WP:Redirect|redirect]] to the new template name is left behind.
Be extremely careful when editing existing templates—changes made can affect a large number of pages, often in ways you might not expect. For this reason many high-use templates are [[WP:Protection|protected]] against editing except by [[WP:Administrator|administrators]] and [[WP:Template editor|template editors]]; other editors can propose changes on the talk page. Some templates offer a sandbox and [[Wikipedia:Template test cases|test cases]] for experimentation.
To propose the deletion or merger of unused or inappropriate templates or templates that can easily be merged, go to [[WP:Templates for discussion|Templates for discussion]] (TfD).
===Handling parameters===
The values of the parameters which can be fed to a template are represented in the template code by items enclosed between {{em|triple}} braces:
* The code <code><nowiki>{{{xxx}}}</nowiki></code> will be replaced by the value of the parameter named <code>xxx</code>. these are known as ''named parameters''.
* The codes <code><nowiki>{{{1}}}</nowiki></code>, <code><nowiki>{{{2}}}</nowiki></code>, and so on will be replaced by the first, second, and so on ''unnamed parameter'' (or the value of a parameter named <code>1</code>, <code>2</code>, etc.); these are sometimes known as ''{{dfn|positional parameter}}s''.
If a parameter is not assigned a value, then no replacement will take place; this means that if no value is passed for parameter {{para|xxx}}, the value of the expression <code><nowiki>{{{xxx}}}</nowiki></code> inside the template will literally be ''<nowiki>{{{xxx}}}</nowiki>'', not the 'blank' you may have expected. A more intuitive behaviour can be achieved by specifying default parameter values. This is done with the ''pipe syntax'': <code><nowiki>{{{xxx|dflt}}}</nowiki></code> specifies the default value <code>dflt</code> for the named parameter {{para|xxx}}, and <code><nowiki>{{{1|dflt}}}</nowiki></code> specifies the default value <code>dflt</code> for the first unnamed parameter. Most often, this is used to specify null default values, such as <code><nowiki>{{{1|}}}</nowiki></code> or <code><nowiki>{{{xxx|}}}</nowiki></code>.
Note the default text must be real text: that you cannot assign another parameter as the default. It will simply return the name of the parameter as text. <!--For example, suppose the template 'Template:Hello' is <code>{{{1|param}}}</code>, and is called with <code><nowiki>{{hello|param=world}}</nowiki></code>. Parameter '1' does not exist, so it will call the default. The text returned is the "param" or what would be expected to be the parameter name, not the parameter's assignment "world". This would be best implemented by a sequence of if [[mw:Help:Extension:ParserFunctions|parser statements]].-->
<!-- Also note that if a template is called with the parameter specified as blank (e.g. <code>{{Example|}}</code>), the default for the parameter will not appear. If that is undesired one can check if the parameter exists through the parser code<code><nowiki>{{#if:{{{1|}}}|{{{1}}}|default}}</nowiki></code> instead. This will produce the text "default" even if the parameter is specified as empty.-->
Because of the multiple uses of double-brace and triple-brace syntax, expressions can sometimes be ambiguous. It may be helpful or necessary to include spaces to resolve such ambiguity. For example, <code><nowiki>{{ {{{xxx}}} }}</nowiki></code> or <code><nowiki>{{{ {{xxx}} }}}</nowiki></code>, rather than typing five consecutive braces, may be more human-readable. However, watch out for unwanted whitespace appearing in template expansions.
====Special case: parameters within an XML-style opening tag====
Parameters do not get expanded when they are wrapped in {{xtag|nowiki}} tags. They aren't expanded either if placed within the actual [[XML]]-style opening tag. Thus, the following will not work within a template:
*{{!mxt|<nowiki><ref name={{{param}}}> Smith, Adam (1776)...</ref></nowiki>}}
because the parameter is not expanded. Instead, you can use the <code><nowiki>{{#tag:}}</nowiki></code> [[WP:PF|parser function]], which is—for example—used in {{tlx|sfn}} to generate the {{tag|ref}} element; see also [[Help:Magic words#Formatting|Help:Magic words § Formatting]]. Therefore, the following example will work:
*{{mxt|<nowiki>{{#tag:ref | Marx, Karl (1848)... | name={{{param}}} }}</nowiki>}}
====Caution: overextending URLs====
If a parameter's value is (or ends with) a [[URL]], check whether it is displayed in Wikipedia with the link overextending by one or more characters after the URL so that clicking the link causes an error or failure. Ensure that, after processing by the software, a soft space ({{em|not}} a [[Non-breaking space|hard or non-breaking space]]) follows the URL, regardless of whether you or a user supplied the URL or whether it was generated by automated processing. Possibly, the source code could contain or generate a space that is discarded in the processing or there might not be any space there. Correct the source code, perhaps by forcing a soft space to appear after the URL. The {{tlx|spaces}} template may be useful.
===System variables and conditional logic===
Template code often makes use of the variables and parser functions described at [[Help:Magic words]] in order to make the template's behaviour depend on the environment (such as the current time or namespace) or on the parameter values that are passed to it. They can also be used for arithmetical calculations, but certain standard programming features such as loops and variable assignment are not available. Full string manipulation is also not available; some templates providing such functionality 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]].
<table><tr><td>
{| class="wikitable" style="text-align:center;" width="100%"
|+Examples of core parser functions
! width="30%" | Description !! width="40%" | Text entered !! width="30%" | Result
|-
| Uppercasing text
| <nowiki>{{uc: Heavens to BETSY! }}</nowiki>
| {{uc: Heavens to BETSY! }}
|-
| Lowercasing text
| <nowiki>{{lc: Heavens to BETSY! }}</nowiki>
| {{lc: Heavens to BETSY! }}
|-
| Getting a namespace name
| <nowiki>{{NS: 1 }}</nowiki>
| {{NS: 1 }}
|-
| Getting a Wikipedia URL
| <nowiki>{{fullurl: pagename }}</nowiki>
| {{fullurl: pagename }}
|}
The ParserFunctions extension provides more programming-oriented parser functions.
{| class="wikitable" style="text-align:center;" width="100%"
|+Examples of extension parser functions
! width="30%" | Description !! width="40%" | Text entered !! width="30%" | Result
|-
| rowspan="2" | Testing for equality between two strings (or parameters)
| <nowiki>{{#ifeq: yes | yes | Hooray...! | Darn...! }}</nowiki>
| {{#ifeq: yes | yes | Hooray...! | Darn...! }}
|-
| <nowiki>{{#ifeq: yes | no | Hooray...! | Darn...! }}</nowiki>
| {{#ifeq: yes | no | Hooray...! | Darn...! }}
|-
| Testing whether a string (or parameter) contains anything (other than whitespace)
| <nowiki>{{#if: {{{param|}}} | Hooray...! | Darn...! }}</nowiki>
| {{#if: {{{param|}}} | Hooray...! | Darn...! }}
|-
| [[Help:Calculation|Making a calculation (mathematics)]]<br />[area of circle of radius 4, to 3 decimal places]
| <nowiki>{{#expr: ( pi * 4 ^ 2 ) round 3 }}</nowiki>
| {{#expr: ( pi * 4 ^ 2 ) round 3 }}
|-
| [[Help:Calculation|Testing the result of a calculation]]<br />[is 1230 even or odd?]
| <nowiki>{{#ifexpr: 1.23E+3 mod 2 | Odd | Even }}</nowiki>
| {{#ifexpr: 1.23E+3 mod 2 | Odd | Even }}
|}
{| class="wikitable" style="text-align:center;" width="100%"
|+Examples of system variables
! width="30%" | Description !! width="40%" | Text entered !! width="30%" | Result (for this help page)
|-
| rowspan="2" | Page names
| <nowiki>{{PAGENAME}}</nowiki>
| {{PAGENAME}}
|-
| <nowiki>{{FULLPAGENAME}}</nowiki>
| {{FULLPAGENAME}}
|-
| Name of the current namespace
| <nowiki>{{NAMESPACE}}</nowiki>
| {{NAMESPACE}}
|-
| Number of registered users
| <nowiki>{{NUMBEROFUSERS}}</nowiki>
| {{NUMBEROFUSERS}}
|-
| Number of pages in a given category
| <nowiki>{{PAGESINCATEGORY:"Weird Al" Yankovic albums}}</nowiki>
| {{PAGESINCATEGORY:"Weird Al" Yankovic albums}}
|-
| Current software version
| <nowiki>{{CURRENTVERSION}}</nowiki>
| {{CURRENTVERSION}}
|-
| Timestamp of last revision
| <nowiki>{{REVISIONTIMESTAMP}}</nowiki>
| {{REVISIONTIMESTAMP}}
|}
</td></tr></table>
The <code><nowiki>{{PAGENAME}}</nowiki></code> and <code><nowiki>{{NAMESPACE}}</nowiki></code> variables are particularly useful, and frequently used, to change template behaviour based on context. For example, if the template transcludes a category link (e.g., cleanup templates, which transclude a link categorizing the page as a page which needs cleanup), it will often check the <code><nowiki>{{NAMESPACE}}</nowiki></code> variable to make sure that talk pages, user pages, or anywhere else the tag might incidentally be placed do not themselves get categorized as pages needing cleanup.
===Nesting templates===
{{shortcut|WP:NEST}}
Templates may contain other templates—this is usually called ''{{dfn|nesting}}''. As the template is processed, the wikitext produced by any nested templates is transcluded into the nesting template, so that the final product is essentially processed from the most deeply nested template out. While fairly straightforward in application, it involves some noteworthy quirks and tricks.
To pass a parameter value to a nested template, place a parameter tag as the value of one of the nested template's parameters.
;Examples{{colon}}
:Template:A contains <code><nowiki>"the quick brown {{B|{{{3}}} }} jumps over..."</nowiki></code> This takes the value passed to the third positional parameter of Template:A and passes it as the first positional parameter of Template:B, then returns the wikitext produced by B as part of the phrase.
:Template:A contains <code><nowiki>"the quick brown {{B|waldo={{{3}}} }} jumps over..."</nowiki></code> As previously, except the third positional parameter of Template:A is passed to the named parameter "waldo" of Template:B.
Template parameters themselves can be chosen conditionally.
;Examples{{colon}}
:Template:A contains <code><nowiki>the quick brown {{B|{{{3}}}=fox}} jumps over...</nowiki></code> Template:A passes the word "fox" as a named parameter of Template:B whose name is A's third positional parameter.
:<code>{<nowiki />{#if: {{var|test string}} | {{var|value if test string is not empty}} | {<nowiki />{#if: {{var|test string}} | {{var|value if test string is not empty}} | {{var|value if test string is empty (or only white space)}} }} }}</code>
{{shortcut|WP:TEMPLATE LOOP}}
A template can call itself but will stop after one iteration to prevent an infinite loop.
When a nested template contains unmatched braces—as in <code>{{tl|lb}}}</code>—the unmatched braces are treated as text during processing, and do not affect the parsing of braces in the nesting template. If the nested template is substituted, however, the substitution is processed first, and this {{em|will}} change how braces are parsed in the nesting template. This has little practical use, but can occasionally introduce unexpected errors.
See [[meta:Help:Advanced templates]] and [[meta:Help:Recursive conversion of wikitext]] for more information. These pages also contain information on unusual calls such as <code><nowiki>{{template {{{parameter|}}} }}</nowiki></code>.
===Noinclude, includeonly, and onlyinclude===
{{shortcut|WP:NOINCLUDE|WP:INCLUDEONLY|WP:ONLYINCLUDE}}
By default, when a template is transcluded (or substituted), the entire wikitext (code) of the template page gets included in that of the target page. However, it is possible to modify that behaviour, using tags that specify which parts of the template code are to be included. This makes it possible to avoid transcluding information intended for display only on the template page itself, such as the template's documentation, [[WP:Category|categories]], and [[WP:Interwiki links|interwiki links]]. It is also possible to have parts of the code be transcluded, but {{em|not}} be processed on the template page itself (e.g., categories to be applied to the target pages which do not apply to the template). The tags are as follows:
* {{tag|noinclude}} – The text between the tags will {{em|not}} be included when the template is transcluded (substituted), but {{em|will}} be processed on the template's page; a common use is for template documentation.
* {{tag|onlyinclude}} – This specifies that nothing on the page {{em|except}} what appears between the tags will be transcluded (substituted).
* {{tag|includeonly}} – The text between the tags {{em|will}} be transcluded (substituted), but will {{em|not}} be processed on the template's own page.
{| class="wikitable" style="text-align: center;"
! Wikitext
! What is rendered {{em|here}} (source page)
! What is transcluded {{em|there}} (destination page)
|-
|<code><nowiki><noinclude> text1 </noinclude> text2</nowiki></code>
|<code>text1 text2</code>
|<code>text2</code>
|-
|<code><nowiki><onlyinclude> text1 </onlyinclude> text2</nowiki></code>
|<code>text1 text2</code>
|<code>text1</code>
|-
|<code><nowiki><includeonly> text1 </includeonly> text2</nowiki></code>
|<code>text2</code>
|<code>text1 text2</code>
|}
Perhaps the most common issue with the use of these blocks is unwanted spaces or lines. It is important to remember that the effect of these tags begins immediately before the first angle bracket, not on the previous line or at the previous visible character; similarly the effect ends immediately after the last angle bracket, not on the next line or with the next visible character. For example:
;<nowiki><includeonly></nowiki>
:{{xt|<nowiki>}}<includeonly></nowiki>}}
:{{!xt|<nowiki>}}</nowiki><br /><nowiki><includeonly></nowiki>}}
;<nowiki><noinclude></nowiki>
:{{xt|<nowiki>}}<noinclude></nowiki>}}
:{{!xt|<nowiki>}}</nowiki><br /><nowiki><noinclude></nowiki>}}
;<nowiki></includeonly><noinclude></nowiki>
:{{xt|<nowiki>{{template}}</includeonly><noinclude></nowiki>}}
:{{!xt|<nowiki>{{template}}</includeonly></nowiki><br /><nowiki><noinclude></nowiki>}}
These tags can be nested inside each other, though (for a given page) this really only applies to the {{tag|onlyinclude|o}} tag; nesting {{tag|includeonly|o}} and {{tag|noinclude|o}} tags is fairly pointless. Be careful to properly nest the tags, however. Constructions like {{tag|onlyinclude|o}}abc{{tag|includeonly|o}}def{{tag|onlyinclude|c}}ghi{{tag|includeonly|c}} will {{em|not}} work as expected. Use the "first opened, last closed" rule that is standard for HTML/XML.
===Problems and workarounds===
* If the first character produced by a template or [[WP:PF|parser function]] is one of four wiki markup characters—<code>:</code>, <code>;</code>, <code>*</code>, <code>#</code>{{efn|These are defined in the [https://web.archive.org/web/20180625163321/https://doc.wikimedia.org/mediawiki-core/master/php/classParser.html#ad463888e40c078ac9bcfcaf1231e39d7 <code>doBlockLevels</code> function of Parser.php].}}—then it is processed as though it were at the beginning of a line, even when the template tag is not. This allows the creation of various kinds of lists in templates where the template may not always be in the correct place for a list. To avoid this, either use {{xtag|nowiki|s}} before the markup or use the [[HTML]] entities <code>&#58;</code>, <code>&#59;</code>, <code>&#42;</code>, and <code>&#35;</code> respectively. In some cases, the HTML entities will work when the {{tag|nowiki|s}} does not. The problem often occurs when a parameter value in a template call starts with one of the four characters.
* For issues involving the substitution of templates, such as how to control whether subtemplates are substituted as well when the parent template is substituted, see [[Help:Substitution]].
* The template {{tlx|Strip whitespace}} can be used to strip any initial or final whitespace from unnamed parameter values if this would cause problems; named parameter values are automatically stripped in this way.
* For debugging templates, the following techniques are sometimes helpful:
** Use <code>subst:</code> to substitute a template (rather than transcluding it), which can show more clearly what is happening when the template is transcluded; see [[Help:Substitution]].
** Use <code>msgnw:</code> (short for "'''m'''e'''s'''sa'''g'''e, '''n'''o'''w'''iki") to more-or-less transclude the wikitext of the template page rather than the processed contents. It is not perfect: lists are rendered, comments are removed, and single newlines are replaced with spaces (which is particularly confounding when transcluding wikitext tables).
** Use [[Special:ExpandTemplates]] to see the full recursive expansion of one or more templates.
* To protect server resources and avoid infinite loops, the parser imposes certain limits on the depth transclusion nesting and on the page size with expanded templates. This may cause pages to break when using very complex templates, particularly if there are multiple such templates on the same page. For more information, see [[Wikipedia:Template limits]]. A page's overall load on the server can be checked by examining the generated HTML for a page and looking for the <code>NewPP limit report</code> comments.
* Do not use <code>=</code> wikimarkup to create section headers within a template which is intended for use in article space; this will create an <code>[edit]</code> link that, when transcluded, will confusingly open the template for editing.
** You may avoid <code>[edit]</code> links to the template by including <code><nowiki><includeonly>__NOEDITSECTION__</includeonly></nowiki></code>.
===Documentation===
{{Details|Wikipedia:Template documentation}}
Categorizing your template and documenting its proper usage will make it easier for other editors to find and use.
Documentation for users, together with the template's categories, is normally placed after the template code, inside {{tag|noinclude}} tags. It is normally necessary to put the opening {{tag|noinclude|o}} tag immediately after the end of the code, with no intervening spaces or newlines, to avoid transcluding unwanted whitespace.
In the case of complex templates, the documentation (together with categories) is often kept on a separate [[WP:Subpage|subpage]] of the template page (named "Template:{{var|XXX}}/doc"). This also applies to many [[WP:Protection|protected]] templates, which allows the information to be edited by non-administrators. This is achieved by placing the {{tlx|Documentation}} template after the main template code and within {{tag|noinclude}} tags. If the "/doc" subpage does not exist, a link will then appear enabling it to be created.
===Categorization===
====Categorize pages by template inclusion====
Some templates contain category definitions in their transcluded code, since they are intended to place the target 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, it may be necessary 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]]).
====Categorize templates====
Categorization declaration <code><nowiki>[[Category:Some-topic templates]]</nowiki></code> should be placed on the template's documentation page (or inside {{tag|noinclude}} tags if there is no documentation page) to avoid polluting the transcluding pages.
===Aliases===
Aliases can be created with [[WP:Redirect|redirects]]. For example, [[Template:Tsh]] redirects to [[Template:Template shortcut]]. You can then write {{tlx|tsh|{{var|foo}}}} instead of {{tlx|Template shortcut|{{var|foo}}}}.
It is good to prepare template aliases which only differ in whitespaces and capitalization. For example, there is a template called {{tlx|See Wiktionary}}. The "W" is capital, since the word "Wiktionary" is so, but a redirect {{tlx|See wiktionary}} with lower "w" exists because users may type the latter instead.
==Template limits==
{{Anchor|Expand limits}}
{{details|Wikipedia:Template limits}}
'''"Post-expand include size" limit.''' When templates are rendered or expanded to HTML for viewing in your browser, they use memory. This is called the "post-expand include size" and has a limit of 2,048,000 bytes. This size is included as an invisible comment in the HTML output—use your browser's view source feature to show the raw HTML and search for "newpp". The report will look like:
<syntaxhighlight lang="html4strict">
<!--
NewPP limit report
Preprocessor node count: 2382/1000000
Post-expand include size: 63476/2048000 bytes
Template argument size: 9517/2048000 bytes
Expensive parser function count: 2/500
-->
</syntaxhighlight>
The example shows that template expansion is using 63,476 bytes out of 2,048,000 bytes of available memory.
'''Display problem.''' If too many templates are included on a page, the post-expand include size may exceed the limit. When this happens, templates after the limit will no longer expand and will instead display as a wikilink (for example, {{red|1=<u>Template:{{var|Template name}}</u>}}). Common causes are the inclusion of too many citation templates and/or flag templates. To resolve this problem [[Wikipedia:Substitution|substitute]] templates, remove templates, or [[Wikipedia:Splitting|split]] the page.
'''Non-rendered tranclusions still count towards limit.''' For example, a page which contains only <code><nowiki>{{#if:{{:Main Page}}}}</nowiki></code> would still have a post-expand include size even though it would have no output at all.
The same applies to [[Wikipedia:Lua|Scribunto modules]]. For example, <code><nowiki>{{#invoke:Test|main}}</nowiki></code> would still increase post-expand include size even if Module:Test were simply:
<syntaxhighlight lang="lua">
mw.getCurrentFrame():preprocess'{{msgnw::Main Page}}'-- remove this line and post-expand include size becomes zero
return {main = function()end}-- p.main() has no return value
</syntaxhighlight>
==Lua programming language==
{{main article|Wikipedia:Lua}}
Since February 2013, the [[Lua (programming language)|Lua programming language]] is available for use through the [[mw:Extension:Scribunto|Scribunto]] MediaWiki extension. Lua code can be embedded into templates by employing the <code><nowiki>{{#invoke:}}</nowiki></code> functionality of the Scribunto MediaWiki extension. The Lua [[source code]] is stored in pages called modules, and these individual modules are then invoked on template pages. For example, [[Module:Bananas]] can be invoked using the code <code><nowiki>{{#invoke:Bananas|hello}}</nowiki></code> to print the text "Hello, world!".
==Template search==
{{main|Wikipedia:Template messages}}
As an alternative to using this index linked above, you can search the Template namespace using the [[Special:Search]] box below:
<div class="inputbox-hidecheckboxes">
<inputbox>
type=fulltext
width=25
namespaces=Template**
searchbuttonlabel=Search Template namespace
break=no
placeholder=i.e. Citation
</inputbox>
</div>
==See also==
{| class="wikitable"
!width=25%|Help pages
!width=25%|Manual pages
!width=25%|Special pages
!width=25%|Other pages not for direct viewing
|- style="vertical-align:top;"
|
*[[Wikipedia:Advanced template coding]]
* [[m:Help:Template]]
* [[meta:User:Happy-melon/Templates]]
* [[m:Help:Advanced templates]]<!-- m: not directly portable due to examples -->
* Introductions:
** [[Transclusion]]
** [[Help:A quick guide to templates]]
** [[mw:Help:Templates]]
* [[Help:Calculation]]
* [[Wikipedia:Template messages]]
* [[Help:Magic words]]
* [[mw:Help:Magic words]]
* [[mw:Help:Extension:ParserFunctions]]
* [[m:Help:Parameter default]]<!-- m: -->
* [[Help:Substitution]]<!-- maybe portable, test it -->
* [[Wikipedia:Template documentation]]
* [[Wikipedia:Template namespace]]
* [[Wikipedia:Template limits]]
* [[Wikipedia:Template test cases]]
* [[Wikipedia:WikiProject Templates]]
|
* [[mw:Manual:$wgEnableScaryTranscluding|Interwiki transclusion]]
* [[mw:Extension:ExpandTemplates]]
* [[mw:Manual:Parser.php]]
|
* [[Wikipedia:Database reports/Templates transcluded on the most pages]]—shows the number of pages in which each template is transcluded, not counting transclusions through redirects
* [[Special:Unusedtemplates]]—templates which are not transcluded (however, they may be used for substitution)
* [{{fullurl:Special:Allpages|namespace=10}} Special:Allpages (namespace 10)] all templates on this server
* [[Special:ExpandTemplates]]
|
* Multilingual portal pages, see [[meta:www]]
* CSS and JS pages, see [[Help:User style]]
* Pages in the MediaWiki namespace, see [[Help:System message]]
* [[Wikipedia:Book sources]], used by [[Special:Booksources]]
* [[m:Spam blacklist]]
|}
==Notes==
{{Notelist}}
{{Wikipedia template messages|state=collapsed}}
{{Wikipedia technical help|collapsed}}
[[Category:Template documentation| ]]
[[Category:Wikipedia how-to]]
[[Category:Wikipedia template help]]
{{Pp-semi-indef}}
{{Wikipedia how to|H:T|WP:TEMPLATE}}
{{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 any number of articles or pages. They are commonly used for [[Wikipedia:Glossary#Boilerplate text|boilerplate]] messages, standard warnings or notices, [[Help:Infobox|infoboxes]], [[WP:Navbox|navigational boxes]], and similar purposes.
The most common method of inclusion is called [[Wikipedia:Transclusion|transclusion]], where the [[WP:wikitext|wikitext]] 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 wikitext 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 and Wikimedia at [[mw:Help:Templates]], [[m:Help:Template]], and [[m:Help:Advanced templates]].
==General description==
[[File:Introduction to templates on Wikipedia.webm|thumb|A basic overview of how templates work (8-minute video)]]
Most templates are pages in the [[WP:Template namespace|Template namespace]], which means that they have titles in the form "Template:{{var|XXXX}}". It is possible, however, to transclude and substitute from any namespace,{{efn|Namespaces from which transclusion is not allowed are specified on a wiki by the variable <code>[[mw:Manual:$wgNonincludableNamespaces|$wgNonincludableNamespaces]]</code>.}} and so some template pages are placed in other namespaces, such as the [[WP:User namespace|User namespace]]. Template pages have associated [[WP:talk page|talk pages]].
Templates can contain any desired [[WP:Wikitext|wikitext]], including calls to other templates. They have some limited programming capacities: customizable values (called [[#Parameters|parameters]]); calculation and branchings (using [[Help:Parser functions|parser functions]]); and access to wiki-specific variables ([[Help:Magic words|magic words]]), such as dates, times, and page names. They may also contain tags which define which parts of the wikitext are to be included when the template is transcluded or substituted. This means that the appearance of the template page itself need not be the same as that of the transcluded content (for example, it can contain documentation, categories, etc. for the template).
{{strong|How to do it:}} To transclude a template into an article or page, type {{tnull|{{var|Template name}}}} in the wikitext at the place where the template is to appear. The first letter may be indifferently lower- or upper-case.
The prefix <code>Template:</code> before the template name is the default one and need not be included. However, for templates stored in other namespaces, the prefix, such as <code>User:</code>, must be specified. To transclude a page in [[WP:mainspace#Namespace|mainspace]], precede its title with a colon, as <code>{<nowiki />{:{{var|Page name}}}}</code>.
{{A note}} Attempting to transclude a template that does not exist produces a [[WP:Redlink|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.
==Usage syntax==
===Parameters===
{{about|template parameters|search parameters|Help:Searching#Parameters|section=yes}}
{{further|m:Help:Templates#Parameters}}
{{shortcut|H:PARAMETER|WP:PARAMETER}}
<!--[[Help:Parameters]] redirects here.-->
The basic transclusion syntax given above can be extended by the addition of [[parameter]]s, which are used to control the template's output. The syntax for this is
{{tnull|{{var|Template name}}|{{var|parameter}}|{{var|parameter}}|{{var|...}}}}
where <code>{{var|Template name}}</code> is the name of the template, and each <code>{{var|parameter}}</code> may either contain just a value (these are called ''{{dfn|unnamed parameters}}'') or be of the form <code>{{var|name}}={{var|value}}</code> (''{{dfn|named parameters}}''). The first, second, third, etc. unnamed parameters will be given the names <code>1</code>, <code>2</code>, <code>3</code>, etc.
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.
Which parameters (if any) can or should be passed to a template and how they are to be named depends on the coding of that template. Named parameters can be defined in any order. Superfluous or misnamed parameters will be ignored; undefined parameters will be assigned default values. If a parameter is defined more than once, the last value takes effect.
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 omitting the parameter altogether, which leaves it undefined, although templates are often coded so as to behave the same in both cases.
Parameters can be specified (and will do nothing) even if not represented in the template's code. For example, {{para|reason}} is frequently used as a pseudo-parameter to explain briefly in the wikisource why the template was placed.{{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 call [[Module:Check for unknown parameters]] 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 [[WP: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 include a new parameter, its call to the module must also be updated to include the new parameter.
===Calling===
{{Further|mw:Help:Magic words}}
Calling a template is not unlike calling a [[Variable (computer science)|variable]] or a [[Function (computer science)|function]] in a programming language: call it with a reference and it returns a value (the [[Input/output|output]]). Like functions, some templates accept parameters that change the output.
In [[MediaWiki]], the [[wiki software]] that Wikipedia uses, [[Help:Magic words#Variables|variables]] have a more specific meaning that distinguishes them from templates, but they are both identified by double [[Curly brackets|braces]] <code><nowiki>{{ }}</nowiki></code> and they both return a value.
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-sensitive (except for the first character); underscores 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]] and [[HTML]].
The [[number sign]] <code>#</code> is called a [[fragment identifier]] because it denotes a {{em|fragment}} or section of a document (such as a [[Help:Section|section]] in a Wikipedia article). Although it [[WP:ANCHOR|can be used]] to link to a section of a template page (like [[Template:Portal#Example]]), there is no reason to put a fragment identifier or fragment name in a template reference. In {{tnull|Portal{{var|#Location}}|Books}}, for example, the string <code>{{var|#Location}}</code> has no purpose and is thus ignored.
===Substitution===
{{Further|Help:Substitution}}
When a template is substituted, its content is [[hard-coded]] in the page rather than [[WP:Transclusion|transcluded]]. To learn how and [[Help:Substitution#When to use substitution|when]] to substitute a template, see [[Help:Substitution]].
==Basic template usage examples==
{{A note}} If you wish to experiment with any of these, please use either the [[Template:Template sandbox|template sandbox]], or your user page or sandbox.
An example of a very simple template can be found at [[Template:Lambda]], which expands out to place the lambda symbol (λ) at that point in the text. A programmer would say that the template {{em|returns}} the lambda symbol (λ).
Click on [[Template:Lambda]], then click on the "Edit source" tab to see the template code (its wikitext). The "active" part of that code, called the {{dfn|expansion}} of the template, is the single word <code>&lambda;</code>.{{efn|<code>&lambda;</code> is the [[List of XML and HTML character entity references|HTML character entity]] for the Greek letter (λ).}} The remainder of the wikitext is enclosed between {{xtag|noinclude}} tags so it is displayed on the template page itself but will not be displayed when the template is used (or called) on another page.
To transclude Template:Lambda onto another page (i.e., to use it on another page), type {{tlx|lambda}} (or {{tlx|Lambda}}—the case of the first letter is not significant) into the wikitext of the target page and press {{Button|Show preview}}. The page will be displayed with the {{dfn|template call}} replaced by the expansion of the template, as if the wikitext actually contained <code>&lambda;</code> at that point. The displayed page will therefore contain the text "λ".
For example, type <code>The 11th letter of the Greek alphabet is the lambda ({{tl|lambda}})</code> and you will see "The 11th letter of the Greek alphabet is the lambda ({{lambda}})" when previewing the page or after saving the change. You can use templates without knowing the details of their code; you only need to remember what result they produce, which is usually described on the template page.
Another way to use this template is to [[Help:Substitution|substitute]] it. If you type <code>The 11th letter of the Greek alphabet is the lambda ({{tls|lambda}})</code> and preview or save the page, you will again see "The 11th letter of the Greek alphabet is the lambda (λ)". If you look again at the saved wikitext,{{efn|Since the new wikitext itself needs to be reviewed, and new wikitext itself cannot be previewed during source editing, previewing the page will not illustrate the result of the template substitution {{em|in the wikitext}}.}} however, you will see that the template calls really were replaced by the expansion of the template when you saved the page. The link between the output text and the template is now broken, and the output will not be affected by future changes to the template (as it would be in the case of transclusion).
==={{anchor|Parameters}} Examples with parameters===
An example of a template that takes parameters is the template {{tlx|about}}. Try typing {{tnull|about|how to use templates|how to use modules|Help:Module}} in the sandbox—it will produce the following text:
{{about|how to use templates|how to use modules|Help:Module}}
The template {{tlx|about}} uses three unnamed parameters in the example above, but the same template can also be used with different numbers of parameters to give slightly different results, as explained in [[Template:About/doc|the template's documentation]]. For example, {{tnull|about||how to use modules|Help:Module}}. Note the usage of an empty parameter—in this instance, the consecutive pipes mean that the first parameter that was "passed" to the template is an empty string, which in this template will cause it to omit the initial "about" sentence. This produces: {{about||how to use modules|Help:Module}}
Other templates, particularly more complex ones, take named parameters or a mixture of named and unnamed ones. A simple example is [[Template:Payoff matrix]], used to generate a 2-by-2 grid. For example:
{{Markup|<nowiki>{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}</nowiki>|{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}}}
See the [[Template:Payoff matrix|template page]] for more possibilities. Notice that the template is used here without defining all its possible parameters—undefined parameters are given default values.
The spaces around the equal signs and before and after the parameters are used only for clarity—they are not needed and are ignored when the template is evaluated (although this is not the case with unnamed parameters). Parameter names {{em|are}} fully case-sensitive, though; for example, it is not possible to replace <code>DR</code> with <code>dr</code> or <code>dR</code> in the above example. Parameters with names that are not used by the template are simply ignored.
Examining the source code of the template shows the standard table mark up with some extra triple bracket entities representing the parameters:
<syntaxhighlight lang="moin">
{| id="Payoff matrix" style="background:white; float: {{{Float|right}}}; clear:right; text-align:center;" align={{{Float|right}}} cellspacing=0 cellpadding=8 width={{{Width|225}}}
|-
|style="width:33%; "|
|style="width:33%; border-bottom: solid black 1px;"| {{{2L|Left}}}
|style="width:33%; border-bottom: solid black 1px;"| {{{2R|Right}}}
|-
|style="border-right: solid black 1px; text-align: right; "| {{{1U|Up}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{ULc|white}}}; font-size:120%; "| {{{UL|0, 0}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{URc|white}}}; font-size:120%; "| {{{UR|0, 0}}}
|-
|style="border-right: solid black 1px; text-align: right; "| {{{1D|Down}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{DLc|white}}}; font-size:120%; "| {{{DL|0, 0}}}
|style="border-right: solid black 1px; border-bottom: solid black 1px; background:{{{DRc|white}}}; font-size:120%; "| {{{DR|0, 0}}}
|-
|style="font-size: 90%;" colspan=3 |''{{{Name|{{PAGENAME}}}}}''
|}</syntaxhighlight>
The entity <syntaxhighlight lang="moin" inline>{{{2L|Left}}}</syntaxhighlight> instructs the template to use the named parameter <code>2L</code> or the text <code>Left</code> if the parameter is not present in the call.
==Usage hints and workarounds==
The following points may be worth noting when using templates:
* An unnamed parameter cannot contain an ordinary equals sign, as this would be interpreted as setting off a named parameter.{{efn|This does not apply if the equals sign comes within another template call or other item which the parser handles separately.}} To pass an equals sign in an unnamed parameter (for example in a [[URL]] with [[attribute–value pair]]s), replace the equals sign with the special template {{tlx|{{=}}}}, which returns an equals sign that will not be interpreted. Another method is to replace the unnamed parameter (and any subsequent unnamed parameters) with named parameters—the first unnamed parameter is equivalent to a named parameter {{para|1}} and so on. To call template {{tlx|done}} with <code>a=b</code> as the literal value for the first parameter, type either <code><nowiki>{{done|a{{=}}b}}</nowiki></code> or <code><nowiki>{{done|1=a=b}}</nowiki></code>.
* Similarly, it is not possible to use an ordinary pipe character <code>|</code> in template parameters, as it will be interpreted as a separator.{{efn|Again, this does not apply if it comes within another separately parsed item, such as a piped wikilink.}} This time, the problem can be solved by using the [[mw:Help:Magic words#Other|magic word]] {{tlx|!}} in place of the pipe, or—if the pipe is not intended to be parsed at a higher level—using the [[List of XML and HTML character entity references|HTML entity]] <code>&#124;</code>.
* Remember that whitespace characters (spaces, tabs, carriage returns, and line feeds) are not automatically stripped from the start and end of unnamed parameters, unlike with named parameters. Including such characters (or any other non-visible characters in any parameters) may in some cases affect the template's behaviour in unexpected ways. (Template designers can use {{tlx|Strip whitespace}} to remove unwanted whitespace in unnamed parameters.)
* In documentation and discussions, it is often convenient to be able to produce the template call syntax with a link to the template in question, but without actually calling the template. This can be done easily using the {{tlx|tl}} template (the "'''t'''emplate '''l'''ink" template). For example, {{tnull|tl|Example}} produces {{tl|Example}}. There are multiple other [[Template:Template-linking templates|template-linking templates]] available with different functionalities.
* When a template is changed (when the template or one of its subtemplates is edited), the change will be reflected on all pages on which the template is transcluded. However, the change may not become visible on all pages immediately; a previously cached version of a page, based on the previous version of the template, may continue to be displayed for some time. Use the [[Help:Purge|purge]] function to force a page to be displayed using the latest versions of templates—including on the template page itself, if it contains usage examples.
* When viewing old versions of pages, remember that templates will be transcluded as they are now, not necessarily as they were when the old page version was active.
* To list all pages onto which a template is transcluded, use [[Help:What links here|What links here]] on the template page. This will {{em|not}} include pages where the template has been substituted.
* To get a list of templates transcluded on a page, click "Edit" and find the list below the edit window. This list also includes the subtemplates used by the templates that are directly transcluded. To get such a list for a page section, an old version of the page,{{efn|For an old version, the subtemplate tree will be constructed according to the templates' current state.}} or your newly edited version prior to saving, click {{Button|Show preview}} on the appropriate edit page.
* There are limits to the number and complexity of the templates that an article may have. See the "[[#Expand limits|§ Expand limits]]" section for help in resolving this.
* If you want the template to leave a [[WP:~|time stamp or signature]], you can write <code><noinclude><nowiki></noinclude><nowiki>~~~~~</nowiki><noinclude></nowiki></noinclude></code>, but this will only work if you substitute the template. If you transclude it, you'll just get <nowiki>~~~~~</nowiki>.
* To improve readability, usually programmers like to split the code with newlines and indent it. Unfortunately, [[MediaWiki]] software does not allow this functionality; in many instances, these purpose-built newlines are treated by the software as content. One possible workaround is to add <code><!--</code> before each newline character and <code>--></code> after it, which produces an [[HTML comment]].
==Creating and editing templates==
<!--[[Help:Subtemplates]] redirects here.-->
Templates are created and edited in much the same way as any other page: choose an appropriate name, navigate to that page, then click the "Edit" tab or create a new page as needed. As mentioned above, templates are normally placed in the [[WP:Template namespace|Template namespace]], though templates intended for your own personal use or for experimentation can be created in your own [[WP:user space|user space]]. Anything that can be included on a normal page or article can be included on a template, including other templates (called ''{{dfn|subtemplates}}''). Templates often make use of programming features—parameters, parser functions, and other [[Help:Magic words|magic words]]—which allow the transcluded content to vary depending on context. There are also special tags to control which information is transcluded and which is not.
Before creating a template, do a quick search for existing templates (such as by exploring [[:Category:Wikipedia templates]]) to see if there is already a template that does what you want or a similar template whose code can be copied and modified (or left in place and expanded). Look for generic templates on which the new template can be based; for example, [[WP:Navbox|navbox]] templates can be easily created by calling the generic [[Template:Navbox]].
There is no hard rule about what name to choose for a template—make it short but reasonably descriptive. If similar templates exist, try to follow a consistent naming pattern. Templates can be renamed without breaking existing transclusions, provided a [[WP:Redirect|redirect]] to the new template name is left behind.
Be extremely careful when editing existing templates—changes made can affect a large number of pages, often in ways you might not expect. For this reason many high-use templates are [[WP:Protection|protected]] against editing except by [[WP:Administrator|administrators]] and [[WP:Template editor|template editors]]; other editors can propose changes on the talk page. Some templates offer a sandbox and [[Wikipedia:Template test cases|test cases]] for experimentation.
To propose the deletion or merger of unused or inappropriate templates or templates that can easily be merged, go to [[WP:Templates for discussion|Templates for discussion]] (TfD).
===Handling parameters===
The values of the parameters which can be fed to a template are represented in the template code by items enclosed between {{em|triple}} braces:
* The code <code><nowiki>{{{xxx}}}</nowiki></code> will be replaced by the value of the parameter named <code>xxx</code>. these are known as ''named parameters''.
* The codes <code><nowiki>{{{1}}}</nowiki></code>, <code><nowiki>{{{2}}}</nowiki></code>, and so on will be replaced by the first, second, and so on ''unnamed parameter'' (or the value of a parameter named <code>1</code>, <code>2</code>, etc.); these are sometimes known as ''{{dfn|positional parameter}}s''.
If a parameter is not assigned a value, then no replacement will take place; this means that if no value is passed for parameter {{para|xxx}}, the value of the expression <code><nowiki>{{{xxx}}}</nowiki></code> inside the template will literally be ''<nowiki>{{{xxx}}}</nowiki>'', not the 'blank' you may have expected. A more intuitive behaviour can be achieved by specifying default parameter values. This is done with the ''pipe syntax'': <code><nowiki>{{{xxx|dflt}}}</nowiki></code> specifies the default value <code>dflt</code> for the named parameter {{para|xxx}}, and <code><nowiki>{{{1|dflt}}}</nowiki></code> specifies the default value <code>dflt</code> for the first unnamed parameter. Most often, this is used to specify null default values, such as <code><nowiki>{{{1|}}}</nowiki></code> or <code><nowiki>{{{xxx|}}}</nowiki></code>.
Note the default text must be real text: that you cannot assign another parameter as the default. It will simply return the name of the parameter as text. <!--For example, suppose the template 'Template:Hello' is <code>{{{1|param}}}</code>, and is called with <code><nowiki>{{hello|param=world}}</nowiki></code>. Parameter '1' does not exist, so it will call the default. The text returned is the "param" or what would be expected to be the parameter name, not the parameter's assignment "world". This would be best implemented by a sequence of if [[mw:Help:Extension:ParserFunctions|parser statements]].-->
<!-- Also note that if a template is called with the parameter specified as blank (e.g. <code>{{Example|}}</code>), the default for the parameter will not appear. If that is undesired one can check if the parameter exists through the parser code<code><nowiki>{{#if:{{{1|}}}|{{{1}}}|default}}</nowiki></code> instead. This will produce the text "default" even if the parameter is specified as empty.-->
Because of the multiple uses of double-brace and triple-brace syntax, expressions can sometimes be ambiguous. It may be helpful or necessary to include spaces to resolve such ambiguity. For example, <code><nowiki>{{ {{{xxx}}} }}</nowiki></code> or <code><nowiki>{{{ {{xxx}} }}}</nowiki></code>, rather than typing five consecutive braces, may be more human-readable. However, watch out for unwanted whitespace appearing in template expansions.
====Special case: parameters within an XML-style opening tag====
Parameters do not get expanded when they are wrapped in {{xtag|nowiki}} tags. They aren't expanded either if placed within the actual [[XML]]-style opening tag. Thus, the following will not work within a template:
*{{!mxt|<nowiki><ref name={{{param}}}> Smith, Adam (1776)...</ref></nowiki>}}
because the parameter is not expanded. Instead, you can use the <code><nowiki>{{#tag:}}</nowiki></code> [[WP:PF|parser function]], which is—for example—used in {{tlx|sfn}} to generate the {{tag|ref}} element; see also [[Help:Magic words#Formatting|Help:Magic words § Formatting]]. Therefore, the following example will work:
*{{mxt|<nowiki>{{#tag:ref | Marx, Karl (1848)... | name={{{param}}} }}</nowiki>}}
====Caution: overextending URLs====
If a parameter's value is (or ends with) a [[URL]], check whether it is displayed in Wikipedia with the link overextending by one or more characters after the URL so that clicking the link causes an error or failure. Ensure that, after processing by the software, a soft space ({{em|not}} a [[Non-breaking space|hard or non-breaking space]]) follows the URL, regardless of whether you or a user supplied the URL or whether it was generated by automated processing. Possibly, the source code could contain or generate a space that is discarded in the processing or there might not be any space there. Correct the source code, perhaps by forcing a soft space to appear after the URL. The {{tlx|spaces}} template may be useful.
===System variables and conditional logic===
Template code often makes use of the variables and parser functions described at [[Help:Magic words]] in order to make the template's behaviour depend on the environment (such as the current time or namespace) or on the parameter values that are passed to it. They can also be used for arithmetical calculations, but certain standard programming features such as loops and variable assignment are not available. Full string manipulation is also not available; some templates providing such functionality 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]].
<table><tr><td>
{| class="wikitable" style="text-align:center;" width="100%"
|+Examples of core parser functions
! width="30%" | Description !! width="40%" | Text entered !! width="30%" | Result
|-
| Uppercasing text
| <nowiki>{{uc: Heavens to BETSY! }}</nowiki>
| {{uc: Heavens to BETSY! }}
|-
| Lowercasing text
| <nowiki>{{lc: Heavens to BETSY! }}</nowiki>
| {{lc: Heavens to BETSY! }}
|-
| Getting a namespace name
| <nowiki>{{NS: 1 }}</nowiki>
| {{NS: 1 }}
|-
| Getting a Wikipedia URL
| <nowiki>{{fullurl: pagename }}</nowiki>
| {{fullurl: pagename }}
|}
The ParserFunctions extension provides more programming-oriented parser functions.
{| class="wikitable" style="text-align:center;" width="100%"
|+Examples of extension parser functions
! width="30%" | Description !! width="40%" | Text entered !! width="30%" | Result
|-
| rowspan="2" | Testing for equality between two strings (or parameters)
| <nowiki>{{#ifeq: yes | yes | Hooray...! | Darn...! }}</nowiki>
| {{#ifeq: yes | yes | Hooray...! | Darn...! }}
|-
| <nowiki>{{#ifeq: yes | no | Hooray...! | Darn...! }}</nowiki>
| {{#ifeq: yes | no | Hooray...! | Darn...! }}
|-
| Testing whether a string (or parameter) contains anything (other than whitespace)
| <nowiki>{{#if: {{{param|}}} | Hooray...! | Darn...! }}</nowiki>
| {{#if: {{{param|}}} | Hooray...! | Darn...! }}
|-
| [[Help:Calculation|Making a calculation (mathematics)]]<br />[area of circle of radius 4, to 3 decimal places]
| <nowiki>{{#expr: ( pi * 4 ^ 2 ) round 3 }}</nowiki>
| {{#expr: ( pi * 4 ^ 2 ) round 3 }}
|-
| [[Help:Calculation|Testing the result of a calculation]]<br />[is 1230 even or odd?]
| <nowiki>{{#ifexpr: 1.23E+3 mod 2 | Odd | Even }}</nowiki>
| {{#ifexpr: 1.23E+3 mod 2 | Odd | Even }}
|}
{| class="wikitable" style="text-align:center;" width="100%"
|+Examples of system variables
! width="30%" | Description !! width="40%" | Text entered !! width="30%" | Result (for this help page)
|-
| rowspan="2" | Page names
| <nowiki>{{PAGENAME}}</nowiki>
| {{PAGENAME}}
|-
| <nowiki>{{FULLPAGENAME}}</nowiki>
| {{FULLPAGENAME}}
|-
| Name of the current namespace
| <nowiki>{{NAMESPACE}}</nowiki>
| {{NAMESPACE}}
|-
| Number of registered users
| <nowiki>{{NUMBEROFUSERS}}</nowiki>
| {{NUMBEROFUSERS}}
|-
| Number of pages in a given category
| <nowiki>{{PAGESINCATEGORY:"Weird Al" Yankovic albums}}</nowiki>
| {{PAGESINCATEGORY:"Weird Al" Yankovic albums}}
|-
| Current software version
| <nowiki>{{CURRENTVERSION}}</nowiki>
| {{CURRENTVERSION}}
|-
| Timestamp of last revision
| <nowiki>{{REVISIONTIMESTAMP}}</nowiki>
| {{REVISIONTIMESTAMP}}
|}
</td></tr></table>
The <code><nowiki>{{PAGENAME}}</nowiki></code> and <code><nowiki>{{NAMESPACE}}</nowiki></code> variables are particularly useful, and frequently used, to change template behaviour based on context. For example, if the template transcludes a category link (e.g., cleanup templates, which transclude a link categorizing the page as a page which needs cleanup), it will often check the <code><nowiki>{{NAMESPACE}}</nowiki></code> variable to make sure that talk pages, user pages, or anywhere else the tag might incidentally be placed do not themselves get categorized as pages needing cleanup.
===Nesting templates===
{{shortcut|WP:NEST}}
Templates may contain other templates—this is usually called ''{{dfn|nesting}}''. As the template is processed, the wikitext produced by any nested templates is transcluded into the nesting template, so that the final product is essentially processed from the most deeply nested template out. While fairly straightforward in application, it involves some noteworthy quirks and tricks.
To pass a parameter value to a nested template, place a parameter tag as the value of one of the nested template's parameters.
;Examples{{colon}}
:Template:A contains <code><nowiki>"the quick brown {{B|{{{3}}} }} jumps over..."</nowiki></code> This takes the value passed to the third positional parameter of Template:A and passes it as the first positional parameter of Template:B, then returns the wikitext produced by B as part of the phrase.
:Template:A contains <code><nowiki>"the quick brown {{B|waldo={{{3}}} }} jumps over..."</nowiki></code> As previously, except the third positional parameter of Template:A is passed to the named parameter "waldo" of Template:B.
Template parameters themselves can be chosen conditionally.
;Examples{{colon}}
:Template:A contains <code><nowiki>the quick brown {{B|{{{3}}}=fox}} jumps over...</nowiki></code> Template:A passes the word "fox" as a named parameter of Template:B whose name is A's third positional parameter.
:<code>{<nowiki />{#if: {{var|test string}} | {{var|value if test string is not empty}} | {<nowiki />{#if: {{var|test string}} | {{var|value if test string is not empty}} | {{var|value if test string is empty (or only white space)}} }} }}</code>
{{shortcut|WP:TEMPLATE LOOP}}
A template can call itself but will stop after one iteration to prevent an infinite loop.
When a nested template contains unmatched braces—as in <code>{{tl|lb}}}</code>—the unmatched braces are treated as text during processing, and do not affect the parsing of braces in the nesting template. If the nested template is substituted, however, the substitution is processed first, and this {{em|will}} change how braces are parsed in the nesting template. This has little practical use, but can occasionally introduce unexpected errors.
See [[meta:Help:Advanced templates]] and [[meta:Help:Recursive conversion of wikitext]] for more information. These pages also contain information on unusual calls such as <code><nowiki>{{template {{{parameter|}}} }}</nowiki></code>.
===Noinclude, includeonly, and onlyinclude===
{{shortcut|WP:NOINCLUDE|WP:INCLUDEONLY|WP:ONLYINCLUDE}}
By default, when a template is transcluded (or substituted), the entire wikitext (code) of the template page gets included in that of the target page. However, it is possible to modify that behaviour, using tags that specify which parts of the template code are to be included. This makes it possible to avoid transcluding information intended for display only on the template page itself, such as the template's documentation, [[WP:Category|categories]], and [[WP:Interwiki links|interwiki links]]. It is also possible to have parts of the code be transcluded, but {{em|not}} be processed on the template page itself (e.g., categories to be applied to the target pages which do not apply to the template). The tags are as follows:
* {{tag|noinclude}} – The text between the tags will {{em|not}} be included when the template is transcluded (substituted), but {{em|will}} be processed on the template's page; a common use is for template documentation.
* {{tag|onlyinclude}} – This specifies that nothing on the page {{em|except}} what appears between the tags will be transcluded (substituted).
* {{tag|includeonly}} – The text between the tags {{em|will}} be transcluded (substituted), but will {{em|not}} be processed on the template's own page.
{| class="wikitable" style="text-align: center;"
! Wikitext
! What is rendered {{em|here}} (source page)
! What is transcluded {{em|there}} (destination page)
|-
|<code><nowiki><noinclude> text1 </noinclude> text2</nowiki></code>
|<code>text1 text2</code>
|<code>text2</code>
|-
|<code><nowiki><onlyinclude> text1 </onlyinclude> text2</nowiki></code>
|<code>text1 text2</code>
|<code>text1</code>
|-
|<code><nowiki><includeonly> text1 </includeonly> text2</nowiki></code>
|<code>text2</code>
|<code>text1 text2</code>
|}
Perhaps the most common issue with the use of these blocks is unwanted spaces or lines. It is important to remember that the effect of these tags begins immediately before the first angle bracket, not on the previous line or at the previous visible character; similarly the effect ends immediately after the last angle bracket, not on the next line or with the next visible character. For example:
;<nowiki><includeonly></nowiki>
:{{xt|<nowiki>}}<includeonly></nowiki>}}
:{{!xt|<nowiki>}}</nowiki><br /><nowiki><includeonly></nowiki>}}
;<nowiki><noinclude></nowiki>
:{{xt|<nowiki>}}<noinclude></nowiki>}}
:{{!xt|<nowiki>}}</nowiki><br /><nowiki><noinclude></nowiki>}}
;<nowiki></includeonly><noinclude></nowiki>
:{{xt|<nowiki>{{template}}</includeonly><noinclude></nowiki>}}
:{{!xt|<nowiki>{{template}}</includeonly></nowiki><br /><nowiki><noinclude></nowiki>}}
These tags can be nested inside each other, though (for a given page) this really only applies to the {{tag|onlyinclude|o}} tag; nesting {{tag|includeonly|o}} and {{tag|noinclude|o}} tags is fairly pointless. Be careful to properly nest the tags, however. Constructions like {{tag|onlyinclude|o}}abc{{tag|includeonly|o}}def{{tag|onlyinclude|c}}ghi{{tag|includeonly|c}} will {{em|not}} work as expected. Use the "first opened, last closed" rule that is standard for HTML/XML.
===Problems and workarounds===
* If the first character produced by a template or [[WP:PF|parser function]] is one of four wiki markup characters—<code>:</code>, <code>;</code>, <code>*</code>, <code>#</code>{{efn|These are defined in the [https://web.archive.org/web/20180625163321/https://doc.wikimedia.org/mediawiki-core/master/php/classParser.html#ad463888e40c078ac9bcfcaf1231e39d7 <code>doBlockLevels</code> function of Parser.php].}}—then it is processed as though it were at the beginning of a line, even when the template tag is not. This allows the creation of various kinds of lists in templates where the template may not always be in the correct place for a list. To avoid this, either use {{xtag|nowiki|s}} before the markup or use the [[HTML]] entities <code>&#58;</code>, <code>&#59;</code>, <code>&#42;</code>, and <code>&#35;</code> respectively. In some cases, the HTML entities will work when the {{tag|nowiki|s}} does not. The problem often occurs when a parameter value in a template call starts with one of the four characters.
* For issues involving the substitution of templates, such as how to control whether subtemplates are substituted as well when the parent template is substituted, see [[Help:Substitution]].
* The template {{tlx|Strip whitespace}} can be used to strip any initial or final whitespace from unnamed parameter values if this would cause problems; named parameter values are automatically stripped in this way.
* For debugging templates, the following techniques are sometimes helpful:
** Use <code>subst:</code> to substitute a template (rather than transcluding it), which can show more clearly what is happening when the template is transcluded; see [[Help:Substitution]].
** Use <code>msgnw:</code> (short for "'''m'''e'''s'''sa'''g'''e, '''n'''o'''w'''iki") to more-or-less transclude the wikitext of the template page rather than the processed contents. It is not perfect: lists are rendered, comments are removed, and single newlines are replaced with spaces (which is particularly confounding when transcluding wikitext tables).
** Use [[Special:ExpandTemplates]] to see the full recursive expansion of one or more templates.
* To protect server resources and avoid infinite loops, the parser imposes certain limits on the depth transclusion nesting and on the page size with expanded templates. This may cause pages to break when using very complex templates, particularly if there are multiple such templates on the same page. For more information, see [[Wikipedia:Template limits]]. A page's overall load on the server can be checked by examining the generated HTML for a page and looking for the <code>NewPP limit report</code> comments.
* Do not use <code>=</code> wikimarkup to create section headers within a template which is intended for use in article space; this will create an <code>[edit]</code> link that, when transcluded, will confusingly open the template for editing.
** You may avoid <code>[edit]</code> links to the template by including <code><nowiki><includeonly>__NOEDITSECTION__</includeonly></nowiki></code>.
===Documentation===
{{Details|Wikipedia:Template documentation}}
Categorizing your template and documenting its proper usage will make it easier for other editors to find and use.
Documentation for users, together with the template's categories, is normally placed after the template code, inside {{tag|noinclude}} tags. It is normally necessary to put the opening {{tag|noinclude|o}} tag immediately after the end of the code, with no intervening spaces or newlines, to avoid transcluding unwanted whitespace.
In the case of complex templates, the documentation (together with categories) is often kept on a separate [[WP:Subpage|subpage]] of the template page (named "Template:{{var|XXX}}/doc"). This also applies to many [[WP:Protection|protected]] templates, which allows the information to be edited by non-administrators. This is achieved by placing the {{tlx|Documentation}} template after the main template code and within {{tag|noinclude}} tags. If the "/doc" subpage does not exist, a link will then appear enabling it to be created.
===Categorization===
====Categorize pages by template inclusion====
Some templates contain category definitions in their transcluded code, since they are intended to place the target 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, it may be necessary 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]]).
====Categorize templates====
Categorization declaration <code><nowiki>[[Category:Some-topic templates]]</nowiki></code> should be placed on the template's documentation page (or inside {{tag|noinclude}} tags if there is no documentation page) to avoid polluting the transcluding pages.
===Aliases===
Aliases can be created with [[WP:Redirect|redirects]]. For example, [[Template:Tsh]] redirects to [[Template:Template shortcut]]. You can then write {{tlx|tsh|{{var|foo}}}} instead of {{tlx|Template shortcut|{{var|foo}}}}.
It is good to prepare template aliases which only differ in whitespaces and capitalization. For example, there is a template called {{tlx|See Wiktionary}}. The "W" is capital, since the word "Wiktionary" is so, but a redirect {{tlx|See wiktionary}} with lower "w" exists because users may type the latter instead.
==Template limits==
{{Anchor|Expand limits}}
{{details|Wikipedia:Template limits}}
'''"Post-expand include size" limit.''' When templates are rendered or expanded to HTML for viewing in your browser, they use memory. This is called the "post-expand include size" and has a limit of 2,048,000 bytes. This size is included as an invisible comment in the HTML output—use your browser's view source feature to show the raw HTML and search for "newpp". The report will look like:
<syntaxhighlight lang="html4strict">
<!--
NewPP limit report
Preprocessor node count: 2382/1000000
Post-expand include size: 63476/2048000 bytes
Template argument size: 9517/2048000 bytes
Expensive parser function count: 2/500
-->
</syntaxhighlight>
The example shows that template expansion is using 63,476 bytes out of 2,048,000 bytes of available memory.
'''Display problem.''' If too many templates are included on a page, the post-expand include size may exceed the limit. When this happens, templates after the limit will no longer expand and will instead display as a wikilink (for example, {{red|1=<u>Template:{{var|Template name}}</u>}}). Common causes are the inclusion of too many citation templates and/or flag templates. To resolve this problem [[Wikipedia:Substitution|substitute]] templates, remove templates, or [[Wikipedia:Splitting|split]] the page.
'''Non-rendered tranclusions still count towards limit.''' For example, a page which contains only <code><nowiki>{{#if:{{:Main Page}}}}</nowiki></code> would still have a post-expand include size even though it would have no output at all.
The same applies to [[Wikipedia:Lua|Scribunto modules]]. For example, <code><nowiki>{{#invoke:Test|main}}</nowiki></code> would still increase post-expand include size even if Module:Test were simply:
<syntaxhighlight lang="lua">
mw.getCurrentFrame():preprocess'{{msgnw::Main Page}}'-- remove this line and post-expand include size becomes zero
return {main = function()end}-- p.main() has no return value
</syntaxhighlight>
==Lua programming language==
{{main article|Wikipedia:Lua}}
Since February 2013, the [[Lua (programming language)|Lua programming language]] is available for use through the [[mw:Extension:Scribunto|Scribunto]] MediaWiki extension. Lua code can be embedded into templates by employing the <code><nowiki>{{#invoke:}}</nowiki></code> functionality of the Scribunto MediaWiki extension. The Lua [[source code]] is stored in pages called modules, and these individual modules are then invoked on template pages. For example, [[Module:Bananas]] can be invoked using the code <code><nowiki>{{#invoke:Bananas|hello}}</nowiki></code> to print the text "Hello, world!".
==Template search==
{{main|Wikipedia:Template messages}}
As an alternative to using this index linked above, you can search the Template namespace using the [[Special:Search]] box below:
<div class="inputbox-hidecheckboxes">
<inputbox>
type=fulltext
width=25
namespaces=Template**
searchbuttonlabel=Search Template namespace
break=no
placeholder=i.e. Citation
</inputbox>
</div>
==See also==
{| class="wikitable"
!width=25%|Help pages
!width=25%|Manual pages
!width=25%|Special pages
!width=25%|Other pages not for direct viewing
|- style="vertical-align:top;"
|
*[[Wikipedia:Advanced template coding]]
* [[m:Help:Template]]
* [[meta:User:Happy-melon/Templates]]
* [[m:Help:Advanced templates]]<!-- m: not directly portable due to examples -->
* Introductions:
** [[Transclusion]]
** [[Help:A quick guide to templates]]
** [[mw:Help:Templates]]
* [[Help:Calculation]]
* [[Wikipedia:Template messages]]
* [[Help:Magic words]]
* [[mw:Help:Magic words]]
* [[mw:Help:Extension:ParserFunctions]]
* [[m:Help:Parameter default]]<!-- m: -->
* [[Help:Substitution]]<!-- maybe portable, test it -->
* [[Wikipedia:Template documentation]]
* [[Wikipedia:Template namespace]]
* [[Wikipedia:Template limits]]
* [[Wikipedia:Template test cases]]
* [[Wikipedia:WikiProject Templates]]
|
* [[mw:Manual:$wgEnableScaryTranscluding|Interwiki transclusion]]
* [[mw:Extension:ExpandTemplates]]
* [[mw:Manual:Parser.php]]
|
* [[Wikipedia:Database reports/Templates transcluded on the most pages]]—shows the number of pages in which each template is transcluded, not counting transclusions through redirects
* [[Special:Unusedtemplates]]—templates which are not transcluded (however, they may be used for substitution)
* [{{fullurl:Special:Allpages|namespace=10}} Special:Allpages (namespace 10)] all templates on this server
* [[Special:ExpandTemplates]]
|
* Multilingual portal pages, see [[meta:www]]
* CSS and JS pages, see [[Help:User style]]
* Pages in the MediaWiki namespace, see [[Help:System message]]
* [[Wikipedia:Book sources]], used by [[Special:Booksources]]
* [[m:Spam blacklist]]
|}
==Notes==
{{Notelist}}
{{Wikipedia template messages|state=collapsed}}
{{Wikipedia technical help|collapsed}}
[[Category:Template documentation| ]]
[[Category:Wikipedia how-to]]
[[Category:Wikipedia template help]]