Welcome to PathfinderWiki. In order to curtail spam, editing is restricted to registered users only. Please request an account here.

Forum:MW Academy 2: Logical template parameters

From PathfinderWiki
Jump to: navigation, search
Forums: Grand Lodge > MW Academy 2: Logical template parameters


Per PathfinderWiki_talk:Administrators, an attempt to reduce the bus factor of our templates. These help topics are entirely unplanned, unscientific, and unscheduled so might trip, fall, and mock itself mercilessly. Don't hesitate to ask silly, obvious, or piercing questions, otherwise, I'm doing this all for naught. In most cases, I'm just simplifying information from the existing (but convoluted) MediaWiki documentation.

Thinking conditionally about spitting

  • {{{1}}} or {{{parameter_name}}} inside a template will spit out the value contained within.
  • But, what if we don't want to do anything if the parameter wasn't given a value?
  • What if we've got a Template:Person with lots of different characteristics, but we know only that our Person is a halfling male?
  • It's a waste of space to show an empty class and deity and familiar and aliases and homeland and phew! all that other stuff.
  • What we want to do is conditionally show things only if they exist.
  • Simply: if a Person's class was passed to the template, show it. Otherwise, do nothing.

Parser functions

  • Inside MediaWiki is a parser. It takes all the gobbledygook we type in and renders it into beauty.
  • It knows that the three surrounding apostrophes of '''I AM YELLING''' means we want it bold.
  • It knows lots of other syntax too, like turning * into bullets and # into numbers and == into headings and so forth.
  • But, these are really just like the template parameters: it replaces what it sees with something else (ex. {{{1}}} with "Aboleth").
  • Parser functions add logic to the parser. You can think of parser functions like software programming.
  • Yes, if you do enough complex template work, you're essentially programming MediaWiki to do something entirely new.
  • We're going to program the template to do something besides just replacing "this" with "that" and "whozits" with "what zits".
  • There are hundreds of parser functions with an infinite number of ways to use them.
  • And more can be added with MediaWiki extensions.
  • You can see a list of the ones we have installed at the bottom of Special:Version.
  • It would be ... unwise for me to spend my time documenting all of them.

A basic parser function: {{#if: }}

  • Recall what we said before: if a Person's class was passed to the template, show it. Otherwise, do nothing.
  • Let's turn that human-readable statement into something that MediaWiki can understand:
{{ #if: {{{class}}} | This person's class is {{{class}}}. }}
  • And say it again in human-speak: if a Person's class is passed, show "This person's class is [the value of class]."
  • The corollary here is: if a Person's class is NOT passed, show absolutely nothing.
  • We can even turn that corollary into code if we wanted (but we don't have to):
{{ #if: {{{class}}} | This person's class is {{{class}}}. | }}
  • It's subtle, but we added an extra pipe at the end. With nothing after it, it means "do absolutely nothing".
  • Let's make the "not passed" condition a little more blatant:
{{ #if: {{{class}}} | This person's class is {{{class}}}. | This person has no class. Haw, haw. }}
  • In human-speak, "If class is passed, show this string. Otherwise, show a bad joke."
  • You'll see this use of {{#if: }} throughout our infoboxes templates (Template:Person, Template:Book, etc.).
    • If a parameter is passed through, show a row in the infobox. If it's not, don't show anything.
  • Anything can go in the "do this" or "do that" part of the {{#if: }}.
  • For example, my auto-categorization efforts are based on whether parameters are passed.
  • Here's a (fictional) example of how to automatically categorize all people into class categories:
{{ #if: {{{class}}} | [[Catergory:Characters who are {{{class}}}s]] }}
  • If someone passes in "wizard" as a value for "class", then the page will be categorized as Category:Characters who are wizards.

A general word of warning: MediaWiki templates are madness

  • To put it bluntly, MediaWiki templates were originally just to reduce duplication.
  • But, over time, more and more things have been added into them, making them a very complex beast.
  • They've only grown more maddening since then, requiring hacks like Template:! and Template:=.
  • And, if you aren't careful, you can spend yarns of hair pulling figuring out why something isn't working.
  • This, sadly, is just a natural side-effect of doing cooler and cooler things with templates.

Other entries in MW Academy

Morbus Iff 18:01, 1 May 2012 (UTC)
Personal tools

Visit the Pathfinder Online Wiki