# Help:ParserFunctions

This wiki has an extension installed called ParserFunctions. ParserFunctions are operators that take strings of text or numbers and perform logical or mathematical operators on them. They are often used in templates, to display current times and dates, and to generate random information. Here's how to use them:

## #expr:

{{#expr:}} solves mathematical expressions. It only works with numerical entries; if you want to use strings, see #ifeq.

Operator Operation Example
() Grouping operators {{#expr: (10+20) / 30 }} = 1
+ Positive {{#expr: +30 - +30 }} = 0
- Negative {{#expr: -30 + 30 }} = 0
not Unary NOT, logical NOT {{#expr: not 30 + 0}} = 0
* Multiplication {{#expr: 30 * 0 }} = 0
/ OR div Division {{#expr: -30 / 30 }} = -1
{{#expr: -30 div 30 }} = -1
+ Addition {{#expr: -30 + 30 }} = 0
- Subtraction {{#expr: 30 - 30 }} = 0
round Rounds off the number on the left to the power of 1/10 given on the right {{#expr: 24 / 49 round 4}} = 0.4898
{{#expr: 24 / 49 round 2}} = 0.49
{{#expr: 24 / 49 round 0}} = 0
= Equality (will be 1 or 0) {{#expr: 30 = 0 }} = 0
{{#expr: 30 = 30 }} = 1
<> OR  != Inequality (will be 1 or 0) {{#expr: 30 <> 0 }} = 1
{{#expr: 30 <> 30 }} = 0
{{#expr: 30 != 0 }} = 1
{{#expr: 30 != 30 }} = 0
< Less Than (will be 1 or 0) {{#expr: 30 < 0 }} = 0
> Greater Than (will be 1 or 0) {{#expr: 30 > 0 }} = 1
>= Greater Than or Equal To (will be 1 or 0) {{#expr: 30 >= 0 }} = 1
<= Less Than or Equal To (will be 1 or 0) {{#expr: 30 <= 0 }} = 0

## #if:

The {{#if:}} function is an if-then-else system. If the condition string is empty, it returns the "else text". If it is not empty, it returns the "then text". The syntax is:

`{{#if: <condition string> | <then text> | <else text> }}`

Its most common usage is in templates, where if the user defines a parameter, then it returns some prescribed text. If the user does not specify the parameter, it returns a default. Example:

`{{#if: {{{parameter|}}} | Parameter is defined | Parameter is not defined }}`

If the user types {{templatename|parameter=true}} or something similar, the result is "Parameter is defined". However, if they type {{templatename}}, the result is "Parameter is not defined". Note in the above example that the expression is a special function that returns the value of the parameter if it has been defined and returns the text to the right of the vertical bar (|) if the parameter has not been defined.

{{#if:}} does not solve expressions. For example, if you type {{#if: 30 - 30 = 50 | 1 | 0 }}, the result will be 1, since 30 - 30 = 50 is not empty. If you want to do if-then-else using expressions, see #ifexpr.

## #ifeq:

{{#ifeq:}} compares two strings and then performs an if-then-else process. To use:

`{{#ifeq: <text 1> | <text 2> | <if equal> | <if not equal> }}`

If text 1 and text 2 are equal, then it returns the <ifequal> text. Otherwise, it returns the <ifnotequal> text.

Some examples:

• {{#ifeq: {{NAMESPACE}} | Help | This page is in the Help Namespace | This page is not in the Help Namespace }} shows up as This page is in the Help Namespace.
• {{#ifeq: 7 | +000007.0 | Seven = Seven | Seven is not equal to Seven }} shows up as Seven = Seven

In this above example, notice that {{#ifeq:}} can treat numbers as well as strings.

{{#ifeq:}} is case-sensitive:

{{#ifeq: capital | Capital | Equal | Not Equal }} shows up as Not Equal.

## #ifexist:

{{#ifexist:}} performs an if-then-else process depending on whether a page exists. Syntax:

`{{#ifexist: <page name> | <if exists> | <if does not exist> }}`

{{#ifexist:}} does not work with interwiki links.

Example:

{{#ifexist: sbemail | [[Strong Bad Email]] | [[Main Page]] }} returns Strong Bad Email because a page named "sbemail" does exist.

## #ifexpr:

{{#ifexpr:}} is an if-then-else routine that, like {{#expr:}}, can only be used with numbers. Syntax:

`{{#ifexpr: <expression> | <if true> | <if false> }}`

Some examples:

• {{#ifexpr: 7 + 8 = 15 | yes | no }} returns yes.
• {{#ifexpr: 7 + 8 = 78 | yes | no }} returns no.
• {{#ifexpr: {{PAGENAME}} = ParserFunctions | This is a help page | No}} returns the error Expression error: Unrecognised word "parserfunctions" because "ParserFunctions" (the text returned by {{PAGENAME}}) is a string, not a number.

## #switch:

{{#switch:}} compares a number or a string against many items, and returns a result corresponding to the item it matches. Syntax:

```{{#switch: <value>
| <result 1> = <text if 1 is true>
| <result 2> = <text if 2 is true>
| <result 3> = <text if 3 is true>
| <result 4> = <text if 4 is true>
| #default = <default value if none are true>
}}```

If <value> matches <result 2>, for example, <text if 2 is true> will be returned by the function. If <value> does not match any of the first four items, <default value if none are true> will be returned.

"| <result n> = <text if n is true>" can be repeated as many times as necessary. However, "#default" can only be used once.

It is also possible to have "fall-through":

```{{#switch: <value>
| <result 1>
| <result 2>
| <result 3> = <text if 1, 2, or 3 is true>
| <result 4> = <text if 4 is true>
| #default = <default value if none are true>
}}```

<result 1> and <result 2> do not have equal signs, and if <value> matches either of them, the text corresponding to <result 3> will be returned.

Example:
{{#switch: {{#rand: 1|10}}
|1
|2
|3
|4
|5= This number is 1-5
|6= This number is 6
|7= This number is not 8
|8= This number is 1 more than 7
|9
|10= This is either 9 or 10
}}

turns out as This number is 1-5.

## #time:

{{#time:}} is used for time and date formatting. It can be used to show the current time or day, or a set time or day in a certain format. Either way, here are the codes (following UTC):

Code What it is Possible results Currently
Y 4 digit year 1970-2038 2024
y 2 digit year 00-99 24
n Month number 1-12 4
m Month number, with leading 0 01-12 04
M Month name, abbreviated Jan-Dec Apr
F Full month name January-December April
W Current week of the year 1-52 or 53, depending on year 17
j Day of the month 1-31 22
d Day of the month, with leading 0 01-31 22
z Day of the year 0-364, 365 on a leap year 112
D Day of the week, abbreviated Mon-Sun Mon
l Full day of the week Monday-Sunday Monday
N ISO 8601 day of the week 1 (Monday) - 7 (Sunday) 1
w American day of the week 0 (Sunday) - 6 (Saturday) 1
a am or pm (lowercase) am-pm pm
A AM or PM (capital) AM-PM PM
g 12 hour format of the hour 1-12 11
h 12 hour format of the hour, with leading 0 01-12 11
G 24 hour format of the hour 0-23 23
H 24 hour format of the hour, with leading 0 00-23 23
i The minute 00-59 55
s The second 00-59 33
U Seconds since Midnight on January 1, 1970 0-∞ 1713830133
L Whether it's a leap year 0 if not, 1 if yes 1
t Days in the month 28-31 30

If any input other than those discussed above is provided, it will pass through the function unmodified. So, links and punctuation can be easily used with this function. Examples:

• {{#time: l j F Y }} generates Monday 22 April 2024.
• {{#time: m/Y }} generates 04/2024.
• {{#time: H:i | - 6 hours}} generates 17:55, 6 hours before the current time
• {{#time: l Y-m-d }} generates Monday 2024-04-22.
• {{#time: l }} generates Monday.
• {{#time: z | 20071031}} generates 303, Halloween in "z" format

## #rand:

{{#rand:}} generates a random integer. (It has been discontinued in standard versions of ParserFunctions, but the Homestar Runner Wiki uses a custom version that includes {{#rand:}}.) Syntax:

`{{#rand: <minimum> | <maximum> }}`

The minimum is the smallest integer it can generate, the maximum is the largest.

Examples:

• {{#rand: 1 | 50 }} generates 30.
• {{#rand: 1 | 500000 }} generates 436651.
• {{#rand: -10 | -1 }} generates -6.
• {{#rand: .1 | .9 }} generates 0, because it rounds the magnitude of the input down to the nearest integer (in this example, both the minimum and maximum values are 0).

## #count:

{{#count:}} is a parser function that returns an integer that increments each time the function is called.

## #var:

{{#var:}} is a parser function that allows text to be stored and retrieved.

To set a variable's value use {{#setvar:}}. Syntax:

`{{#setvar: <variable name> | <new value>}}`

This will overwrite any previous value that may have been set. It does not return a value. To retrieve a variable's value use {{#var:}}. Syntax:

`{{#var: <variable name>}}`

This replaces the text of the function with the actual text of the variable.

A variable may be conditionally initialized using {{#if:}}, {{#var:}} and {{#setvar:}}. Syntax:

`{{#if: {{#var: <variable name>}}||{{#setvar: <variable name>|<new value>}}}}`

An example (the following lines produce this result when entered in order):

• {{#var: help_Variable_Test}} returns nothing (an empty string) because the variable has not been set.
• {{#if: {{#var: help_Variable_Test}}||{{#setvar: help_Variable_Test|Initialized}}}}{{#var: help_Variable_Test}} returns Initialized.
• {{#var: help_Variable_Test}} still returns Initialized.
• {{#setvar: help_Variable_Test|New Value}}{{#var: help_Variable_Test}} returns New Value.
• {{#if: {{#var: help_Variable_Test}}||{{#setvar: help_Variable_Test|Initialized}}}}{{#var: help_Variable_Test}} still returns New Value since the variable was already initialized.

## #urlformat:

{{#urlformat:}} is a parser function that formats text to be used in a link to the official site.

`{{#urlformat: <text> }}`
• `text`Optional (default: the name of the page on which the parser function [or a containing template] is placed).

The function makes the following conversions (in order):

• All multiple space is collapsed to single spaces and trimmed off the ends.
• The disambiguation "(toon)" is ignored at the end of the text.
• The text is converted to lowercase.
• Anything that is not a letter, number, underscore, or space is removed.
• Spaces and underscores are converted to hyphens. (Consecutive spaces or underscores are converted to a single hyphen.)

The function currently has one special case: `{{#urlformat:bottom 10}}` returns `bottom-10-` (with a hyphen at the end) because the official site erroneously includes one in the URL.

This function was built to accommodate Strong Bad Emails but may be useful for other toons too. To make a valid link, other elements such a path or a number may be necessary.

Please note any oddities or requests on the talk page.

Some examples:

• {{#urlformat: some kinda robot}} generates some-kinda-robot.
• {{#urlformat: bottom 10}} generates bottom-10- (see special case above).
• {{#urlformat: myths & legends}} generates myths-legends.
• {{#urlformat: Cheat Commandos (toon) }} generates cheat-commandos.

• {{#urlformat:}} generates some-kinda-robot if the parser function (or a containing template) is placed on the some kinda robot page.
• {{#urlformat:}} generates bottom-10- if the parser function (or a containing template) is placed on the bottom 10 page.
• {{#urlformat:}} generates myths-legends if the parser function (or a containing template) is placed on the myths & legends page.
• {{#urlformat:}} generates cheat-commandos if the parser function (or a containing template) is placed on the Cheat Commandos (toon) page.

## #navlist:

{{#navlist:}} looks up a value from a list on a template and returns a value from the list, usually the next or previous value in the list, for use in navigation templates.

`{{#navlist: <template name> | <action> | <lookup value> | <flag> }}`
• `template name` — Must be a name of a valid wiki template (`Template:` prefix not necessary). A malformed template name or nonexistent template will cause an error.
• `action`Optional (default: `0`). Can be `next`, `prev`, `first`, `last`, `size`, an integer, or `#` (literal number sign).
• `lookup value` (action not `#`) — Not case sensitive; optional (default: the name of the page on which the parser function [or a containing template] is placed). If specified, must be a valid list item. If not specified, the name of the page on which the parser function (or a containing template) is placed must be a valid list item.
• `lookup value` (action `#`) — Must be an integer.
• `flag`Optional (default: no flag). Can be `target`, `pipe`, or `#` (literal number sign).

###  Action

• `next` or `1` or `+1` returns the value in the list immediately after the lookup value. If there is no value after the lookup value, it returns the first value in the list.
• `prev` or `-1` returns the value in the list immediately before the lookup value. If there is no value before the lookup value, it returns the last value in the list.
• `first` returns the first value in the list. In this case the lookup value has no effect.
• `last` returns the last value in the list. In this case the lookup value has no effect.
• `size` returns the total number of items in the list. In this case the lookup value and flags have no effect.
• `n` or `+n` returns the nth item after the lookup value (converted to an integer, limit: 1000). If it reaches the end of the list, it will wrap around to the beginning.
• `-n` returns the nth item before the lookup value (converted to an integer, limit: -1000). If it reaches the beginning of the list, it will wrap around to the end.
• `0` or a numeric value that converts to 0 returns the lookup value itself.
• `#` (literal number sign) with required lookup value integer `n` returns the nth item in the list (converted to an integer, limit: ±1000). Lookup value `0` returns the last item in the list. The list repeats backward and forward to accommodate a negative lookup value or a lookup value greater than the size of the list (lookup value `-1` returns the next-to-last item in the list; lookup value `<total size + 1>` returns the first item in the list; etc.).
• An invalid action or a numeric value that's out of range returns an error.

###  Flag

Refer to the template setup below.

• `#` (literal number sign) returns the order in the list (index) of the lookup value.
• Otherwise, if no pipe or no return value, then the flags below have no effect. The function returns the page name.
• A pipe with a return value and:
• no flag (or unrecognized flag) returns the return value.
• `target` returns the page name.
• `pipe` returns "Page name|Return value".

###  Template setup

The template should consist of a simple bulleted list. A template without a list will cause an error. Each item in the list must potentially be a valid wiki title (although it doesn't necessarily have to be a valid wiki page). Do not use brackets. Any invalid characters in the page name portion of a list item will cause an error.

A pipe can be used to return a value other than the page name.

The template can have other paragraphs or explanatory text. Only list items (wherever they appear outside `<noinclude>` sections) are parsed.

```* List of
* Page
* Names
* Page name | Return value

<noinclude>

Explanatory text.

* This list
* Will NOT be included

</noinclude>
```

###  Examples

The {{charnav-lookup}} template contains the following list:

• Homestar Runner
• The Cheat
• Pom Pom
• Marzipan
• Coach Z
• Bubs
• The King of Town
• The Poopsmith
• Homsar
• {{#navlist: charnav-lookup | next | Homestar Runner }} generates Strong Bad.
• {{#navlist: charnav-lookup | next }} generates Strong Bad if the parser function (or a containing template) is placed on the Homestar Runner page.

• {{#navlist: charnav-lookup | prev | Strong Bad }} generates Homestar Runner.
• {{#navlist: charnav-lookup | prev }} generates Homestar Runner if the parser function (or a containing template) is placed on the Strong Bad page.

• {{#navlist: charnav-lookup | next | Homsar }} generates Homestar Runner.
• {{#navlist: charnav-lookup | next }} generates Homestar Runner if the parser function (or a containing template) is placed on the Homsar page.

• {{#navlist: charnav-lookup | prev | Homestar Runner }} generates Homsar.
• {{#navlist: charnav-lookup | prev }} generates Homsar if the parser function (or a containing template) is placed on the Homestar Runner page.

• {{#navlist: charnav-lookup | +2 | Homestar Runner }} generates The Cheat.
• {{#navlist: charnav-lookup | 2.9 | Homestar Runner }} generates The Cheat.
• {{#navlist: charnav-lookup | -2 | Homestar Runner }} generates The Poopsmith.
• {{#navlist: charnav-lookup | 954 | Homestar Runner }} generates Marzipan.

• {{#navlist: charnav-lookup | 0 | Homestar Runner }} generates Homestar Runner.
• {{#navlist: charnav-lookup | | Homestar Runner }} generates Homestar Runner.
• {{#navlist: charnav-lookup }} generates Homestar Runner if the parser function (or a containing template) is placed on the Homestar Runner page.

• {{#navlist: Template:charnav-lookup | next | Homestar Runner }} generates Strong Bad. (Using the `Template:` prefix is valid.)
• {{#navlist: not-a-real-tempate | next | Homestar Runner }} generates INVALID TEMPLATE NAME.
• {{#navlist: charnav | next | Homestar Runner }} generates INVALID LIST. (Note that {{charnav}} is a valid template but doesn't contain a list.)

• {{#navlist: charnav-lookup | invalid | Homestar Runner }} generates INVALID ACTION.
• {{#navlist: charnav-lookup | 10000 | Homestar Runner }} generates ACTION OFFSET OUT OF RANGE.

• {{#navlist: charnav-lookup | next | Not in the List }} generates INVALID LOOKUP VALUE.
• {{#navlist: charnav-lookup | next }} generates INVALID LOOKUP VALUE if the parser function (or a containing template) is not placed on a page in the list.

• {{#navlist: charnav-lookup | first }} generates Homestar Runner.
• {{#navlist: charnav-lookup | # | first }} generates Homestar Runner.
• {{#navlist: charnav-lookup | # | 1 }} generates Homestar Runner.

• {{#navlist: charnav-lookup | last }} generates Homsar.
• {{#navlist: charnav-lookup | # | last }} generates Homsar.
• {{#navlist: charnav-lookup | # | 0 }} generates Homsar.

• {{#navlist: charnav-lookup | # | 5 }} generates Strong Sad.
• {{#navlist: charnav-lookup | # | -5 }} generates Marzipan.
• {{#navlist: charnav-lookup | # | 500 }} generates Coach Z.
• {{#navlist: charnav-lookup | # | 5000 }} generates INDEX OUT OF RANGE.
• {{#navlist: charnav-lookup | # | five }} generates INVALID INDEX.
• {{#navlist: charnav-lookup | # }} generates INVALID INDEX.

• {{#navlist: charnav-lookup | 0 | Homestar Runner | # }} generates 1.
• {{#navlist: charnav-lookup | | Homestar Runner | # }} generates 1.
• {{#navlist: charnav-lookup | | | # }} generates 1 if the parser function (or a containing template) is placed on the Homestar Runner page.

• {{#navlist: charnav-lookup | next | Homestar Runner | # }} generates 2.
• {{#navlist: charnav-lookup | next | | # }} generates 2 if the parser function (or a containing template) is placed on the Homestar Runner page.
• {{#navlist: charnav-lookup | prev | Homestar Runner | # }} generates 12.
• {{#navlist: charnav-lookup | prev | | # }} generates 12 if the parser function (or a containing template) is placed on the Homestar Runner page.

• {{#navlist: charnav-lookup | | Pom Pom | # }} generates 6.
• {{#navlist: charnav-lookup | | the king of town | # }} generates 10.
• {{#navlist: charnav-lookup | | Not in the List | # }} generates INVALID LOOKUP VALUE.

• {{#navlist: charnav-lookup | size }} generates 12.

The {{hrenav-lookup}} template contains the following list:

• Hremail 49
• Hremail 24
• Hremail 62
• Hremail 2000
• Hremail 7
• hremail 3184 | Hremail 3184
• {{#navlist: hrenav-lookup | prev | Hremail 49 }} generates Hremail 3184.
• {{#navlist: hrenav-lookup | prev | Hremail 49 | target }} generates hremail 3184.
• {{#navlist: hrenav-lookup | prev | Hremail 49 | pipe }} generates hremail 3184|Hremail 3184.

• [[{{#navlist: hrenav-lookup | prev | Hremail 49 | pipe }}]] generates [[hremail 3184|Hremail 3184]], which generates the piped link Hremail 3184.

• {{#navlist: hrenav-lookup | prev | }} generates Hremail 3184 if the parser function (or a containing template) is placed on the Hremail 49 page.
• {{#navlist: hrenav-lookup | prev | | target }} generates hremail 3184 if the parser function (or a containing template) is placed on the Hremail 49 page.
• {{#navlist: hrenav-lookup | prev | | pipe }} generates hremail 3184|Hremail 3184 if the parser function (or a containing template) is placed on the Hremail 49 page.

• {{#navlist: hrenav-lookup | next | Hremail 49 }} generates Hremail 24.
• {{#navlist: hrenav-lookup | next | Hremail 49 | target }} generates Hremail 24.
• {{#navlist: hrenav-lookup | next | Hremail 49 | pipe }} generates Hremail 24.

• {{#navlist: hrenav-lookup | next | }} generates Hremail 24 if the parser function (or a containing template) is placed on the Hremail 49 page.
• {{#navlist: hrenav-lookup | next | | target }} generates Hremail 24 if the parser function (or a containing template) is placed on the Hremail 49 page.
• {{#navlist: hrenav-lookup | next | | pipe }} generates Hremail 24 if the parser function (or a containing template) is placed on the Hremail 49 page.

• [[{{#navlist: hrenav-lookup | next | Hremail 49 | pipe }}]] generates [[Hremail 24]], which generates the regular link Hremail 24 because there is no pipe.

• {{#navlist: hrenav-lookup | # | last }} generates Hremail 3184.
• {{#navlist: hrenav-lookup | # | 6 }} generates Hremail 3184.
• {{#navlist: hrenav-lookup | # | 6 | target }} generates hremail 3184.
• {{#navlist: hrenav-lookup | # | 6 | pipe }} generates hremail 3184|Hremail 3184.

• [[{{#navlist: hrenav-lookup | # | 6 | pipe }}]] generates [[hremail 3184|Hremail 3184]], which generates the piped link Hremail 3184.