Community Central
Community Central

In this blog, I'll try my best to explain the working of the various parser functions (provided by the ParserFunctions extension). However, I will not explain the working of the magic words, the basic set of parser functions (for instance, {{fullurl:}}). Let's get started then.

Syntax[]

Parser functions by the said extension will always be enclosed within two pairs of braces ({{ }}). The name of the function is always at the beginning prefixed by a hash (#) and suffixed by a colon (:). All arguments are placed after the colon, separated by a pipe (|). Like so:

{{#function: argument 1 | argument 2 | argument 3 | ... }}

where function is the name of the function and argument 1, 2 and 3 are the various arguments. The output depends upon argument 1, i.e., if the value of argument 1 is altered, the output will also be changed likewise.

Parser Functions[]

A list of parser functions can be found below:

#expr[]

#expr: Syntax: {{#expr: expression }}
#expr: evaluates a mathematical expression and returns the calculated value. If boolean algebra is used, zero will evaluate to false, whereas any non-zero integer will evaluate to true.
  • If the first argument is not specified, the output will be an empty string:
    • {{#expr: }}
  • If the first argument possess an invalid expression, it will result in an error:
    • {{#expr: 5 + }}Expression error: Missing operand for +.
    • {{#expr: 10 foo 6}}Expression error: Unrecognized word "foo".
  • But if the operands are placed so as to form meaningful expressions, the output will not result in an error:
    • {{#expr: + 1 }} → 1
    • {{#expr: - 5 }} → -5
  • Illustrative examples:
    • {{#expr: 10 ^ 5 }} → 10000000000
    • {{#expr: 17 - 13 }} → 4
    • {{#expr: 560 * 12 mod 9 → 6
    • {{#expr: 1 + 2 - 3 * 4 mod 5 ^ 6 }} → -9

#if[]

#if: Syntax: {{#if: string | parameter 1 | parameter 2 }}
#if: checks whether the first argument is an empty string or not. If the argument contains only white space, it is considered to be empty. If the string is not empty, the output will be the value of parameter 1 (second argument). If it is empty (or white space), the output will be the value of parameter 2 (third argument).
  • The first argument is always interpreted as pure text, hence integers are not evaluated:
    • {{#if: 1 mod 2 | yes | no }} → yes
  • Either or both the values may be omitted:
    • {{#if: string | yes }} → yes
    • {{#if: | yes }}
    • {{#if: string }} →   (empty string as the second argument is not specified)
  • Illustrative examples:
    • {{#if: string | not empty | empty }} → not empty
    • {{#if: | yes | {{#if: string | no | yes }} }} → no (case of a nested #if:)

#ifeq[]

#ifeq: Syntax: {{#ifeq: str 1 | str 2 | param 1 | param 2 }}
#ifeq: compares str 1 (first argument) and str 2 (second argument) and checks whether they are equal or not. If the two strings are equal, the output will be the value of param 1 (third argument), otherwise it will be the value of param 2 (fourth argument).
  • If both the strings have a numerical value, they are compared numerically:
    • {{#ifeq: 0001 | 1 | equal | unequal }} → equal
    • {{#ifeq: {{#expr: 10 mod 7 * 5 }} | 15 | equal | unequal }} → equal
  • Comparison made as text is always case-sensitive:
    • {{#ifeq: yes | Yes | equal | unequal }} → unequal
    • {{#ifeq: '0001' | '1' | equal | unequal }} → unequal
  • Illustrative examples:
    • {{#ifeq: string | string | equal | unequal }} → equal
    • {{#ifeq: string 1 | string 2 | equal | unequal }} → unequal
    • {{#ifeq: myString | myString | {{#ifeq: 1 | 01 | yes | no }} | no }} → yes (case of a nested #ifeq:)

#ifexpr[]

#ifexpr: Syntax: {{#ifexpr: expression | param 1 | param 2 }}
#ifexpr: evaluates a mathematical expression (expression; first argument) and returns one of the two values depending on the boolean value of the result. The value of the second argument (param 1) is treated as true, whereas the value of the third argument (param 2) is treated as false.
#ifexpr: is the same as {{#ifeq: {{#expr: expression }} | 0 | param 1 | param 2 }}.
  • If the first argument is empty, the output is evaluated to false:
    • {{#ifexpr: | yes | no }} → no
  • As with #expr, if the value of first argument is not a valid expression, the output results in an error:
    • {{#ifexpr: + | yes | no }}Expression error: Missing operand for +.
  • Either or both return values may be omitted. The output results in an empty string when the required argument is not specified:
    • {{#ifexpr: 1 + 5 | | no }}
    • {{#ifexpr: | yes | }}
  • Illustrative examples:
    • {{#ifexpr: 1 > -5 | yes | no }} → yes
    • {{#ifexpr: ( (10 ^ 3) + 5) > 10 | yes | no }} → yes

#ifexist[]

#ifexist: Syntax: {{#ifexist: page title | val 1 | val 2 }}
#ifexist: takes the first argument as the page title, and checks whether it exists in the local wiki. If it does, the output becomes the value of the second argument (val 1). If it doesn't, the output becomes the value of the the third argument (val 2). #ifexist: checks whether the page is a redlink or not (including previously deleted pages).
#ifexist has its own limits and if used after a specific number of time on a single page, the return value would be evaluated as false.
  • For system messages, the function returns true for messages that have been customized:
    • {{#ifexist: MediaWiki:Chat-log-reason-banadd | exists | doesn't exist }} → doesn't exist (said message has been customized)
  • Files can also be checked via this function. However, note that if a file page is created with the description, with no file present at the target, the return value would be true:
    • {{#ifexist: File:Wiki-wordmark.png | exists | doesn't exist }} → exists
  • Illustrative examples:
    • {{#ifexist: Special:Chat | chat exists | chat doesn't exist }} → chat doesn't exist
    • {{#ifexist: Community Guidelines | {{#ifexist: Chat Guidelines | both guidelines exist | Community Guidelines exists | {{#ifexist: Chat Guidelines | Chat Guidelines exist | no guidelines exist }} → both guidelines exist (case of a nested #ifexist:)

#switch[]

#switch: Syntax: {{#switch: string | case = result | default }}
#switch: takes the first argument as a comparison string and checks the subsequent arguments for that string. If the value of the first argument and the value of any argument before the equal sign (case) match, the return value becomes the value of that argument after the equal sign (result). If no match is found, the return value evaluates to the last argument.
  • The comparison and the case string, both are case-sensitive. Additionally, the default value when no match is found, can be declared with a case string, #default:
    • {{#switch: Case | case = result found | #default = no result found }} → no result found
  • It is possible to group results by providing a case string with no value:
    • {{#switch: string | string | string 1 | string 2 = string found | string 3 | string 4 | string 5 = other string found | #default = no string found }} → string found
  • For a comparison string, a numerical value must be used with #expr:
    • {{#switch: 2 + 3 | 5 = five | 3 = three | 2 = two | #default = one }} → one
    • {{#switch: {{#expr: 2 + 3 }} | 5 = five | 3 = three | 2 = two | #default = one }} → five
  • It is possible for a case string to be empty:
    • {{#switch: | = empty | #default = not empty }} → empty
  • If a match is found, subsequent case strings with equal values are ignored:
    • {{#switch: a | x = 1 | a = 2 | a = 3 | a = 4 | #default = 5 }} → 2
  • Case strings cannot contain raw equal signs:
    • {{#switch: 2 = 5 | 2 = 5 = found | 2 = 5 = found | #default = not found }} → found
  • Illustrative examples:
    • {{#switch: string | a = 1 | b = 2 | string = 3 | #default = 0 }} → 3
    • {{#switch: a | x = 1 | y = 2 | z = 3 | a = {{#switch: b | a = 1 | b = 2 | #default = 0 }} | #default = 0 }} → 2 (case of a nested #switch:)

#time[]

#time: Syntax: {{#time: format string}}
#time: takes a date and/or time (Gregorian calendar) and formats it according to the given syntax. A full list of valid formatting codes is given below, which can be placed in place of the format string.
List of formatting codes in #time:
Year Day
Code Description Code Description
Y 4-digit-year j day of the month, not zero-padded
y 2-digit-year d day of the month, zero-padded.
L 1 if it's a leap year, 0 if not z day of the year (-1)
Month D abbreviated name of the day of the week
n month index, not zero-padded. l full name of the day of the week
m month index, zero-padded. w number of the day of the week
M abbreviated month name Hour
F full month name a am during morning?, pm otherwise?
Week A uppercase version of a
W week number, zero-padded. g hour in 12-hour format, not zero-padded
Timezone (as of 1.22) h hour in 12-hour format, zero-padded
e timezone identifier G hour in 24-hour format, not zero-padded
I whether or not the date is in DST H hour in 24-hour format, zero-padded
O difference to GMT Minutes and seconds
P difference to GTM, with colon i minutes past the hour, zero-padded
T timezone abbreviation s seconds past the minute, zero-padded
Z timezone offset (in seconds) U seconds since January 1 1970 00:00:00 GMT
  • Inside the function, characters can be escaped either by a backslash followed by a formatting character, or by wrapping that character inside double quotes:
    • {{#time: \Y\e\a\r Y }} → Year 2024
    • {{#time: "Year" Y }} → Year 2024
  • Illustrative examples:
    • {{#time: h:i, F d, Y }} → 10:22, April 17, 2024 (time stamp)

There are a few more parser functions ({{#iferror:}}, for example), which I have not explained in this post. If you have any questions, comments or feedback, feel free to post them in the comments.