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

Forum:MW Academy 3: Template values and switches

From PathfinderWiki
Jump to: navigation, search
Forums: Grand Lodge > MW Academy 3: Template values and switches


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.

Default values for a template parameter

  • We've seen the following, which lets us check if a parameter has been passed:
{{ #if: {{{class}}} | This person's class is {{{class}}}. }}
  • And we've also seen that we can act when a parameter has NOT been passed:
{{ #if: {{{class}}} | This person's class is {{{class}}}. | This person has no class. Haw, haw. }}
  • Sometimes, we always want a parameter to have a value, even if it has not been passed.
  • For example, consider the following entirely fictional example:
{{ #if: {{{level}}} | This person is level {{{level}}}. | This person is level 0. }}
  • When a level is passed, we print it, and when the level is not passed, we print the same string, only with 0.
  • We can simplify this by using the parameter's default value - the value the parameter will get when it wasn't given a value.
  • The following is the exact same thing as the previous code sample, but is far easier to use, edit, and understand:
This person is level {{{level|0}}}.
  • When {{{level}}} exists, use the value we've been given. Otherwise, use 0 instead.
  • "0" could have been anything - a string, an path, a link, a word, etc.
  • A real, but simple, example of a default value can be seen in Template:Deletion.
  • For sanity's sake and to prevent potential bugs, it is usually safest if you always:
    • Use parameters with an explicit "no default": {{{parameter_name|}}} (there's a pipe at the end there).
    • Specify a default value when you've not checked its existence first: {{{parameter_name|default value}}}.
  • For simplicity's sake, previous examples did not follow these rules. From now on, I will.

Not just existence, but comparison: {{#ifeq: }}

  • We've seen how to check if a parameter has been passed or not.
  • Sometimes, we want to find out what the value actually is and do something speshul.
  • Here's a simple example that will be implemented over in Template:Book soon:
{{ #ifeq: {{{rules|}} | D&D 3.5 | [[Category:D&D 3.5 sourcebooks]] }}
{{ #ifeq: {{{rules|}} | PFRPG | [[Category:Pathfinder RPG]] }}
  • The human-speak for these two lines is:
    • If {{{rules}}} is defined and is equal to "D&D 3.5", set [[Category:D&D 3.5 sourcebooks]].
    • If {{{rules}}} is defined and is equal to "PFRPG", set [[Category:Pathfinder RPG]].
  • Like {{ #if: }}, we can also know when something is NOT equal to our desire:
{{ #ifeq: {{{rules|}} | D&D 3.5 | [[Category:D&D 3.5 sourcebooks]] | Anything in here will happen when it's not a match. }}
  • My examples use categories primarily cos that's where I'm mentally focussed for the template work I'm doing.
  • The actions taken could just as well be "create a table", "add some text or an image", or what have you.

Lotsa comparisons with quickie tweaks

  • This example has been implemented "for realz" in Template:Book.
  • We've got lots of different types of books we cover: Core, Adventure, PFS scenarios, Novels, etc.
  • We certainly could work with these particular types using lots of {{ #ifeq: }} statements, as above.
  • If you've got a lot of different comparisons to make on the same parameter, with one-liner actions, a {{ #switch: }} is useful.
  • Here's a slightly simpler example of what we're now doing in Template:Book:
{{ #switch: {{{type|}}}
 | Core = [[Category:Sourcebooks]]
 | Accessory = [[Category:Sourcebooks]]
 | Adventure = [[Category:Sourcebooks]]
 | Adventure/Accessory = [[Category:Sourcebooks]]
 | Pathfinder Society scenario = [[Category:Pathfinder Society scenarios]] [[Category:Sourcebooks]]
 | Novel = [[Category:Novels]]
 | Short Fiction = [[Category:Short fiction]]
 | [[Category:Unknown book type]]
  • In human-speak, this is what this chunk of code is doing:
    • If {{{type}}} is defined and is equal to "Core", set [[Category:Sourcebooks]].
    • If {{{type}}} is defined and is equal to "Accessory", set [[Category:Sourcebooks]].
    • If {{{type}}} is defined and is equal to "Adventure", set [[Category:Sourcebooks]].
    • If {{{type}}} is defined and is equal to "Adventure/Accessory", set [[Category:Sourcebooks]].
    • If {{{type}}} is defined and is equal to "Pathfinder Society scenario", set [[Category:Pathfinder Society scenarios]] and [[Category:Sourcebooks]].
    • If {{{type}}} is defined and is equal to "Novel", set [[Category:Novels]].
    • If {{{type}}} is defined and is equal to "Short Fiction", set [[Category:Short fiction]].
    • If {{{type}}} is not defined, set [[Category:Unknown book type]].
  • Notice how the human-speak is the same for both {{ #ifeq: }} and {{ #switch: }}.
  • Using a bunch of {{ #ifeq: }}s would have more brackets, colons, and other syntaxy things to get exactly right.
  • A {{ #switch: }} uses less syntax, looks nicer, is easier to edit...
  • And, in this case, we've added some error checking with a catch-all:
    • If the parameter value does not match any of our comparisons, categorize the book as an unknown type.

Other entries in MW Academy

Morbus Iff 00:35, 2 May 2012 (UTC)
Personal tools

Visit the Pathfinder Online Wiki