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 information messages.

Templates are created in the  namespace, which means that a fictitious template called Shelf would be located on page. 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:

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

...while  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.

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

Another example of a conditional in current is in line three, with. 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,  (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   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  and   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. ) belong between the   tags.

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

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

ar:قالب:حدث جاري