Help:ParserFunctions
From Homestar Runner Wiki
m (tweaked link) |
(some rewording, etc.) |
||
| Line 4: | Line 4: | ||
==#expr:== | ==#expr:== | ||
| - | <tt>expr</tt> solves mathematical expressions. It | + | <tt><nowiki>{{#expr:}}</nowiki></tt> solves mathematical expressions. It only works with numerical entries; if you want to use strings, see [[#.23ifeq:|#ifeq]]. |
{| width="100%" border="5" cellspacing="2" cellpadding="2" style="font-size:15px;text-align:center;" | {| width="100%" border="5" cellspacing="2" cellpadding="2" style="font-size:15px;text-align:center;" | ||
|- bgcolor="#FFFF00" | |- bgcolor="#FFFF00" | ||
! width="33%" | Operator !! width="33%" | Operation !! width="33%" | Example | ! width="33%" | Operator !! width="33%" | Operation !! width="33%" | Example | ||
|- | |- | ||
| - | | () || Grouping operators || <nowiki>{{#expr: (10+20) / 30 }} = | + | | () || Grouping operators || <nowiki>{{#expr: (10+20) / 30 }} = 1</nowiki> |
|- | |- | ||
| + || Positive || <nowiki>{{#expr: +30 - +30 }} = 0</nowiki> | | + || Positive || <nowiki>{{#expr: +30 - +30 }} = 0</nowiki> | ||
| Line 41: | Line 41: | ||
==#if:== | ==#if:== | ||
| - | The <tt>if</tt> function is an if-then-else system. | + | The <tt><nowiki>{{#if:}}</nowiki></tt> 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: |
<pre>{{#if: <condition string> | <then text> | <else text> }}</pre> | <pre>{{#if: <condition string> | <then text> | <else text> }}</pre> | ||
| - | Its most common usage is in templates, where if the user defines a parameter, then it | + | 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: |
<pre>{{#if: {{{parameter|}}} | Parameter is defined | Parameter is not defined }}</pre> | <pre>{{#if: {{{parameter|}}} | Parameter is defined | Parameter is not defined }}</pre> | ||
| - | If the user types <nowiki>{{templatename|parameter=true}}</nowiki> or something similar, the result is "Parameter is defined". However, if they type <nowiki>{{templatename}}</nowiki>, the result | + | If the user types <nowiki>{{templatename|parameter=true}}</nowiki> or something similar, the result is "Parameter is defined". However, if they type <nowiki>{{templatename}}</nowiki>, the result is "Parameter is not defined". Note in the above example that the expression <tt>{{{parameter|}}}</tt> 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 (<tt>|</tt>) if the parameter has not been defined. |
| - | + | <tt><nowiki>{{#if:}}</nowiki></tt> does not solve expressions. For example, if you type <nowiki>{{#if: 30 - 30 = 50 | 1 | 0 }}</nowiki>, the result will be 1, since 30 - 30 = 50 is not empty. If you want to do if-then-else using expressions, see [[#.23ifexpr:|#ifexpr]]. | |
==#ifeq:== | ==#ifeq:== | ||
| - | <tt>ifeq</tt> compares two strings and then | + | <tt><nowiki>{{#ifeq:}}</nowiki></tt> compares two strings and then performs an if-then-else process. To use: |
<pre>{{#ifeq: <text 1> | <text 2> | <if equal> | <if not equal> }}</pre> | <pre>{{#ifeq: <text 1> | <text 2> | <if equal> | <if not equal> }}</pre> | ||
| - | If text 1 and text 2 are equal, then it | + | If text 1 and text 2 are equal, then it returns the <nowiki><ifequal></nowiki> text. Otherwise, it returns the <nowiki><ifnotequal></nowiki> text. |
Some examples: | Some examples: | ||
*'''<nowiki>{{#ifeq: {{NAMESPACE}} | Help | This page is in the Help Namespace | This page is not in the Help Namespace }}</nowiki>''' shows up as '''{{#ifeq: {{NAMESPACE}} | Help | This page is in the Help Namespace | This page is not in the Help Namespace }}'''. | *'''<nowiki>{{#ifeq: {{NAMESPACE}} | Help | This page is in the Help Namespace | This page is not in the Help Namespace }}</nowiki>''' shows up as '''{{#ifeq: {{NAMESPACE}} | Help | This page is in the Help Namespace | This page is not in the Help Namespace }}'''. | ||
| - | *'''<nowiki>{{#ifeq: 7 | +000007 | Seven = Seven | Seven is not equal to Seven }}</nowiki>''' shows up as '''{{#ifeq: 7 | +000007 | Seven = Seven | Seven is not equal to Seven }}''' | + | *'''<nowiki>{{#ifeq: 7 | +000007.0 | Seven = Seven | Seven is not equal to Seven }}</nowiki>''' shows up as '''{{#ifeq: 7 | +000007.0 | Seven = Seven | Seven is not equal to Seven }}''' |
| - | ifeq is case-sensitive | + | In this above example, notice that #ifeq can treat numbers as well as strings. |
| + | |||
| + | <tt><nowiki>{{#ifeq:}}</nowiki></tt> is case-sensitive: | ||
'''<nowiki>{{#ifeq: capital | Capital | Equal | Not Equal }}</nowiki>''' shows up as '''{{#ifeq: capital | Capital | Equal | Not Equal }}'''. | '''<nowiki>{{#ifeq: capital | Capital | Equal | Not Equal }}</nowiki>''' shows up as '''{{#ifeq: capital | Capital | Equal | Not Equal }}'''. | ||
==#ifexist:== | ==#ifexist:== | ||
| - | <tt>ifexist</tt> | + | <tt><nowiki>{{#ifexist:}}</nowiki></tt> performs an if-then-else process depending on whether a page exists. Syntax: |
<pre>{{#ifexist: <page name> | <if exists> | <if does not exist> }}</pre> | <pre>{{#ifexist: <page name> | <if exists> | <if does not exist> }}</pre> | ||
| - | ifexist does not work with interwiki links. | + | <tt><nowiki>{{#ifexist:}}</nowiki></tt> does not work with interwiki links. |
Example: | Example: | ||
| - | '''<nowiki>{{#ifexist: sbemail | [[Strong Bad Email]] | [[Main Page]] }}</nowiki>''' | + | '''<nowiki>{{#ifexist: sbemail | [[Strong Bad Email]] | [[Main Page]] }}</nowiki>''' returns '''{{#ifexist: sbemail | [[Strong Bad Email]] | [[Main Page]] }}''' because a page named "sbemail" {{#ifexist: sbemail | does exist | does not exist }}. |
==#ifexpr:== | ==#ifexpr:== | ||
| - | <tt>ifexpr</tt> is | + | <tt><nowiki>{{#ifexpr:}}</nowiki></tt> is an if-then-else routine that, like <tt><nowiki>{{#expr:}}</nowiki></tt>, can only be used with numbers. Syntax: |
<pre>{{#ifexpr: <expression> | <if true> | <if false> }}</pre> | <pre>{{#ifexpr: <expression> | <if true> | <if false> }}</pre> | ||
Some examples: | Some examples: | ||
| - | *'''<nowiki>{{#ifexpr: 30 / 40 = 120 | yes | no }}</nowiki>''' | + | *'''<nowiki>{{#ifexpr: 30 / 40 = 120 | yes | no }}</nowiki>''' returns '''{{#ifexpr: 30 / 40 = 120 | yes | no }}'''. |
| - | *'''<nowiki>{{#ifexpr: {{PAGENAME}} = ParserFunctions | This is a help page | No}}</nowiki>''' | + | *'''<nowiki>{{#ifexpr: {{PAGENAME}} = ParserFunctions | This is a help page | No}}</nowiki>''' returns '''{{#ifexpr: {{PAGENAME}} = ParserFunctions | This is a help page | No}}''' because "ParserFunctions" (the text returned by <nowiki>{{PAGENAME}}</nowiki>) is a string, not a number. |
==#switch:== | ==#switch:== | ||
| - | <tt>switch</tt> compares a string against many items, and | + | <tt><nowiki>{{#switch:}}</nowiki></tt> compares a number ''or'' a string against many items, and returns a result corresponding to the item it matches. Syntax: |
<pre> | <pre> | ||
{{#switch: <value> | {{#switch: <value> | ||
| Line 98: | Line 100: | ||
}}</pre> | }}</pre> | ||
| - | "| <result n> = <text if n is true>" can be repeated as many times as | + | <nowiki>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.</nowiki> |
| + | |||
| + | "| <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": | It is also possible to have "fall-through": | ||
| Line 109: | Line 113: | ||
| #default = <default value if none are true> | | #default = <default value if none are true> | ||
}}</pre> | }}</pre> | ||
| - | <nowiki><result 1> and <result 2> do not have equal signs, and | + | <nowiki><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.</nowiki> |
==#time:== | ==#time:== | ||
| - | <tt>time</tt> 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): | + | <tt><nowiki>{{#time:}}</nowiki></tt> 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): |
{| width="100%" border="5" cellspacing="2" cellpadding="2" style="font-size:15px;text-align:center;" | {| width="100%" border="5" cellspacing="2" cellpadding="2" style="font-size:15px;text-align:center;" | ||
| Line 147: | Line 151: | ||
| w || American day of the week || 0 (Monday - 6 (Sunday) || {{#time: w }} | | w || American day of the week || 0 (Monday - 6 (Sunday) || {{#time: w }} | ||
|- | |- | ||
| - | | a || | + | | a || am or pm (lowercase) || am-pm || {{#time: a }} |
|- | |- | ||
| - | | A || | + | | A || AM or PM (capital) || AM-PM || {{#time: A }} |
|- | |- | ||
| g || 12 hour format of the hour || 1-12 || {{#time: g }} | | g || 12 hour format of the hour || 1-12 || {{#time: g }} | ||
| Line 169: | Line 173: | ||
| t || Days in the month || 28-31 || {{#time: t }} | | t || Days in the month || 28-31 || {{#time: t }} | ||
|} | |} | ||
| - | + | 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: | Examples: | ||
*'''<nowiki>{{#time: l j F Y }}</nowiki>''' generates '''{{#time: l j F Y }}'''. | *'''<nowiki>{{#time: l j F Y }}</nowiki>''' generates '''{{#time: l j F Y }}'''. | ||
| Line 180: | Line 184: | ||
==#rand:== | ==#rand:== | ||
| - | <tt>rand</tt> generates a random | + | <tt><nowiki>{{#rand:}}</nowiki></tt> generates a random integer. It has been deprecated in current versions of ParserFunctions, but the Homestar Runner Wiki currently uses an older version that still includes <tt><nowiki>{{#rand:}}</nowiki></tt>. Syntax: |
<pre>{{#rand: <minimum> | <maximum> }}</pre> | <pre>{{#rand: <minimum> | <maximum> }}</pre> | ||
| - | The minimum is the smallest | + | The minimum is the smallest ineteger it can generate, the maximum is the largest. |
Examples: | Examples: | ||
| Line 190: | Line 194: | ||
*'''<nowiki>{{#rand: 1 | 50 }}</nowiki>''' generates '''{{#rand: 1 | 50 }}'''. | *'''<nowiki>{{#rand: 1 | 50 }}</nowiki>''' generates '''{{#rand: 1 | 50 }}'''. | ||
*'''<nowiki>{{#rand: 1 | 500000 }}</nowiki>''' generates '''{{#rand: 1 | 500000 }}'''. | *'''<nowiki>{{#rand: 1 | 500000 }}</nowiki>''' generates '''{{#rand: 1 | 500000 }}'''. | ||
| - | *'''<nowiki>{{#rand: .1 | .9 }}</nowiki>''' generates '''{{#rand: .1 | .9 }}''', because it | + | *'''<nowiki>{{#rand: -10 | -1 }}</nowiki>''' generates '''{{#rand: -10 | -1 }}'''. |
| + | *'''<nowiki>{{#rand: .1 | .9 }}</nowiki>''' generates '''{{#rand: .1 | .9 }}''', because it rounds the magnitude of the input down to the nearest integer (in this example, both the minimum and maximum values are '''0'''). | ||
==See Also== | ==See Also== | ||
Revision as of 00:05, 29 October 2007
This wiki has an extension installed, called ParserFunctions. Here's how to use them:
Contents |
#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: 30 / 40 = 120 | yes | no }} returns no.
- {{#ifexpr: {{PAGENAME}} = ParserFunctions | This is a help page | No}} returns 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.
#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 | 2026 |
| y | 2 digit year | 00-99 | 26 |
| n | Month number | 1-12 | 2 |
| m | Month number, with leading 0 | 01-12 | 02 |
| M | Month name, abbreviated | Jan-Dec | Feb |
| F | Full month name | January-December | February |
| W | Current week of the year | 1-52 or 53, depending on year | 06 |
| j | Day of the month | 1-31 | 4 |
| d | Day of the month, with leading 0 | 01-31 | 04 |
| z | Day of the year | 1-364, 365 on a leap year | 34 |
| D | Day of the week, abbreviated | Mon-Sun | Wed |
| l | Full day of the week | Monday-Sunday | Wednesday |
| N | ISO 8601 day of the week | 1 (Monday) - 7 (Sunday) | 3 |
| w | American day of the week | 0 (Monday - 6 (Sunday) | 3 |
| a | am or pm (lowercase) | am-pm | am |
| A | AM or PM (capital) | AM-PM | AM |
| 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 | 11 |
| H | 24 hour format of the hour, with leading 0 | 00-23 | 11 |
| i | The minute | 00-59 | 02 |
| s | The second | 00-59 | 57 |
| U | Seconds since Midnight on January 1, 1970 | 0-∞ | 1770202977 |
| L | Whether it's a leap year | 0 if not, 1 if yes | 0 |
| t | Days in the month | 28-31 | 28 |
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 Wednesday 4 February 2026.
- {{#time: m/Y }} generates 02/2026.
- {{#time: H:i | - 6 hours}} generates 05:02, 6 hours before the current time
- {{#time: l Y-m-d }} generates Wednesday 2026-02-04.
- {{#time: l }} generates Wednesday.
- {{#time: z | 20071031}} generates 303, Halloween day in "z" format
#rand:
{{#rand:}} generates a random integer. It has been deprecated in current versions of ParserFunctions, but the Homestar Runner Wiki currently uses an older version that still includes {{#rand:}}. Syntax:
{{#rand: <minimum> | <maximum> }}
The minimum is the smallest ineteger it can generate, the maximum is the largest.
Examples:
- {{#rand: 1 | 50 }} generates 45.
- {{#rand: 1 | 500000 }} generates 230958.
- {{#rand: -10 | -1 }} generates -3.
- {{#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).
