Wikipedia:Anatomy of a template

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 –   – with parameters optionally added –    – or named parameters optionally added –   .

Let's take a look at a popular template:. The code for is located below:

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 –   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 template, it will appear in that location. Otherwise, article will appear there instead.   produces...

...while   produces...

Of course, the writers of 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 template.

Conditionals
Take at the fifth line in. You'll see something that says  </tt>. 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: </tt>,  Talk: </tt>,  User: </tt>, etc. – the test statement will return  true </tt> and produce what immediately follows the first pipe character ( | </tt>), which in the example here is a comment (the text between the  </tt> become hidden from view by web page viewers). If no namespace precedes the page, the test statement returns  false </tt> and produces what immediately follows the second pipe character – a category,  </tt>.

Another example of a conditional in is in line three, with   </tt>. The pipe character ( | </tt>) 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,  </tt> (which does not exist in ), which returns a template by the name of article if the first item does not exist. Furthermore, note that   </tt> would produce parameter article if false. So, in summary...

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  </tt> and  </tt> 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.  </tt>) belong between the <tt> </tt> tags.

<tt> </tt> is sort-of the inverse of <tt> </tt>. Everything enclosed in a <tt> </tt> 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...

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
 * ParserFunctions – more information on parser and conditional functions
 * Template list of Wikipedia templates
 * Help:Variable – a look at variables