GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing en.wikipedia.org — Help:Template - Wikipedia "H:T" and "WP:TEMPLATE" redirect here. For help regarding tables, see Help:Table. For content guidelines, see Wikipedia:Template namespace.
For lists of commonly used templates, see Wikipedia:Template index. For a quick introduction to templates, see Help:A quick guide to templates.
A template on Wikipedia is a page designed for inclusion in other pages. Templates typically contain standardised or repetitive material that needs to appear across multiple articles or pages. They are commonly used for boilerplate messages, standard warnings or notices, infoboxes, navigation boxes, and similar recurring elements. Templates can take parameters, allowing the same template page to produce different text or formatting depending on how it is called. The most common inclusion method is transclusion, where the wikitext of a target page references a template using the For an introduction, see Help:A quick guide to templates. More detailed guidance is available in the MediaWiki documentation at mw:Help:Templates and mw:Manual:Advanced templates. General descriptionMost templates are in the template namespace, which means that they have titles in the form "Template:XXXX". It is possible, however, to transclude and substitute from any namespace,[a] 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. There is some programming ability: customizable values (via 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 markup to define certain parts of the page to be included while other parts are not. This means that when you display the template page itself, you may see more than when you display a page that includes it (for example, it can contain documentation, categories, etc. for the template). To call a template (cause it to be transcluded or substituted in the page), a page contains wikitext in double braces. For example, the wikitext Note that while templates are the usual way transclusion and substitution happen on Wikipedia, they are not the only way. Using templatesGeneralUsing a template is similar to calling a function in programming. When called, it returns a value. For a template, calling is known as a template call and the return value is known as the expansion of the template, which is treated as part of the calling page's source. Like functions, some templates can take parameters that affect their output. Calling a template causes it to be either transcluded or substituted in the page that calls it (the target page). Transcluding a template means that when MediaWiki displays the page, it treats the template as if it were directly in the page’s source. Substituting a template differs in that it inserts the content of the template permanently into the target page when it is saved and subsequent editors cannot tell that it originated from a template. When you update a template, every page that transcludes it automatically reflects the update when the page is displayed in the future, whereas updating a template has no effect on pages that have previously been saved with the template substituted. To transclude a template, insert If the template only includes TemplateStyles (no visible content), although it could be called once anywhere on the page for all uses, it should be called near each usage. In the case of table styles, call it near and above each table that uses it. This avoids issues like missing when previewed/copied or flashing. In MediaWiki, the wiki software that Wikipedia uses, variables are distinct from templates, though both are referenced by double curly braces The number sign The template namespace is the default, so you can leave out the namespace
Parameters"WP:PARAMETER" redirects here. For search parameters, see Help:Searching § Parameters.
Further information: Help:Transclusion § Template parameters The basic transclusion syntax for a template can be further controlled using parameters, which allow you to customize a template’s output. The syntax is Each template has specific parameters it can accept, as defined within its code. You can list named parameters in any order in a template call. Extra or misnamed parameters have no effect, while missing parameters cause a default value to be used. If a parameter is repeated, the last defined parameter value is used. The value of a parameter can be an empty string, such as when the pipe ( If a template call specifies a parameter which is not defined in the template, it has no effect. Certain templates, especially complex ones like infoboxes, may use the check for unknown parameters module to alert editors about any unrecognized parameters they code by mistake. An editor who wants to add a comment relating to the template call can use the hidden text syntax: Whitespace handlingLeading and trailing whitespace (including line breaks) around named parameters and values is collapsed automatically, but spaces in the middle of values are kept. For instance, The collapsing of line breaks around parameters can be used to improve the readability of a template call with many parameters by placing each parameter specification in its own line. Variable length parameter listsIn MediaWiki, templates cannot automatically handle an unknown or unlimited number of parameters. Each possible parameter usually has to be predefined in the template code. For example, a template might be set up to use three specific parameters, such as However, there are some ways to work around this:
There are several techniques for allowing a template to accept an unlimited number of parameters; see Help:Infinite parameters for an overview. Templates that accept an open number of parameters are often collected under Category:Variadic templates. Examples
An example of a very simple template can be found at Template:TM, which expands to wikitext that places the trademark symbol (™), linked to the trademark article, at that point in the displayed page. A programmer would say that the template "returns" the trademark symbol link wikitext. Display Template:TM (by clicking on the foregoing link), then click on the "Edit" tab to see the template code (its page source). The active part of that code, which becomes the expansion of the template when it is transcluded, is the single link wikitext To transclude Template:TM onto another page (i.e., to use it on another page), type For example, type The other way to use a template is to substitute it. If you type Examples with parametersAn example of a template that takes parameters is the template This page is about how to use templates. For how to use modules, see Help:Lua.
The template For how to use modules, see Help:Lua.
A simple example of a template that has named parameters is Template:Payoff matrix, used to generate a 2-by-2 grid. For example:
See the template page for more possibilities. Notice that the template is called here without specifying all its possible parameters, so undefined parameters are given default values. Hints and workaroundsThe following points may be worth noting when using templates:
Writing templatesProcessTemplates are just Wikipedia pages. They are created, deleted, and edited in much the same way as any other page. CreatingTo create a template, choose an appropriate name, navigate to that page, then click the "Edit" tab or create a new page as needed. Templates are normally placed in the template namespace, though you can place a template intended for your own personal use or for experimentation in your own user space. 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, you can create a navbox template easily by creating a brief template that calls the generic Template:Navbox. There is no hard rule about what name to choose for a template—make it short but reasonably descriptive. The naming guideline says: "Template function should be clear from the template name". If similar templates exist, try to follow a consistent naming pattern. You can rename a template without breaking existing transclusions (what is called breakage) by leaving a redirect to the new template name. ModifyingEdit a template the same way as any other page: navigate to the template and click the "Edit" tab. 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 and template editors; other editors can propose changes on the talk page. Some templates offer a sandbox and test cases for experimentation. DeletingUnused or inappropriate templates should be deleted. Templates that can be easily merged into another should be merged. To propose the deletion or merger of a template, go to Templates for discussion (TfD). Coding a templateAnything 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. MetatemplatesVarious metatemplates and metamodules exist to help accomplish common template tasks. They are called like normal templates and modules, but they serve a purpose that makes writing templates easier. See Category:Wikipedia metatemplates and Category:Template metamodules for a list of those templates and modules. Handling parametersIn template code, the value of a parameter is represented by items enclosed between triple braces, which is a parameter reference.
If a parameter is not specified in the template call, then the parameter reference is not replaced with anything -- it is expanded literally; this means that if the template call does not specify the parameter "xxx", the wikitext If a call sets a parameter to the empty string like You can use default parameter values to effect a parameter alias:
For example, if parameters "text" and "message" are names for the same parameter, which can also be specified as the only unnamed parameter, then refer to the parameter with Because of the multiple meanings of double-brace and triple-brace syntax in wikitext, expressions can sometimes be ambiguous. It may be helpful or necessary to include spaces to resolve such ambiguity. For example, Parameter references do not get expanded when they are wrapped in ExampleThe parameter usage example above refers to the {{payoff matrix}} template. Here is the code in the template that implements those parameters: {| 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}}}}}'' |} The entity Special case: parameters within an XML-style opening tagParameter references aren't expanded inside XML-style opening tags. Thus, the following will not work within a template:
because the parameter is not expanded. Instead, you can use the
Caution: overextending URLsIf 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.
This could happen because the source code does not have a space after the URL or it contains or generates a space that is discarded in the processing. Ensure that in the template expansion 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. The System variables and conditional logicTemplate code often makes use of the variables and parser functions described at Help:Magic words to make the template's behavior depend on the environment in which it is included (such as the current time or namespace). Parser functions can be used for some arithmetic calculations and string manipulations on variables and parameter values, but certain standard programming features such as loops and variable assignment are not available. Full string manipulation is not available; some templates providing such function 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.
The ParserFunctions extension provides more programming-oriented parser functions:
The Nesting templatesA template may call another template—this is called nesting and the called template is called, in this context, a subtemplate. When WikiMedia expands the template, it expands subtemplates as the calls to them appear, so that the final product is essentially the result of expanding templates from the most deeply nested out. While fairly straightforward in application, it involves some noteworthy quirks and tricks. To pass a parameter value from a template call to a subtemplate, use a parameter reference in the template call to the subtemplate.
A template can even choose which subtemplate parameter to pass conditionally.
Template recursion is not available; that is, a template may not call itself directly, or indirectly by calling other templates which call it. Attempts to do so will result in an error message describing a "template loop". When a subtemplate contains unmatched braces—as in See mw:Manual:Advanced templates and mw:Manual:Recursive conversion of wikitext for more information. Inclusion control: noinclude, includeonly, and onlyincludeBy default, when a page calls a template, MediaWiki includes the expansion of the entire template in the calling page. However, it is possible to modify that behavior, using tags that specify which parts of the template code are to be included. This makes it possible for the template to contain information only for display when the template page is viewed, such as the template's documentation, or categories. It is also possible to have parts of the template be included when it's transcluded, but neither be not displayed when the template page itself is viewed nor be processed when the template page itself is saved (e.g., categories which do not apply to the template but need to be applied to pages which call or transclude the template). The tags are as follows:
Perhaps the most common issue with the use of these tags is unwanted whitespace or linebreaks. It is important to remember that the effect of these tags begins immediately before the first opening angle bracket (<), not on the previous line or at the previous visible character; similarly the effect ends immediately after the last closing angle bracket (>), not on the next line or with the next visible character. For example:
These tags can be nested inside each other, though (for a given page) this is significant only for the Problems and workarounds
DocumentationFurther information: WP:Template documentation
Documentation for users, together with the template's categories, normally goes after the template code, inside In the case of complex templates, the documentation is often kept on a separate subpage of the template page (named "Template:XXX/doc"). This applies especially to many protected templates, so that non-administrators can edit the documentation. To do this, place a call to the The documentation subpage, rather than the template itself, is normally what is placed in categories to represent a template. CategorizationSee also: Wikipedia:Categorization § Templates
Categorize pages by template inclusionSome templates generate category declarations in their expansion, since the template is intended to place calling pages in particular categories. This is often done with maintenance categories. Placing articles into ordinary content categories in this way is discouraged. When doing this, you may have to use Categorize templatesCategorizing your template and documenting its proper usage will make it easier for other editors to find and use. Category declarations for a template itself should be placed on the template's documentation subpage (or inside AliasesA redirect of a template functions as an alias. For example, Template:Tsh redirects to Template:Template shortcut, so you can code It is good to prepare template aliases for variations in whitespace and capitalization. For example, there is a template called Template limitsFurther information: Help: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,097,152 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: <!-- NewPP limit report Preprocessor node count: 2382/1000000 Post-expand include size: 63476/2097152 bytes Template argument size: 9517/2097152 bytes Expensive parser function count: 2/500 --> The example shows that template expansion is using 63,476 bytes out of 2,097,152 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:Example templates). Common causes are the inclusion of too many citation templates, navbox templates, and/or flag templates. To resolve this problem substitute templates, directly invoke modules, remove templates, or split the page. Non-rendered tranclusions still count towards limit. For example, a page which contains only The same applies to Scribunto modules. For example, 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 Lua programming languageMain page: WP:Lua
The Lua programming language is available for use through the Scribunto MediaWiki extension. You can embed Lua code into templates by employing the TerminologyA template is a Wikipedia page whose purpose is to be transcluded or substituted in another page. Templates are usually in the template namespace, but don't have to be. Pages can transclude or substitute any page, not just templates; what makes a page a template is the page's purpose. Calling a template means transcluding or substituting one. A page calls a template. The wikitext on a page that causes the page to call a template is called a template call. For example, Expansion is the process of generating page source from a template, when a page calls it, by applying parameters and other features. MediaWiki expands a template as it renders a page. One can also say that the template itself expands, so both "MediaWiki expands Expansion of a page is also the page source that results from template expansion. For example, the expansion of the {{sic}} template when called by The displayed page content generated by a template call (which is the rendering of the expansion of the called template) is the template result. The template result generated by the template call The name of a template is the name of the Wikipedia page (which is also the title of the page). The namespace is normally left off if it is "Template" (which it almost always is). Furthermore, it is customary to write the name with double braces, like a template call, to emphasize that it names a template. For example, "Use the {{tm}} template to generate the trademark symbol." But don't go so far as to put a template name in the code font, to prevent confusion with an actual template call. {{tm}} is a template name, but Some template calls perform a tagging function; such a template call is often called a tag, as in, "If you are in the middle of a major edit, place an {{in use}} tag at the top of the page." This is one of many ways the term "tag" is used in Wikipedia. A parameter is a particular piece of information that can be specified in a template call to affect the expansion of the template. A parameter has a value. A named parameter has a name; an unnamed parameter has a position. A template call specifies a parameter and the code that does that is a parameter specification. A template defines a parameter (not explicitly -- just by the template being designed to use it). The code in a template that expands to the parameter value (e.g. Common variationsIt is common to use "template" to refer not only to a template, but to a template call, a template result, and a template name. For example:
These usages can be confusing, especially to someone less familiar with templates. Consider using more specific language. Template searchMain page: WP:Template index
As an alternative to using this index linked above, you can search the Template namespace using the Special:Search box below: See alsoHelp pages
Mediawiki manual pagesSpecial pages
Other backend pages
Notes
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||