Page semi-protected


From Wikipedia, the free encyclopedia
Jump to navigation Jump to search



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

A basic overview of how templates work (8-minute video)


Templates can contain any desired wikitext, including calls to other templates. They have some limited programming capacities: customizable values (called parameters); calculation and branchings (using parser functions); and access to wiki-specific variables (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).

How to do it: To transclude a template into an article or page, type {{Template name}} in the wikitext at the place where the template is to appear. The first letter may be indifferently lower- or upper-case.


 Note: Attempting to transclude a template that does not exist produces a 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


The basic transclusion syntax given above can be extended by the addition of parameters, which are used to control the template's output. The syntax for this is {{Template name|parameter|parameter|...}} where Template name is the name of the template, and each parameter may either contain just a value (these are called unnamed parameters) or be of the form name=value (named parameters). The first, second, third, etc. unnamed parameters will be given the names 1, 2, 3, etc.



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, |reason= is frequently used as a pseudo-parameter to explain briefly in the wikisource why the template was placed.[b] 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 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 a template is not unlike calling a variable or a function in a programming language: call it with a reference and it returns a value (the output). Like functions, some templates accept parameters that change the output.

In MediaWiki, the wiki software that Wikipedia uses, variables have a more specific meaning that distinguishes them from templates, but they are both identified by double braces {{ }} and they both return a value.

Whereas MediaWiki variable names are all uppercase, template names have the same basic features and limitations as all page names: they are case-sensitive (except for the first character); underscores are parsed as spaces; and they cannot contain any of these characters: # < > [ ] | { }. This is because those are reserved for wiki markup and HTML.




Basic template usage examples

 Note: If you wish to experiment with any of these, please use either the template sandbox, or your user page or sandbox.


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 expansion of the template, is the single word &lambda;.[c] The remainder of the wikitext is enclosed between <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 {{lambda}} (or {{Lambda}}—the case of the first letter is not significant) into the wikitext of the target page and press Show preview. The page will be displayed with the template call replaced by the expansion of the template, as if the wikitext actually contained &lambda; at that point. The displayed page will therefore contain the text "λ".

For example, type The 11th letter of the Greek alphabet is the lambda ({{lambda}}) and you will see "The 11th letter of the Greek alphabet is the 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 substitute it. If you type The 11th letter of the Greek alphabet is the lambda ({{subst:lambda}}) 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,[d] 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).

Examples with parameters


The template {{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 the template's documentation. For example, {{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:


Markup Renders as
Left Right
Up 5 7
Down 2 9
Example usage




|style="width:33%;                                                     "| 

The entity {{{2L|Left}}} instructs the template to use the named parameter 2L or the text Left 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.[e] To pass an equals sign in an unnamed parameter (for example in a URL with attribute–value pairs), replace the equals sign with the special template {{=}}, 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 |1= and so on. To call template {{done}} with a=b as the literal value for the first parameter, type either {{done|a{{=}}b}} or {{done|1=a=b}}.
  • Similarly, it is not possible to use an ordinary pipe character | in template parameters, as it will be interpreted as a separator.[f] This time, the problem can be solved by using the magic word {{!}} in place of the pipe, or—if the pipe is not intended to be parsed at a higher level—using the HTML entity &#124;.
  • 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 {{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 {{tl}} template (the "template link" template). For example, {{tl|Example}} produces {{Example}}. There are multiple other 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 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.
  • To list all pages onto which a template is transcluded, use What links here on the template page. This will 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,[g] or your newly edited version prior to saving, click 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" section for help in resolving this.
  • If you want the template to leave a time stamp or signature, you can write <noinclude><nowiki></noinclude>~~~~~<noinclude></nowiki></noinclude>, but this will only work if you substitute the template. If you transclude it, you'll just get ~~~~~.
  • 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 <!-- before each newline character and --> after it, which produces an HTML comment.

Creating and editing templates


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, navbox templates can be easily created by calling the generic Template:Navbox.




Handling parameters


  • The codes {{{1}}}, {{{2}}}, and so on will be replaced by the first, second, and so on unnamed parameter (or the value of a parameter named 1, 2, etc.); these are sometimes known as positional parameters.

If a parameter is not assigned a value, then no replacement will take place; this means that if no value is passed for parameter |xxx=, the value of the expression {{{xxx}}} inside the template will literally be {{{xxx}}}, 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: {{{xxx|dflt}}} specifies the default value dflt for the named parameter |xxx=, and {{{1|dflt}}} specifies the default value dflt for the first unnamed parameter. Most often, this is used to specify null default values, such as {{{1|}}} or {{{xxx|}}}.

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.

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, {{ {{{xxx}}} }} or {{{ {{xxx}} }}}, 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 <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:


because the parameter is not expanded. Instead, you can use the {{#tag:}} parser function, which is—for example—used in {{sfn}} to generate the <ref>...</ref> element; see also Help:Magic words § Formatting. Therefore, the following example will work:


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 (not a 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 {{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.

Examples of core parser functions
Description Text entered Result
Getting a namespace name {{NS: 1 }} Talk

The ParserFunctions extension provides more programming-oriented parser functions.

Examples of extension parser functions
Description Text entered Result
Making a calculation (mathematics)
Testing the result of a calculation
[is 1230 even or odd?]
Examples of system variables
Description Text entered Result (for this help page)
Page names {{PAGENAME}} Template
{{FULLPAGENAME}} Help:Template
Name of the current namespace {{NAMESPACE}} Help
Number of registered users {{NUMBEROFUSERS}} 35,159,401
Current software version {{CURRENTVERSION}} 1.33.0-wmf.6 (e7d6fb2)
Timestamp of last revision {{REVISIONTIMESTAMP}} 20181209221529

The {{PAGENAME}} and {{NAMESPACE}} 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 {{NAMESPACE}} 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




Template parameters themselves can be chosen conditionally.

Template:A contains the quick brown {{B|{{{3}}}=fox}} jumps over... Template:A passes the word "fox" as a named parameter of Template:B whose name is A's third positional parameter.




Noinclude, includeonly, and 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, categories, and interwiki links. It is also possible to have parts of the code be transcluded, but 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:

  • <noinclude>...</noinclude> – The text between the tags will not be included when the template is transcluded (substituted), but will be processed on the template's page; a common use is for template documentation.
  • <onlyinclude>...</onlyinclude> – This specifies that nothing on the page except what appears between the tags will be transcluded (substituted).
  • <includeonly>...</includeonly> – The text between the tags will be transcluded (substituted), but will not be processed on the template's own page.
<noinclude> text1 </noinclude> text2 text1 text2 text2
<onlyinclude> text1 </onlyinclude> text2 text1 text2 text1
<includeonly> text1 </includeonly> text2 text2 text1 text2



These tags can be nested inside each other, though (for a given page) this really only applies to the <onlyinclude> tag; nesting <includeonly> and <noinclude> tags is fairly pointless. Be careful to properly nest the tags, however. Constructions like <onlyinclude>abc<includeonly>def</onlyinclude>ghi</includeonly> will 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 parser function is one of four wiki markup characters—:, ;, *, #[h]—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 <nowiki /> before the markup or use the HTML entities &#58;, &#59;, &#42;, and &#35; respectively. In some cases, the HTML entities will work when the <nowiki /> 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 {{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 subst: 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 msgnw: (short for "message, nowiki") 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 NewPP limit report comments.
  • Do not use = wikimarkup to create section headers within a template which is intended for use in article space; this will create an [edit] link that, when transcluded, will confusingly open the template for editing.




In the case of complex templates, the documentation (together with categories) is often kept on a separate subpage of the template page (named "Template:XXX/doc"). This also applies to many protected templates, which allows the information to be edited by non-administrators. This is achieved by placing the {{Documentation}} template after the main template code and within <noinclude>...</noinclude> tags. If the "/doc" subpage does not exist, a link will then appear enabling it to be created.


Categorize pages by template inclusion


Categorize templates

Categorization declaration [[Category:Some-topic templates]] should be placed on the template's documentation page (or inside <noinclude>...</noinclude> tags if there is no documentation page) to avoid polluting the transcluding pages.


Aliases can be created with redirects. For example, Template:Tsh redirects to Template:Template shortcut. You can then write {{tsh|foo}} instead of {{Template shortcut|foo}}.

It is good to prepare template aliases which only differ in whitespaces and capitalization. For example, there is a template called {{See Wiktionary}}. The "W" is capital, since the word "Wiktionary" is so, but a redirect {{See wiktionary}} with lower "w" exists because users may type the latter instead.

Template limits


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

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, Template:Template name). Common causes are the inclusion of too many citation templates and/or flag templates. To resolve this problem substitute templates, remove templates, or split the page.




Lua programming language

Since February 2013, the Lua programming language is available for use through the Scribunto MediaWiki extension. Lua code can be embedded into templates by employing the {{#invoke:}} 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 {{#invoke:Bananas|hello}} to print the text "Hello, world!".

Template search


See also

Help pages Manual pages Special pages Other pages not for direct viewing


  2. ^ Some templates, such as {{Requested move}}, have code to display |reason= as visible output; whether to do so is determined on a template-by-template basis.
  3. ^ &lambda; is the HTML character entity for the Greek letter (λ).
  4. ^ 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 in the wikitext.
  5. ^ This does not apply if the equals sign comes within another template call or other item which the parser handles separately.
  6. ^ Again, this does not apply if it comes within another separately parsed item, such as a piped wikilink.
  7. ^ For an old version, the subtemplate tree will be constructed according to the templates' current state.
  8. ^ These are defined in the doBlockLevels function of Parser.php.