Using Templates

Using MediaWiki Templates
An overview of how to use and create MediaWiki templates.

What are templates used for?
Templates exist primarily to make it easy for a piece of content to be re-used in multiple pages. Some example uses for templates include:


 * Pre-styled and self-contained "badges" or "banners" that can be placed on multiple pages.
 * Fill-in-the-blanks charts or tables.
 * Automatic generation of complex page content based on simple data points.

An example of a very simple template of the "banner" variety is Template:Antiquated, which can be seen on a handful of pages as a common indicator that the project is probably no longer being maintained.

A more complex example is Template:Project, which generates a styled table of data about a project as well as (potentially) category links for the including page based on that data.

How do templates work?
If you have a programming background, it's easy to think of a template as a function -- when you have a piece of program logic you want to be able to reuse in multiple places, you encapsulate it in a function, and you can then invoke it whenever you like. If you need to change that commonly-used bit of logic, you only need to change the function definition; everything that calls the function is affected automatically.

If you don't have a programming background, think of a template as a way to copy and paste something into a bunch of pages without having to actually copy and paste it, and at the same time make it possible to update it in all of those pages by editing it in a single place.

Another way to think of a template is as a type of link that places its contents directly in the linking page.

A simple template is invoked with the following syntax:

When the page is rendered, the contents of the page Template:TemplateName will be inserted in its place. The template page itself can modify what its including pages receive from it with the and tags, which specify that their contents should respectively only appear in including pages or never appear in including pages. This is used to separate the actual "body" of the template from documentation seen when the Template:TemplateName page is viewed directly.

Templates can also include parameters, which are included in the template usage like this:

The template page has access to these parameters as  ,  , et cetera. If you want to use a default value in cases where no parameter is passed, you can specify this as  . If no default value is specified in this manner and the parameter is not passed, the literal text   will be used as the default value.

When invoking a template with parameters, note that if one of the parameter values includes a pipe (|) character, it must be escaped. The HTML code &amp;#124; is one way of doing this, although this also prevents the character from being used as part of other wiki markup, such as in a table. Another way (which allows the pipe character to be used in other markup) is to wrap the pipe in its own template; this wiki (and others such as Wikipedia) use Template:! for this purpose, so that a pipe character in a template parameter can be expressed as  | .

Parameters can also be given arbitrary names rather than being numbered by the order in which they appear, which is usually preferable when there are too many of them to keep straight otherwise. The syntax in this case is:

The template page will then have access to these as  , et cetera.

A contrived example
Suppose that we create a page called Template:Trio and give it these contents:

''',, and '

Then suppose that in another page we put the text:

This would result in a page containing: Huey, Dewey, and Louie

A real-life example
The template Template:Sam Stafford is another example of a simple template that takes no parameters and generates a banner for inclusion on a number of pages. If you click on that link, you'll see exactly what the template produces when it's used on a page, since no special tags are used to modify what is included from that template. Clicking the What links here toolbox link from that page shows you all of the pages that use that template; when the template is updated, all of those pages immediately see the change.

If you look at the source for this template, which as of this writing is:

you'll notice that it provides the contents of the banner but not the layout. For this, it itself uses another template, Template:Contributor. This template's source is:

's Projects

This provides the basic styling and layout for the banner, and gets its contents from the two parameters passed to it; the first parameter is the name of the contributor for the top of the banner, and the second parameter is the list of links for the bottom. This allows multiple different contributor-specific banners to be created without each one having to set up the styling and layout. If Template:Contributor is ever changed, the change will be reflected in Template:Sam Stafford and therefore all of the pages that use that template in turn.