7 min read


Consider a case where a Haunted site’s users felt that they should make a repository of all haunted movies and share it with others. A movie, however, will have a lot of information to go with it—producers, director, actors, release date, distributors, storyline, etc. It will be very easy to create a page with all the information, but it won’t be possible to summarize the movie in a way that anybody can have summary information when they visit any movie’s page. There needs to be a common format that will be used by all the movie pages. That is how Wikipedia shows a summary of every James Bond movie at the right side of the page. Since all movies have some common attributes but different values, they use the same format for all the James Bond movie summaries—it’s only the attributes’ values that change, and not the attributes themselves. Can we use the same thing in our Haunted site?

MediaWiki has the solution, and it is known as a template. A template is a page that can be inserted into another page via a process called transclusion. Templates usually reside in the Template namespace in MediaWiki. Templates are useful for any text for which one wants a copy in two or more pages, and there is no need for each copy to be edited independently, to adapt it to the page it is in. Templates can also be parameterized—we can add parameters to a template in order to show different content based on the parameter. This lets a template act like a subroutine. Looking at it from other angle, a template can be thought of as being like the include file that we use in programming.

Creating our First Template

The syntax for insertion of the page Template:templatename is {{ templatename }}. This is called a template tag. If the page Template:templatename does not exist, then {{ templatename }} works as [[Template:templatename]], a link to a non-existing page, leading to the edit page for the template. Thus, one way of creating a template is putting in the tag first, and then following the link. Let’s create our first template using this technique. Write down the following text in the URL section of the browser:


This will take us to an empty non-existent template page. We can edit the template and save it as our template. Let’s make the movie summary information template for our movie section. It will contain the movie name, a poster, screenwriter, cast details, etc. Editing a template page is similar to editing a normal page. There is no difference at all, and so we can use wiki syntax in our template page. Let us add the following content in our template page for a movie named “The Haunting” and save it:

'''The Haunting''' 


'''The Haunting''' film poster

'''Directed by''' Jan de Bont

'''Produced by''' Donna Roth,

Colin Wilson

'''Written by''' Novel:

Shirley Jackson


David Self

'''Starring''' Lili Taylor,

Catherine Zeta-Jones,

Owen Wilson,

Liam Neeson

'''Distributed by''' DreamWorks

'''Release date(s)''' July 20, 1999

'''Running time''' 113 minutes

'''Language''' English

'''Budget''' ~ US$80,000,000

We can now call this template from any of our pages using a pair of double curly braces {{ }} with the name of the template between the braces. Assuming that we are creating a new page where we will show all stories, let’s add the template to a story page. Open any of the story pages that we have created so far, and add the following line at the beginning of the edit page:


Now save the page and preview it in the browser. You will see true magic now; the content of the template is shown in the story page as follows:

MediaWiki Content Organizing Features: Templates, Sections, Redirection

We put the template tag at the start of the page, but you can always put it anywhere you want in the content page. We can use templates to create a header, a footer, the copyright information, special messages, etc., for our site. This is a very simple but powerful use of templates. Think about a situation where we have a lot of movie information available. What we did is just for a single movie, but we can use the same template for other movies with the same type of attributes. When we use templates, we don’t have to worry about changing the summary attributes in each and every page. We will just change the template and all the pages will be updated, since pages include that template. We can do amazing things using templates. Also, since they are similar to normal pages, we can always create nice-looking templates using tables, images, links, etc.

Templates work on a project basis. So a template in one wiki will not work in another wiki site. In order to use the same template on another wiki site, we have to build the same template in that site. Also when we change a template, we must be careful about the impact of the changes in the pages where the template is actually used.

Parameterizing Templates

We already know that we can add parameters in our template to make it work like a subroutine. I hope all of you know what a subroutine means; if not you could visit the following URL: http://en.wikipedia.org/wiki/Subroutine. Based on its parameters, a subroutine performs some task and shows results. We know templates are not subroutines, but they can be used as subroutines for performing different tasks.

Take the example of our movie summary template. We have hardcoded the name of the movie and other attributes, but we can use the same template for another movie by changing the attributes’ values. So it is almost same as adding the content in each page. However, if we can parameterize the template, it will definitely make our task easy.

What we can do is make the movie name, movie poster, writer, actors’ names etc., into variables that will be set by parameters and passed from the calling page. All the template parameters are divided into two categories: named parameters and numbered parameters. In order to create a parameterized template, we need to perform the following two tasks every time:

  1. In the template page, declare parameters that will be changed based on the passed values.
  2. Call the template with proper values from the calling page.

Parameters are declared with three pairs of braces with the parameter name inside. {{{myVar}}} declares a parameter name myVar. So in the template, the parameter declaration will done as follows:

{{{parname1|default}}}, {{{parname2|default}}}

and in the tag or calling page, we have to write it as follows:


The default option in the parameter declaration is totally optional. It can be different for each and every parameter, and applies when no value has been provided for the parameter. Here default stands for the default value of the parameter. This default value will be used if a parameter is not set to any value from the calling pages.

You will see that we are using the parameter name in both template definition and declaration page. This is known as a named parameter. There is another type of parameter as well, called a numbered parameter, which is indicated by the use of ?a number instead of a name. In a numbered parameter option, the declaration looks like this:

{{{1|default}}}, {{{2|default}}}

and in the calling page, we have to write down the tag as follows:


Now back to our movie summary example. We want to convert our movie summary template to a named parameterized template. We will use different parameters for different attributes of the template. We will also use a table to make the template look better. Here is the code for the template:

{|style="width:250px; " border="0"
| colspan="2" align="center" |'''{{{name}}}'''
| colspan="2" align="center" |[[Image:{{{image}}}
| colspan="2" align="center" |''{{{caption}}}''
|'''Directed by'''||{{{director}}}
|'''Produced by'''||{{{producer}}}
|'''Written by'''||{{{writer}}}
|'''Distributed by''' ||{{{distributor}}}
|'''Release date(s)'''||{{{released}}}
|'''Running time'''|| {{{runtime}}}
|'''Language'''|| {{{language}}}
|'''Budget'''|| {{{budget}}}

Subscribe to the weekly Packt Hub newsletter

* indicates required


Please enter your comment!
Please enter your name here