Link
Other languages: Template:ParserFunctions Template:H:h Template:Sspp Template:Shortcut Template:Move Template:Extension
The MediaWiki extension ParserFunctions is a collection of parser functions (note the difference between the name of the collection, and the general term). These parser functions have a hash character in front of the function name, so they typically have the syntax:
Contents
Functions
The extension defines nine functions: expr, if, ifeq, ifexist, ifexpr, switch, time, rel2abs, and titleparts.
#expr:
- syntax: Template:Ft
The expr function computes mathematical expressions based on permutations of numbers (or variables/parameters that translate to numbers) and operators. It does not recognize strings (use ifeq below instead). The accuracy and format of numeric results varies with the server's operating system.
Operators are listed below. In a complex expression, operators are evaluated from left to right with the following precedence (explained in more detail in Help:Calculation):
- grouping (parentheses);
- unary (+/- signs and NOT);
- multiplicative (*, /, div, mod);
- additive (+ and -);
- round;
- comparative (=, !=, <, >, etc.);
- logical AND;
- logical OR.
When evaluating as boolean, "0" is considered false and any other non-space value is true (boolean true is represented as "{{#expr: 30 and 7}}").
Decimal numbers use "." as a decimal point (formatnum:
can be used to change the decimal point to a comma for the appropriate locales). Scientific notation with E plus exponent is not yet supported on input for expressions, but used on output (see Help:Calculation).
Operator | Operation | Example | Result |
---|---|---|---|
none | Template:Ft | {{#expr:123456789012345}} | |
Template:Ft | {{#expr:0.000001}} | ||
Arithmetic | |||
+ | Unary positive sign | Template:Ft | {{#expr:+30 * +7}} |
- | Unary negative sign | Template:Ft | {{#expr:-30 * -7}} |
* | Multiplication | Template:Ft | {{#expr:30 * 7}} |
/ div |
Division | Template:Ft Template:Ft |
{{#expr:30 / 7}} {{#expr:30 div 7}} |
+ | Addition | Template:Ft | {{#expr:30 + 7}} |
- | Subtraction | Template:Ft | {{#expr:30 - 7}} |
Logic | |||
not | Unary NOT logical NOT |
Template:Ft Template:Ft |
{{#expr: not 0 * 7}} {{#expr:not 30+7}} |
and | Logical AND | {{#expr: 4<5 and 4 mod 2}} | |
or | Logical OR | {{#expr: 4<5 or 4 mod 2}} | |
Comparison | |||
= | Equality (numerical & logical) | Template:Ft | {{#expr:30 = 7}} |
<> != |
Inequality, logical exclusive or | Template:Ft Template:Ft |
{{#expr:30 <> 7}} {{#expr:1 != 0}} |
< | Less than | {{#expr: 30 < 7}} | |
> | Greater than | {{#expr: 30 > 7}} | |
<= | Less than or equal to | {{#expr: 30 <= 7}} | |
>= | Greater than or equal to | {{#expr: 30 >= 7}} | |
Other | |||
( ) | Grouping operators | Template:Ft | {{#expr:(30 + 7) * 7 }} |
mod | "Modulo" truncates the numbers to whole numbers, divides the left number by the right, and returns the remainder that could not be divided. Note: div and mod are different from all programming languages. |
Template:Ft Template:Ft Template:Ft |
{{#expr:30 mod 7}} {{#expr:-8 mod -3}} {{#expr:8 mod 2.7}} |
round | Rounds off the number on the left to the power of 1/10 given on the right. | Template:Ft Template:Ft Template:Ft Template:Ft |
{{#expr: 30 / 7 round 4}} {{#expr:30 / 7 round 1}} {{#expr:1911 round -2}} {{#expr:-2.5 round 0}} |
#if:
- syntax:
The if
function is an if-then-else construct. The condition is always true unless it is empty or whitespace-only. The code in the second argument is parsed if the condition is true, and the third argument is parsed if the condition is false (either or both may be blank, and the code-if-false argument can be omitted entirely).
The condition is evaluated as text, so mathematical expressions will always evaluate as true; to use expressions as conditions, see #ifexpr. It is not possible to distinguish between an undefined parameter in a template and a blank one (it is possible with #ifexpr).
Examples:
- Template:Ft = {{#if:foo| do if true | do if false}}
- Template:Ft = {{#if:0 | do if true | do if false}}
- Template:Ft = {{#if: | do if true | do if false}}
#ifeq:
- syntax: