What are MediaWiki templates?

MediaWiki templates boost consistency, ease content management, and streamline wiki updates, improving efficiency and creating a cohesive user experience.

...
WikiTeq
Share:
Structuring of templates

In MediaWiki, templates are pre-formatted pieces of text that can be included in other pages. Templates are technically pages in the Template namespace, so they can be created and edited using the same editor as regular pages. There is a special syntax for including templates and passing parameters via templates to the MediaWiki parser.

Helpful Hints. Mediawiki Template Example  Various MediaWiki hints using Templates  

For example, you do not need to insert a picture of Merlin and a “Helpful Hint” label into each block. Instead, insert them into the Helpful Hint template once, and then provide the block with only variable content:

{{Helpful hint | Hint text here ... }}

Why are templates important in MediaWiki?

Templates play a crucial role in MediaWiki by enabling users to uniformly apply identical content or formatting across numerous pages. This uniformity aids in preserving a consistent appearance and user experience throughout the wiki, simplifying the processes of content addition and modification. Furthermore, templates minimize redundant efforts, as any modifications to a template are instantaneously propagated to every page utilizing that template. This efficiency is particularly beneficial in the management of extensive and large wikis, saving time and streamlining content management.

Common use cases for MediaWiki Templates

MediaWiki templates are commonly used for a variety of purposes, including:

  • Creating standardized formatting for documents and pages, such as a company's branding or style guidelines, personal blog or journal.
  • Inserting commonly used information, such as contact details or disclaimers, important messages or warnings to readers, into multiple pages.
  • Providing a consistent layout for similar types of pages, such as product pages or employee bios, recipe pages or travel logs.
  • Creating infoboxes or other types of data tables that can be easily added to pages to display key information.
  • Adding citations and references of unified structure to articles.
  • Automating repetitive tasks, such as generating reports or summaries of data.

How do MediaWiki Templates work?

There are two main ways to reference a template in MediaWiki: using the "transclusion" syntax, which looks like this: 

{{TemplateName}} 

or using the "substitution" syntax, which looks like this: 

{{subst:TemplateName}}

The transclusion syntax tells MediaWiki to insert the content of the template directly into the page. This means that any updates to the template will automatically be reflected on the pages that use it.

The substitution syntax, on the other hand, tells MediaWiki to copy the content of the template into the page and treat it as regular page content. This means that any updates to the template will not be reflected on the pages that use it.

In addition to simply inserting content, templates can also accept parameters, which allow you to customize the content that is inserted. These templates features can make it easier to create complex, structured content.

The values of template parameters can be queried by other extensions (eg. DynamicPageList3) and even stored aside of the page content (eg. Semantic MediaWiki) for the fine-grained grouping or filtering of pages. Other extensions (VisualEditor, PageForms) can provide forms for the guided input of the template values.

MediaWiki VisualEditor form for the website reference template.  MediaWiki VisualEditor form for the website reference template.  

How to use parameters in MediaWiki templates?

To use parameters in a MediaWiki template, you first need to define the parameters in the template's code. After that, you will be able to pass values to the inline template call.


Various MediaWiki hints using Templates  Various MediaWiki hints using Templates  

For example, one can design a generic hint template that inserts specific image and label as required by the context. In the above example, you can add the “Hint type” parameter like this:

{{Hint | Hint type=Observe | Hint text here ...  }}
{{Hint | Hint type=Helpful hint | Hint text here ... }}
{{Hint | Hint type=Caution | Hint text here ... }}

This way, the template does the formatting job, letting the editor to focus on the hint content. To achieve this, the template code must have a kind of logical expression (simplified):

{{#switch: {{{Hint type|}}}
    |Caution = Variable content (image and label) here ...
    |Helpful hint = Variable content (image and label) here ...
    |Observe = Variable content (image and label) here ...
    |#default = Image and label to be used if no value is provided for the parameter
    }}

What's the difference between the numbered and named parameters in MediaWiki templates?

In MediaWiki templates, the named and numbered parameters both allow you to specify a value that can be used in the template, but they differ in how they are specified and accessed. Named parameters are specified by using a name and an equals sign, like this: 

{{template|name=value}}

They can be accessed in the template using the name, like this: 

{{{name}}}

One advantage of named parameters is that they make it easier to understand the purpose of the parameter because the name provides a description:

{{templateName|name=John|age=35|occupation=Software Engineer}}

This can make templates easier to use and maintain.

Numbered parameters, on the other hand, are specified by just providing a value, without using a name.They are accessed in the template using their position, like this: {{{1}}} for the first parameter, {{{2}}} for the second parameter, and so on. Parameters are delimited with “|” (pipe) symbol.

Numbered parameters are more compact and can be easier to use in some cases. For example, if you just want to specify a simple list of items, you can use numbered parameters without having to assign names to them, like this:

{{templateName|John|35|Software Engineer}}

In this example, the template has three numbered parameters, with the first one representing a person's name, the second one representing their age, and the third one representing their occupation.

Tips for using MediaWiki Templates

  • When you use numbered parameters, you must make sure that a particular piece of data is in the correct position, and the order of the named parameters can be arbitrary.
  • If you mix named and numbered parameters in the same template, note that named parameters are not assigned numbers.

In the previous example:

{{Hint | Hint type=Observe | Hint text here ... }}

There is one named parameter (“Hint type”) referred to in the template code as {{{Hint type}}}, and one numbered parameter, referred to in the template code as {{{1}}} although the parameter is in the second position in the template call. {{{1}}} means “the first unnamed parameter” here.

  • You can define a default value for the template parameter: {{{Author|Unknown author}}} will render “Unknown author” if no name was provided inline.
  • If you want to simply suppress the output of the parameter name if value is empty, just use “|” (pipe) symbol: {{{Author|}}}.
  • Instead of copying templates from Wikipedia, we would recommend to create your own templates from scratch, or to modify existing templates to better suit your needs. This will ensure that your templates are optimized for your specific use case, and will help to avoid any potential legal or technical issues.


Conclusion

We hope this blog provided the information on MediaWiki templates you were looking for. If you have any questions, feel free to leave a comment, reach out to us for a free consultation, or see if our other blogs have the answers to your questions!

Latest Stories

Here’s what we've been up to recently.


Stay informed!

Get our latest blogs and news delivered straight to your inbox.