Wikipedia:Anatomy of a template
You don't need to be Editor-In-Chief to add or edit content to WikiDoc. You can begin to add to or edit text on this WikiDoc page by clicking on the edit button at the top of this page. Next enter or edit the information that you would like to appear here. Once you are done editing, scroll down and click the Save page button at the bottom of the page.
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 informational messages. Templates are created in the Template: namespace, which means they are created and accessed via http://en.wikipedia.org/wiki/Template:SomeTemplateHere. This way, they do not conflict with the articles, images, and other elements on Wikipedia. Inside each template, parameters are allowed, which allow the templates to be tailored to specific needs of different articles and pages. Templates are then added to articles, talk pages, and wherever else they are needed using pairs of curly brackets around the name – {{SomeTemplateHere}} – with parameters optionally added – {{SomeTemplateHere|Parameter1|Parameter2}} – or named parameters optionally added – {{SomeTemplateHere|a=Parameter1|b=Parameter2}}.
Let's take a look at a popular template: {{current}}. The code for {{current}} is located 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==
*[[Template:Current-related]]
*[[Template:Current-section]]
[[Category:Temporal templates|{{PAGENAME}}]]
[[ar:قالب:حدث جاري]]
[[de:Vorlage:Neuigkeiten]]
[[it:Template:Incorso]]
[[no:mal:Pågående]]
[[pl:Szablon:Aktualne]]
[[vi:Tiêu bản:Đang diễn ra]]
[[eu:Txantiloi:Eguneratu]]
</noinclude>
Contents |
Parameters
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...
| This article documents a current event. Information may change rapidly as the event progresses. |
...while {{current|cha-cha}} produces...
| This cha-cha documents a current event. Information may change rapidly as the event progresses. |
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.
- See also: m:Help:Parameter default
Conditionals
Take 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 namespace – Wikipedia:, 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 c |
| {{{a|{{b|c=d}}}}} | parameter named a | template entitled b, with parameter d named c |
| {{{a|{{{b}}}}}} | parameter named a | parameter named b |
| {{{a|{{{b|c}}}}}} | parameter named a | embedded conditional similar to prescribed {{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.
- See also: m:ParserFunctions
<noinclude>
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), interlanguage links (e.g. ar:قالب:حدث جاري), 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>
<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 equasion, et cetera...
See also
Extensive tutorials
- Help:Template – a more extensive page on creating templates
- m:Help:Advanced templates – a very, very extensive page on creating templates
Other pages
- Help:Calculation – assistance with calculations
- m:Help:Parameter default – some help with parameters
- m:ParserFunctions – more information on parser and conditional functions
- Wikipedia:Template list of Wikipedia templates
- Help:Variable – a look at variables

