One of the strengths of MediaWiki is that you can use templates to include the same information across a wide number of pages. (I wish WordPress would do this.)

The idea is a simple one: create a page in the `Template` namespace, and then include it in any page by wrapping its name in double curly brackets:

{{Template:WP_Title_Box}}

In Template:WP Title Box, I have the following:

<div class="kjo-manual-title">
''How to use '''WordPress''' — the easy-to-use content management system that lets you add, delete, or edit content on your website as easy as writing an email.''
</div>

When that template is included in a page, it results in the following:

mediawiki-wordpress-template

So far, that’s pretty straightforward.

Parameters: Passing a Variable to a Template

However, there are times when you need to include a template, but vary some of the information it contains. For example, I was creating a color manual which included samples of various colors. In each wiki page, I had to type:

{| width=90% 
|- style="background:#FF8818; color:#000;"
| align=center width=10% | FF8818 || width=90% | Bright Orange
|- style="background:#F7B271; color:#000;"
| align=center | F7B271 || Light Orange
|- style="background:#8D43DB; color:#000;"
| align=center | 8D43DB || Violet
|- style="background:#491B7A; color:#fff;"
| align=center | 491B7A || Purple
|- style="background:#2E0857; color:#fff;"
| align=center | 2E0857 || Dark Purple
|}

Notice that each background color is included twice, because I am using it as part of a CSS style to color the background, but I also need to include it in the text, so that readers know what color they are looking at.

mediawiki-wordpress-template2To do this many times, I need to simply copy and paste that source code, and then change four items in each row: the background color in the style, the background color in the text, the color of the description text, and the description itself.

As it turns out, this is just what template parameters are meant for. I created a template called “Color Manual” which looks like this:

{| width=90% 
|- style="background:#{{{1}}}; color:#{{{2}}};"
| align=center width=10% | {{{1}}}|| width=90% | {{{3}}}
|- style="background:#{{{4}}}; color:#{{{5}}};"
| align=center | {{{4}}}|| {{{6}}}
|- style="background:#{{{7}}}; color:#{{{8}}};"
| align=center | {{{7}}}|| {{{9}}}
|- style="background:#{{{10}}}; color:#{{{11}}};"
| align=center | {{{10}}}|| {{{12}}}
|- style="background:#{{{13}}}; color:#{{{14}}};"
| align=center | {{{13}}}|| {{{15}}}
|}

The fifteen numbers wrapped in triple curly brackets are template parameters that will be passed to the main page when the template file is parsed. To include this template file and its parameters, I simply add this code to any page:

{{Color Manual
|1=FF8818
|2=000
|3=Bright Orange
|4=F7B271
|5=000
|6=Light Orange
|7=8D43DB
|8=000
|9=Violet
|10=491B7A
|11=FFF
|12=Purple
|13=2E0857
|14=FFF
|15=Dark Purple
}}

Each line corresponds to a separate parameter, although that is just to make things easy for me. The pipe character is the true separator. Notice that I only have to type each background color code one, since the same parameter gets passed from the template to the including page twice. Neat!

This is a slightly more complicated example than you may see on the MediaWiki template documentation, and it doesn’t seem that I am writing that much less code. But this approach, even in this instance, has several advantages:

  1. I am actually typing less code: just two hex codes and a color name for each row of the table.
  2. I am not copying and pasting wiki markup, so there is much less opportunity for me to make a mistake in the code and cause an error in the page.
  3. If I decided to change the basic layout of the color page (say, making the table only 50% of the page width, or making the description side shorter relative to the color code side) I only have to change a single template, rather than changing the dozens or even hundreds of pages this template may end up on.

That’s it, really. There are other ways to pass parameters, other than numbering them, but for this example, this approach made sense. You can read about them on the MediaWiki template documentation.