Log in

Forum:MW Academy 4: Parameter looping and splicing

From PathfinderWiki
Forums: Grand Lodge > MW Academy 4: Parameter looping and splicing
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.

Looping over multiple parameters

  • Let's say we have a list of keywords, or authors, or locations, or levels.
  • We want to do something for each one of the entries in that list.
  • "A, B, C, D" is human-speak. We need to make it usable in a template.
    • We COULD make "A, B, C, D" parseable, but that's more complex and breaks easily.
  • Let's start with something like this, from Template:Adventure overview:
{{Adventure overview
| location1    =
| location2    =
| location3    =
  • Based on what we've learned so far, we could do something like:
{{ #if: {{{location1|}}} | [[Category:Adventures in {{{location1}}}]] }}
{{ #if: {{{location2|}}} | [[Category:Adventures in {{{location2}}}]] }}
{{ #if: {{{location3|}}} | [[Category:Adventures in {{{location3}}}]] }}
  • There are a few problems with this approach though:
    • It's boring. Lots of cutting and pasting. Makes editing in the future difficult.
    • If you want to support {{{location20}}}, you have to copy the above 20 times.
    • Eventually, you'll stop cutting and pasting, creating an upper limit on the number of entries you can pass in.
    • There's way too much syntax and brackets and noise and splooge. Ew.
  • Let's translate what we really want into human-speak:
    • For each location parameter that is defined, add a category like "Category:Adventures in LOCATION".
  • And now, let's turn that into a {{ #foreach: }} parser function:
{{ #foreach: location$n$ | [[Category:Adventures in {{{location$n$}}}]] }}
  • In the above code, $n$ represents a number starting at 1 and going as high as necessary.
    • "Necessary" is defined as "as high as we have sequential location parameters that are passed in".
  • The advantages of this approach are:
    • FaaaaaAAr less code. That one line does it all.
    • No upper limit. If we pass in 132 locations - {{{location1}}} through {{{location132}}} - it'll happily go through them all.
  • Template:Adventure overview contains a few real-life examples of {{ #foreach: }}.

Fetching meaning from chaos

  • I am going to show one small example of a regular expression.
  • Regular expressions are a language unto itself, and almost every programming language supports them.
  • Entire books have been written about regular expressions. I am not going to go nutty explaining them.
  • See for a quick and concise overview.
  • Let's start with a problem statement: I want to auto categorize books into [[Category:YEAR sourcebooks]].
  • Typically, the answer is to "add a new template parameter called year and then edit all the pages!"
  • And, yes, that would certainly work. But, Template:Book already has a "released" parameter.
  • And that already contains the year along with the month: "June 2007" is the standard format we use.
  • Adding a "year" template parameter is a lot of boring editing work... and at the cost of duplicate data.
  • It'd be better if I could somehow get at the year written into the "released" parameter.
  • We can, with a regular expression. Regular expressions eat text and spit out the desired parts.
  • Regular Expression Fun Fact #1: Given enough syntax, you can extract small pieces from a larger pie.
  • Regular Expression Fun Fact #2: You can capture these small pieces and use them in another way.
  • Regular Expression Fun Fact #3: To a regular expression, a number (0 through 9) is called a "digit".
  • Let's look at what a "year" is. For us, it's a bunch of numbers: four in a row no spaces, to be exact.
  • Here's the regular expression for "four numbers, or digits, in a row":
  • To capture those four numbers in a row, we need to capture them with a loving hug (parenthesis):
  • In a regular expression, we can hug/capture as many times as we want. The first hug is named \1 or $1.
  • The second capture is named \2 or $2 and so on. (There's no benefit to \ or $ - you use whatever feels best, readability-wise).
  • The above regular expression will capture the four digit year. In "June 2007" or "2006, April", \1 or $1 will contain 2007 or 2006.
  • Using the {{ #rmatch: }} parser function, we can return that for use in the template:
The year is {{#rmatch:{{{released|}}}|(\d{4})|$1}}.
  • If someone passes {{{released}}} as "September 17, 2007", the above will print "The year is 2007."
  • We can even print something when the regular expression can't find a match:
The year is {{#rmatch:{{{released|}}}|(\d{4})|$1|entirely unknown}}.
  • If {{{released}}} is not passed in, or contains "Gibberish", the above will print "The year is entirely unknown."
  • Let's apply the above knowledge to our problem statement:
[[Category:{{#rmatch:{{{released|}}}|(\d{4})|$1|????}} sourcebooks]]
  • Human-speak: "If a four digit number is found in {{{released}}}, set [[Category:FOURDIGITNUMBER sourcebooks]]."
  • Human-speak: "If a four digit number is NOT found in {{{released}}}, set [[Category:???? sourcebooks]] instead."

Other entries in MW Academy