Jump to content

Wikipedia:Anatomy of a template

From Wikipedia, the free encyclopedia

Templates on Wikipedia can be used for a variety of purposes. Some common uses for templates include infoboxes to summarize the information presented in an article, while others are information messages.

Templates are created in the Template: namespace, which means that a fictitious template called Shelf would be located on page http://en.wikipedia.org/wiki/Template:Shelf. In this way, template names do not conflict with the names of articles, images, and other elements on Wikipedia.

Template names are added to pages inside double curly brackets. In addition, user-typed parameters are allowed, so that the template has some input to work with. The parameters allow the templates to be tailored to the specific needs of different articles and pages. When the page is previewed or saved, the result or output of the template will be found in place of the template code.

A typical page entry for a template with two parameters takes the form:


or using named parameters:


For the discussion below we will make use of a popular template: {{current}}. The former code for {{current}} is shown in the block below:

{| class="messagebox current" style="width: auto;"
| [[Image:Current event marker.svg|50px| ]]
| '''This {{{1|article}}} documents a [[Portal:Current events|current event]].''' <br /> 
<small>Information may change rapidly as the event progresses.</small>
|}{{#if:{{NAMESPACE}}|<!-- don't categorize -->|[[Category:Current events]]}}<noinclude>
==See also==

[[Category:Temporal templates|{{PAGENAME}}]]


As you can see, one can put almost anything in a template – text, images, tables, colors, ... you name it. One will need to be familiar with other wiki markup if one wants to make templates that are especially complex. Nevertheless, the beginning of the third line – {{{1|article}}} is what we will focus on first. This is how a parameter is included. It essentially says that if there is an unnamed parameter added to the inclusion of the {{current}} template, it will appear in that location. Otherwise, article will appear there instead. {{current}} produces...

...while {{current|cha-cha}} produces...

Of course, the writers of {{current}} probably intended section or something to that effect to be used as the parameter. Note how parameters have three curly brackets on each side of them; those items with two brackets are templates and variables embedded into the {{current}} template.


Take a look at the fifth line in {{current}}. You'll see something that says {{#if:{{NAMESPACE}}|<!-- don't categorize -->|[[Category:Current events]]}}. This is a conditional statement and the #if function assists with the construction, which involves three parameters – a test statement, a result when the test statement is true, and a result when the test statement is false. In this case, if the location of the page is preceded by a namespaceWikipedia:, Talk:, User:, etc. – the test statement will return true and produce what immediately follows the first pipe character (|), which in the example here is a comment (the text between the <!-- and --> become hidden from view by web page viewers). If no namespace precedes the page, the test statement returns false and produces what immediately follows the second pipe character – a category, [[Category:Current events]].

Another example of a conditional in {{current}} is in line three, with {{{1|article}}}. The pipe character (|) indicates that if the parameter name or number indicated on the left side does not exist, the text on the right will appear instead. Note also another example, {{{1|{{article}}}}} (which does not exist in {{current}}), which returns a template by the name of article if the first item does not exist. Furthermore, note that {{{1|{{{article}}}}}} would produce parameter article if false. So, in summary...

Conditional Result, if True Result, if False
{{{a|b}}} parameter named a text b
{{{a|{{b}}}}} parameter named a template entitled b
{{{a|{{b|c}}}}} parameter named a template entitled b, with unnamed parameter having a value of c
{{{a|{{b|c=d}}}}} parameter named a template entitled b, with parameter named c having a value of d
{{{a|{{{b}}}}}} parameter named a parameter named b
{{{a|{{{b|c}}}}}} parameter named a embedded conditional similar to previously described {{{a|b}}}

As you can see by the last example, conditionals can even be embedded within other conditionals. Note also that #ifexpr: is needed for mathematical expressions.


Everything in a template will be transcluded when added to a page, unless the content is between <noinclude> and </noinclude> tags. Thus, see also links (e.g. Template:Current-related and Template:Current-section), and categories that belong to the template itself rather than the pages on which the template will placed (e.g. [[Category:Temporal templates|{{PAGENAME}}]]) belong between the <noinclude> tags.


<includeonly> is sort-of the inverse of <noinclude>. Everything enclosed in a <includeonly> tag will not be displayed in the template page, but will be transcluded into another page. This has several uses:

  • A page containing the template needs to be in a category the template itself does not belong in.
  • A word, description or some other chunk is meaningless on the template page that holds meaning on a page using the template, such as a table format, part of an equation, et cetera...

See also[edit]

Extensive tutorials[edit]

Other pages[edit]