The ParserFunctions extension adds even more parser functions to the magic words allowed on the Scratch Wiki. The syntax of parser functions are: {{#functionname:argument 1|argument 2|argument 3 ...}}}
.
#expr
- Main page: Help:Calculation
The #expr (expression) function evaluates expressions in the function. The syntax of this function goes: {{#expr|expression}}
. Expressions are placed in the expression parameter and the function will be replaced with an answer. The expressed answer can be an error message, if the calculation cannot be done. Boolean/logical truth gates considers all numbers except zero to be true, and zero to be false. Empty expressions yield empty answers.
Code | Output | Explanation |
---|---|---|
{{#expr:5+5}}
|
10 | Five plus five is equal to ten. |
{{#expr:8/0}}
|
Division by zero. | Any division by zero is undefined, therefore, no answer. |
{{#expr: }}
|
Empty spaces result in no output. | |
{{#expr:5 and 0}}
|
0 | 5 is True, while 0 is False, so they don't both equal true. The expression is false, represented by a zero. |
#if
This function evaluates a text field to determine whether it is empty or not. If it is empty, it will print out a value. If not, it will print out another value. The values are user-defined. White space is considered an empty string. All text in the function is parsed as plain text. The syntax of this function goes: {{#if:string|value returned if string is not empty|value returned if string is empty}}
.
Code | Output | Explanation |
---|---|---|
{{#if: |true|false}}
|
false | The text field is empty (only spaces), so the expression is false. |
{{#if:notblank|true|false}}
|
true | The text field is not empty, so the expression is true. |
{{#if:a b|true|false}}
|
true | The text field is not empty (has text in it), so the expression is true. |
#ifeq
This function (if equal) evaluates two strings and checks to see if they are equal. The syntax of this function goes: {{#ifeq:string|string2|value returned if equal|value returned if not equal}}
. Avoid putting html tags into the text filed; parser tags are replaced, and will not equal each other. Numerical answer and string answers are parsed differently; numerical values are compared numerically (01 is the same as 1) and string functions are placed in quotations and are case-sensitive and numbers are parsed as text, therefore 01 and 1 are not equal. In this function, all numbers are turned into type integer, so the digit accuracy goes up to 9E18.
Code | Output | Explanation |
---|---|---|
{{#ifeq: 0| 0 |true|false}}
|
true | Numerical values are treated as numbers, so spaces do not matter. |
{{#ifeq: "0 "|" 0 "|true|false}}
|
false | Spaces in strings count. Since the spaces are not equal with each other, the expression is false. |
{{#ifeq: nien ma|nienma|true|false}}
|
false | All strings, in or not in quotations, are treated as text and spaces count. |
{{#ifeq: J|j|true|false}}
|
false | Strings are case-sensitive. |
#iferror
Functions such as the #expr function, have errors messages in case something goes wrong. This function checks to see if a nested function has outputted an error message or not. The syntax of this function goes: {{#iferror:input|string if error message is found|string if error message is not found}}
. A combination of parameters are optional. If the 'string if error message is not found' parameter is omitted, then an error-free output will then be displayed. If you remove both 'string if error message is not found' and 'string if error message is found' parameters, then an error will be shown as a blank string.
Code | Output | Explanation |
---|---|---|
{{#iferror: {{#expr:5+5}} |er|no er}}
|
no er | 5+5 is a valid expression, and no errors were produced, so 'no er(rors)' is printed. |
{{#iferror: {{#expr:abc}} |er|no er}}
|
er | abc is not a valid mathematical expression to solve. er(rors) is displayed. |
{{#iferror: {{#expr:2/8}} |er}}
|
0.25 | 2/8 is a valid expression. Since there is no 'no er(ror)' parameter, the expression answer is displayed. |
{{#iferror: {{#expr:@#$^&*}} |er}}
|
er | @#$^&* is not a valid mathematic expression. Therefore, er(ror) is displayed. |
{{#iferror: {{#expr:2^3}}}}
|
8 | 2^3 is a good expression. Since there is no 'no er(ror)' parameter, the expression answer is displayed. |
{{#iferror: {{#expr:=}} }}
|
= is not a good expression. Since there is no 'no er(ror)' or a 'er(ror)' parameter, nothing is outputted. | |
{{#iferror:<span class="error">a</span>|er|no er}}
|
er | This function detects any error classes. The class "error" is one, so the function shows its output as 'er(ror)'. |
#ifexpr
This function (if expression [is true/false]) evaluates an expression then returns one of the two parameters. The syntax of this function goes: {{#ifexpr:expression|string if true|string if false}}
. The expression is parsed exactly the way #expr is, and a blank expression results in 'false'. In any case, all numbers are equal to true, except zero which is equal to false in boolean calculations. The 'false' parameter can be omitted; if an expression is false, the output shows a blank space. The "true" parameter can also be omitted, but the pipe characters still needs to be placed.
Code | Output | Explanation |
---|---|---|
{{#ifexpr: |true|false}}
|
false | Empty expressions are considered false. |
{{#ifexpr: 8>9 |true|false}}
|
false | Eight is not greater than nine, so the output is false. |
{{#ifexpr: = |true|false}}
|
Expression error: Unexpected = operator. | Since = is an inaccurate expression, an error shows up. |
{{#ifexpr: 8+4^5 |true|false}}
|
true | Since there is no comparison, the value is always true. |
{{#ifexpr: 8=9 |true}}
|
Since 8=9 is false, and there is no 'false' parameter, an empty output shows. | |
{{#ifexpr: (3+3) = (2+4) ||false}}
|
Since 3+3 = 2+4 is true, and there is an empty 'true' parameter, an empty output shows. | |
{{#ifexpr: 9>=9}}
|
Since 9>=9 is true, and there are no parameters, an empty output shows. |
#ifexist
This expensive function takes a string, interprets it as a page title and returns one of two values depending on whether the page exists or not. The syntax of this function goes: {{#ifexist:page title|string if exists|string if doesn't exist}}
. There is a 100 call limit on this function.
Code | Output | Explanation |
---|---|---|
{{#ifexist: Help:ParserFunctions|exists|does not exist}}
|
exist | This page exists. |
{{#ifexist: Scratch Wiki:ParserFunctions|exists|does not exist}}
|
does not exist | The namespace matters. |
#rel2abs
The function turns relative file paths into absolute file paths, as the name suggests ("relative to absolute"). The first parameter is the path, and the second parameter is the base path. The base path is optional - if left nil, the full name of the current page is used. The path parameter accepts the following syntax:
.
— the current level..
— go up one level/x
— go down one level into the subdirectory x
Code | Output | Explanation |
---|---|---|
{{#rel2abs:/abc|Scratch Wiki:Example}}
|
Scratch Wiki:Example/abc | The path is appended onto the base path. |
{{#rel2abs:../../ms|Scratch Wiki:Example/Example2/Example3}}
|
Scratch Wiki:Example/ms | Two level up from the base path is Scratch Wiki:Example, then append path ms. |
{{#rel2abs:../ms|Scratch Wiki:Example}}
|
ms | One level up from the base path nil, then appends ms without leading / because the path is added to a nil path. |
{{#rel2abs:../.|Scratch Wiki:Example/Example2}}
|
Scratch Wiki:Example | Goes one level up, then gets the current level (equivalent to {{#rel2abs:..|Scratch Wiki:Example/Example2}} )
|
{{#rel2abs:../../..|Scratch Wiki:Example/Example2}}
|
Error: Invalid depth in path: "Scratch Wiki:Example/Example2/../../.." (tried to access a node above the root node). | Goes three level up, which does not exist. |
#switch
This function compares a comparison with several cases, and outputs a string defined in each case. This function is equivalent to:
set [comparison v] to [hello] set [output v] to [] if <(comparison) = [hello]> then //case1 set [output v] to [welcome!] else if <(comparison) = [bye]> then //case2 set [output v] to [see you!] else set [output v] to [ok!] //default case end end
In this case, the output variable returns "welcome!". Be aware, this function is case-sensitive.
Code | Output | Explanation |
---|---|---|
{{#switch:pop|lol = HI|pop = BOOM|aww :(}}
|
BOOM | pop, the argument, matches case 2, |pop = BOOM|, and prints BOOM. |
{{#switch:dfd|lol = HI|pop = BOOM|aww :(}}
|
aww :( | dfd, the argument, matches no case, and prints the default case, aww :(. |
{{#switch:A|a = lowercase a|none of the above}}
|
none of the above | This function is case-sensitive. |
{{#switch:ps|lf = le}}
|
If the condition matches no case and a default case is not given, the output is null. |
#time
This function formats a given date into a specified format.
{{#time: format | date given (default is {{CURRENTTIMESTAMP}}) | language code}}
. The table below gives some of the main format codes that can be used. Careful, the format code is case sensitive.
Note: | The time outputted by default is only accurate in the UTC time zone. Use the date given parameter to adjust the hour to one's own time zone. |
Code | Description | Output |
---|---|---|
Y | Full year | 2024 |
y | 2-digit year | 24 |
L | Leap-year indicator, is one if it is a leap year, otherwise 0. | 1 |
n | Month not padded | 7 |
m | Month padded | 07 |
M | Month abbreviated | Jul |
F | Full name of month | July |
W | Week, padded | 30 |
j | Day of the month, not padded | 23 |
d | Day of the month, padded | 23 |
z | Day of the year | 204 |
D | Day of the week name abbreviated | Tue |
l | Full name of day of the week | Tuesday |
N | Day of the week (Sunday = 7) | 2 |
w | Day of the week (Sunday = 0) | 2 |
a | AM or PM | am |
g | Hour in 12-hour format, not padded | 1 |
h | Hour in 12-hour format, padded | 01 |
G | Hour in 24-hour format, not padded | 1 |
H | Hour in 24-hour format, padded | 01 |
i | Minutes past hour, padded | 54 |
s | Seconds past hour, padded | 56 |
t | Days in the month | 31 |
U | Unix time | 1721699696 |
It is possible to place strings into the format code as well.
Code | Output | Explanation |
---|---|---|
{{#time:d-m-Y, G:i|now+2 hours}}
|
23-07-2024, 3:54 | The format string is day padded "-" month padded "-" 4-digit year "," 24-hour format hour ":" minutes past hour. The date provided is the current timestamp plus two hours. |
{{#time:d-"m"-Y}}
|
23-m-2024 | To literally show format code letters, use quotations. |
{{#time:M||de}}
|
Jul. | "de", or the character code for German, is places as the language code to translate the months. |
#timel
This is exactly the same as #time, but uses the wiki local time taken from $wgLocaltimezone.
{{#timel: format | date given (default is {{CURRENTTIMESTAMP}}) | language code}}
#titleparts
The function splits a page title by its slashes and sub-pages. This function can accept three arguments, pagename, optional number of segments, and optional first segment. If the number of segments parameter is omitted, then it defaults to "0", returning all segments. If the first segment parameter is omitted, it defaults to one.
Code | Output | Explanation |
---|---|---|
{{#titleparts:Talk:Hello/Bye/Yo/Me/See|1|2}}
|
Bye | Goes to the first section ("Bye") and returns only itself. |
{{#titleparts:Talk:Hello/Bye/Yo/Me/See||3}}
|
Yo/Me/See | The number of segments parameter is omitted, which means that it defaults to 0 (returns all the segments). The start is 3 (at "Yo"), so it returns everything starting at "Yo". |
{{#titleparts:Talk:Hello/Bye/Yo/Me/See||-2}}
|
Me/See | Returns the last two segments. |
{{#titleparts:Talk:Hello/Bye/Yo/Me/See|-2}}
|
Talk:Hello/Bye/Yo | Removes 2 segments from the end of the string. |
{{#titleparts:Talk:Hello/Bye/Yo/Me/See|-2|-3}}
|
Yo | Removes 2 segments from the end of the string then starts from the 3rd last segment. |