Advanced Templating Techniques in MediaWiki for Power Users

Picture this: you’re poking around a bustling wiki, and every page you glance at looks like a polished brochure, not a hodgepodge of raw wikitext

Beyond the Basics: Power‑User Templates in MediaWiki

Picture this: you’re poking around a bustling wiki, and every page you glance at looks like a polished brochure, not a hodgepodge of raw wikitext. That’s not luck; that’s the quiet magic of a well‑crafted template. If you’ve ever felt that the stock “Infobox” feels a bit like shoes that are one size too big, you’re in the right spot.

First off, let’s admit something—templates are a love‑hate relationship. You love the reusability, you hate the debugging nightmare when a stray pipe or missing brace throws a tantrum. I’ve spent more evenings wrestling with {{#iferror}} than I care to admit, but the payoff? A wiki that feels like a single, living organism rather than a patchwork quilt.

When Variables Meet Logic

Most newbies think “variables” are just placeholders, like “{{NAME}}”. Not true. In MediaWiki’s parser, you can spin up named parameters, use {{#if}} for conditionals, and even harness {{#switch}} for multi‑branch logic. Imagine you’re building a “Project Card” template that should hide the “Budget” line if the figure is zero. Here’s a sketchy‑looking snippet that actually works:


{{#if: {{{budget|}}}
 |'''Budget:''' {{{budget}}}
 | 
}}

Notice the double braces around budget? That’s the trick that tells MediaWiki “if there’s any content at all, spit it out”. If you’re feeling fancy, wrap the whole thing in {{#ifeq}} to treat “0” as “no budget”.

Loop‑De‑Loop: Building Repeating Sections

Okay, you’re thinking, “What if I need a list of team members, but the number changes daily?” Enter #foreach from the Scribunto Lua module library. I know, “Scribunto” sounds like a fancy coffee shop, but it’s just MediaWiki’s bridge to Lua scripts, and it lets you loop without turning your wiki into a spaghetti bowl of nested templates.


-- Module:TeamList
local p = {}
function p.render(frame)
    local members = mw.text.split(frame.args[1] or "", ",")
    local out = {}
    for i, name in ipairs(members) do
        table.insert(out, string.format("* %s", mw.ustring.trim(name)))
    end
    return table.concat(out, "\n")
end
return p

Then, on the wikitext side:


{{#invoke:TeamList|render|John Doe, Jane Smith, Alice Q.}}

That little one‑liner will spit out a bullet list, no matter how many commas you toss in. It’s the kind of thing that makes you grin and say “Well, that’s neat”.

Styling on the Fly: CSS Variables in Templates

Ever tried to change a template’s colour scheme without digging into the site’s Common.css? You can actually pass CSS variables straight into a template, then reference them in a <style> block inside the template. It feels a bit like slipping a secret note under the door.


{{#if: {{{bgcolor|}}}
 | 
     .my-card { background-color: {{{bgcolor}}}; }
   
}}

   {{{content|No content supplied}}}

Now you can do {{MyCard|bgcolor=#ffdead|content=Hello world}} and watch the card turn that mellow orange‑tan hue. Handy for seasonal pages—think “Winter Wiki” with icy blues, or “Hackathon 2025” with neon greens.

Debugging the Dark Corners

Let’s get real: templates break. The error messages are often cryptic, like “Parser error: Unexpected token”. My go‑to move? Sprinkle {{#showdebug}} at the bottom of the template, then preview. You’ll see the raw HTML, the parser output, and a handy list of which parameters were passed.

And for those stubborn loops that just won’t quit, add a guard clause:


{{#if: {{{loop|}}}
 | {{#invoke:MyModule|run|{{{loop}}}}}
 | 
}}

That way, if someone forgets to feed the loop param, the template politely bows out instead of spiralling into an infinite recursion that would bring the whole server to a crawl. (I’ve seen that happen on a community wiki once; the admins were pulling their hair out for hours.)

Embedding Structured Data: JSON & RDF

For the data‑geek in you, MediaWiki can output JSON or RDF from a template, which you can then grab with JavaScript on the front‑end. Say you have a “StatBox” template that should also be consumable by external tools. Here’s a quick JSON export trick:


{{#if: {{{export|}}}
 | {{#vardefine:json}}
   {
     "title": "{{{title}}}",
     "views": {{#expr: {{{views|0}}} }},
     "contributors": "{{{contributors|N/A}}}"
   }
   {{#var:json}}
}}

Later, a <script> tag can fetch that JSON via mw.config.get('wgArticleId') and feed it into a chart library. It’s a low‑key way to turn a static wiki into a data‑driven portal without pulling in a full‑blown API.

Internationalisation – Not Just a Buzzword

Templates should speak the language of the reader. MediaWiki’s Message system lets you put translatable strings inside templates. Instead of hard‑coding “Edit”, you call {{int:edit}}. For custom messages, add them to i18n/en.json (or your language file), then reference them like {{int:my‑template‑title}}. It’s a tiny step that makes the wiki feel inclusive, especially when you roll out a multilingual project later this year.

Case Study: The “Research Paper” Template

Last month my team needed a consistent layout for dozens of academic papers hosted on our internal wiki. The goal: a header with authors, abstract, citation, and a collapsible “Supplementary Materials” section that only shows up if a file is attached.

We built a composite template that pulls in three modules:

  • Module:Authors – formats a list with ORCID links.
  • Module:Cite – spits out a <cite> tag based on DOI.
  • Module:Toggle – handles the show/hide logic with a bit of JavaScript.

Here’s the skeleton wikitext (the heavy lifting lives in the modules):


{{#if: {{{doi|}}}
 | {{#invoke:Cite|render|{{{doi}}}}}
 | 
}}
{{#invoke:Authors|render|{{{authors}}}}}
'''Abstract''' {{{abstract|No abstract provided}}}
{{#if: {{{supp|}}}
 | {{#invoke:Toggle|button|label=Show supplementary}}
   {{{supp}}}
 | 
}}

Result? Each paper page reads like a mini‑journal, and the “Show supplementary” button only appears when there’s actually something to show. The whole thing took a couple of evenings to nail down, but the consistency it brought to the knowledge base was worth the coffee‑fuelled debugging sessions.

Future‑Proofing: Keep Your Templates Lean

One temptation for power users is to cram everything into one massive template. Resist that urge. Think of a template as a LEGO brick: you can stack them, replace one, and the whole structure stays sturdy. When MediaWiki 1.41 ships with the new ParserCache improvements, lean templates will reap the speed benefits automatically.

Also, version‑control your templates. A Git repo (or even the built‑in PageHistory) lets you roll back a rogue change without tearing the whole site apart. I once merged a #foreach loop that accidentally referenced the wrong variable name; the whole “Recent Changes” page blew up like a bad fireworks show. A quick revert saved the day.

Wrapping It All Up

So, what’s the take‑away? Templates aren’t just boxes you fill in; they’re a language of their own, with conditionals, loops, styling, and even data export baked right in. You can make them talk to Lua, whisper CSS, and hand off JSON to the front‑end. All while keeping the wiki accessible to non‑technical editors, because you hide the complexity behind friendly parameter names.

Subscribe to MediaWiki Tips and Tricks

Don’t miss out on the latest articles. Sign up now to get access to the library of members-only articles.
jamie@example.com
Subscribe