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:

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

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


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:

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:

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:

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.


© 2017 Kenneth John OdlePermalink for this article: