top of page
Search

What are MediaWiki templates?

Updated: Dec 27, 2022

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
Helpful Hints Mediawiki Template Example

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 are important in MediaWiki because they allow users to easily insert the same content or formatting into multiple pages. This helps to maintain a consistent look and feel across the entire wiki, and makes it easier for users to add and edit content. Additionally, templates can help to reduce duplication of effort, as changes to a template are automatically reflected on all the pages that use that template. This can save time and make it easier to manage the content of a large wiki.


What are 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 ... }}