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 boilerplate messages, standard warnings or notices, infoboxes, navigational boxes and similar purposes. The most common method of inclusion is called transclusion, where the wikitext of the target page contains a reference to the template, using the {{Template name}}
syntax. Another method is substitution, where the content of the template is copied into the wikitext of the target page, just once, when it is saved. For a brief introduction to the subject, see A quick guide to templates.
Most templates are pages in the Template namespace, which means that they have titles in the form "Template:XXX". It is possible, however, to transclude and substitute from any namespace, and so some template pages are placed in other namespaces, such as the User namespace. Template pages have associated talk pages. 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 to an article or page, type {{template name}}
in the wikitext at the place where the template is to appear. The prefix "Template:" need not be included, and an initial capital is not necessary. Templates stored in other namespaces are transcluded in the same way, except that the namespace prefix, such as "User:", must be included. To transclude a page in mainspace, precede its title with a colon, as, for example, {{:Page name}}
. 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) – to use a template on another language project, a copy of the template must be created in that project.
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 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, etc. unnamed parameters are equivalent to parameters named "1", "2", etc. Whitespace characters (spaces, tabs, returns) are stripped from the beginnings and ends of named parameter names and values, but not from the middle: thus {{ ... | myparam = this is a test }}
has the same effect as {{ ... |myparam=this is a test}}
. This does not apply to unnamed parameters, where the whitespace characters are preserved. What 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 (pipe or equals sign followed immediately by the next pipe or the closing braces). This is different from leaving the parameter undefined (although templates are often coded so as to behave the same in both cases).
If a template is to be substituted in the wikitext rather than transcluded, add the modifier subst:
after the initial pair of braces, as in {{subst:afd}}
. Parameters can be added as normally.
Template names are exactly like other page names: case-sensitive except for the first letter, with spaces indistinguishable from underscores. If the symbol # (normally used to link to a section of a page) appears in a transclusion, then it and any characters that follow it are ignored. If the a redirect is transcluded, the redirect's target will be transcluded instead. See #Aliases for more. Notice that the same double-brace syntax is used for many MediaWiki variables and parser functions (see Help:Magic words). For example, the code {{NAMESPACE}}
may look like a template call, but it is actually a variable whose value is the namespace prefix of the current page.
(If you wish to experiment with any of these, please use the template sandbox.) An example of a very simple template can be found at Template:Lambda, which places the lambda symbol, λ. Click the "Edit" tab on that template page to see the template code (its wikitext). The "active" part of that code, called the expansion of the template, is λ
. (The remainder of the wikitext is enclosed between {{lambda}}
(or {{Lambda}}
– the case of the first letter is not significant) somewhere into the wikitext of the target page, and save it. The page will be displayed as if the template call were replaced by the expansion of the template, i.e. as if the wikitext contained λ
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 (λ)". You can use templates without knowing the details of their code – you only need to remember what result they produce (this 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 save the page, you will again see ""The 11th letter of the Greek alphabet is the lambda (λ)
". However this time, if you look again at the saved wikitext, 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 changes which might be made to the template at some future time (as it would be in the case of transclusion).
An example of a template that takes parameters is Template:For. Type {{for|the card game|Contract bridge}}
in the sandbox to produce the following text:
This usage takes two unnamed parameters, but the same template can also be used with different numbers of parameters to give slightly different results, as explained in the documentation on the template page. For example, {{For||Latins|Latin (disambiguation)}}
produces Error: no context parameter provided. Use {{other uses}} for "other uses" hatnotes. (help). Note in particular the usage with an empty parameter – here the consecutive pipes mean that the first parameter passed to the template is the empty string. 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. Type:
{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}
Left | Right | |
Up | 5 | 7 |
Down | 2 | 9 |
Example usage |
to produce the grid you can see on the right. See the 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). However parameter names are fully case sensitive – for example, it is not possible to replace "DR" by "dr" or "Dr" in the above example. Parameters with names that are not used by the template are simply ignored.
The following points may be worth noting when using templates:
{{=}}
, which returns an equals sign that will not be specially 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 with the name "1", and so on. So to call template {{Done}}
with the parameter "a=b", type either {{done|a{{=}}b}}
or {{done|1=a=b}}
.{{!}}
in place of the pipe, or (if the pipe is not intended to be parsed specially at a higher level) using the HTML entity |.{{StripWhitespace}}
to remove unwanted whitespace in unnamed parameters.){{Tl}}
" template (the template link template). For example, using the text "{{tl|tc}}" produces "{{Tc}}
". There is an extended version, {{Tlx}}
, which also supports parameters. ~~~~~
. But this will only work if you substitute the template. If you transclude it, you'll just get ~~~~~.
after it.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 Template namespace, though templates intended for your own personal use or for experimentation can be created in your own user space. Anything that can be included on a normal page or article can be included on a template, including other templates (called subtemplates). Templates often make use of programming features – parameters, parser functions and other 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 (e.g. by exploring ವರ್ಗ:Wikipedia templates) to see if there's 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). 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 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 protected against editing except by administrators (other editors can propose changes on the talk page). Some templates offer a sandbox and test cases for experimentation. To propose the deletion of unused or inappropriate templates, or other changes in the way particular templates are used, go to Templates for discussion (TfD).
The values of the parameters which can be fed to a template are represented in the template code by items enclosed between triple braces:
{{{xxx}}}
will be replaced by the value of the parameter named xxx{{{1}}}
, {{{2}}}
etc. will be replaced by the first, second etc. unnamed parameter (or the value of a parameter named 1, 2, etc.); these are sometimes known as positional parametersIf a parameter is not assigned a value, then the above replacements will not take place – the form "{{{xxx}}}" will remain as the effective value of the parameter. To change this behaviour, define default values using the pipe syntax. For example, {{{1|default}}}
will be replaced by the first unnamed parameter if there is one, or otherwise by the text "default". Similarly, {{{xxx|}}}
will be replaced by the parameter named xxx if it exists, or otherwise will be left blank. Though if a template is called with the parameter specified as empty (e.g. {{Example|}}
), the default for the parameter will be ignored; if that is undesired one can use {{#if:{{{1|}}}|{{{1}}}|default}}
instead to get the text "default" even if the parameter is specified as empty. Parameters do not get expanded when they are inside nowiki tags or XML-style extension tags. Thus, the following will not work within a template – {{ {{{xxx}}} }}
or {{{ {{xxx}} }}}
, rather than typing five consecutive braces. However, watch out for unwanted whitespace appearing in template expansions.
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 which are passed to it. They can also be used for arithmetical calculations. Notice that full string manipulation is not available (although templates have been created which provide such functionality, though very inefficiently and imperfectly), nor are certain standard programming features such as loops and variable assignment. Some of the most often used variables and functions are listed hereafter. For more, see Help:Magic words, and the fuller documentation at the MediaWiki pages mw:Help:Magic words and mw:Help:Extension:ParserFunctions.
Description | Text entered | Result |
---|---|---|
Uppercasing text | {{uc: Heavens to BETSY! }} | HEAVENS TO BETSY! |
Lowercasing text | {{lc: Heavens to BETSY! }} | heavens to betsy! |
Getting a namespace name | {{NS: 1 }} | ಚರ್ಚೆಪುಟ |
Getting a Wikipedia URL | {{fullurl: pagename }} | //kn.wikipedia.org/wiki/Pagename |
The ParserFunctions extension gives more programming-oriented parser functions.
Description | Text Entered | Result |
---|---|---|
Testing between options | {{#ifeq: yes | yes | Hooray...! | Darn...! }} {{#ifeq: yes | no | Hooray...! | Darn...! }} | Hooray...! Darn...! |
Testing if a parameter is set | {{#if: {{{param|}}} | Hooray...! | Darn...! }} | Darn...! |
Making a calculation (mathematics) [area of circle of radius 4, to 3 decimal places] | {{#expr: ( pi * 4 ^ 2 ) round 3 }} | 50.265 |
Testing the result of a calculation [is 1230 even or odd?] | {{#ifexpr: 1.23E+3 mod 2 | Odd | Even }} | Even |
Description | Text Entered | Result (for this help page) |
---|---|---|
Page names | {{PAGENAME}} {{FULLPAGENAME}} | Template ಸಹಾಯ:Template |
Name of the current namespace | {{NAMESPACE}} | ಸಹಾಯ |
Number of registered users | {{NUMBEROFUSERS}} | ೮೬,೬೧೯ |
Number of pages in a given category | {{PAGESINCATEGORY:"Weird Al" Yankovic albums}} | ೦ |
Current software version | {{CURRENTVERSION}} | 1.43.0-wmf.2 (ce9d259) |
Timestamp of last revision | {{REVISIONTIMESTAMP}} | 20210906144146 |
The PAGENAME and NAMESPACE variables are particularly useful, and frequently used, to change template behavior 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.
Templates may contain other templates – this is usually called "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.
Template parameters themselves can be chosen conditionally.
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 {{Lb}}
} – 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 will change how braces are parsed in the nesting template. This has little practical use, but can occasionally introduce unexpected errors. See the meta:Help:Advanced templates and meta:Help:Recursive conversion of wikitext for more information. These pages also contain information on unusual calls such as {{template {{{parameter|}}} }}.
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
...
– the text between the noinclude tags will not be included when the template is transcluded (substituted), but will be processed on the template's page...
– the text between the includeonly tags will be transcluded (substituted), but will not be processed on the template's own page...
(not often used) – specifies that nothing on the page except what appears between the onlyinclude tags will be transcluded (substituted)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 ends immediately after the last angle bracket, not on the next line or with the next visible character. These tags can be nested inside each other, though (for a given page) this really only applies to the
tag; nesting
and
tags is fairly pointless. Be careful not to split the tags, however. Constructions like
abc
def
ghi
will not work as expected. Use the "first opened, last closed" rule that is standard for XML.
:;*#
, then it is processed as though it is 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
before the markup; see Help:Nowiki, or use the HTML entities : ; * #
respectively.{{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).=
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. [edit]
links to the template by including __NOEDITSECTION__
Categorizing your template and documenting its proper usage will make it easier for other editors to find and use. For detailed advice, see Wiki: Template documentation. Documentation for users, together with the template's categories and interwiki links, is normally placed after the template code, inside "noinclude" tags. It is normally necessary to put the opening "noinclude" 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 and wikilinks) is often kept on a separate subpage of the template page (named "Template:XXX/doc"). This also applies to many protected templates (to allow the information to be edited by non-administrators). This is achieved by placing the {{Documentation}}
template after the main template code (within "noinclude" tags). If the "/doc" subpage does not exist, a link will then appear enabling it to be created. Some templates contain category definitions in their transcluded code, i.e. 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 "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 X1, X2, or X3) or suppress categorization (see category suppression in templates).
Aliases can be created with redirects. For example, Template:Tsh redirects to Template:Template shortcut. Then you can write {{tsh|foo}} instead of {{Template shortcut|foo}}. It's good to prepare template aliases which only differ in whitespaces and capitalization. For example there's a template {{See Wiktionary}}
. The "W" is capital, since the word "Wiktionary" is so. But a redirect {{See wiktionary}}
with lower "w" exists because users are likely to use the latter.
"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:
The example shows that template expansion is using 63k out of 2M 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:templatename). 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.
Since February 2013 Lua programming language is available for use via the Scribunto MediaWiki extension. Lua code can be embedded into wiki 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!".
Help pages | Manual pages | Special pages | Other pages not for direct viewing |
---|---|---|---|
|
|
|
This article uses material from the Wikipedia ಕನ್ನಡ article ಸಹಾಯ:Template, which is released under the Creative Commons Attribution-ShareAlike 3.0 license ("CC BY-SA 3.0"); additional terms may apply (view authors). ವಿಶೇಷವಾಗಿ ಟಿಪ್ಪಣಿ ಮಾಡದಿದ್ದ ಹೊರತು ಪಠ್ಯ "CC BY-SA 4.0" ರಡಿ ಲಭ್ಯವಿದೆ. Images, videos and audio are available under their respective licenses.
®Wikipedia is a registered trademark of the Wiki Foundation, Inc. Wiki ಕನ್ನಡ (DUHOCTRUNGQUOC.VN) is an independent company and has no affiliation with Wiki Foundation.