Help:ParserFunctions

This is an archived version of this page, as edited by Patrick (talk | contribs) at 07:08, 20 April 2006 (expr syntax). It may differ significantly from the current version.

Template:ParserFunctions

This MediaWiki extension is a collection of parser functions. Parser functions typically have the syntax:

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

This module defines five functions at present: expr, if, ifeq, ifexpr, and switch.

expr

The expr function computes mathematical expressions. The syntax is:

{{ #expr: expression }}


The supported operators (roughly in order of precedence) are:

Operator Operation Example
* Multiplication {{#expr: 30 * 7}} = 210
/ or div Division {{#expr: 30 / 7}} = 4.2857142857143
+ Addition {{#expr: 30 + 7}} = 37
- Subtraction (or negation) {{#expr: 30 - 7}} = 23
mod Modulo, gives the remainder of a division {{#expr: 30 mod 7}} = 2
round Rounds off the number on the left to the specified number
of digits after the decimal place, given on the right
{{#expr: 30 / 7 round 7}} = 4.2857143
= Equality {{#expr: 30 = 7}} = 0
<> or != Inequality {{#expr: 30 <> 7}} = 1
< Less than {{#expr: 30 < 7}} = 0
> Greater than {{#expr: 30 > 7}} = 1
<= Less than or equal to {{#expr: 30 <= 7}} = 0
>= Greater than or equal to {{#expr: 30 >= 7}} = 1
and Logical AND {{#expr: 30 and 7}} = 1
or Logical OR {{#expr: 30 or 7}} = 1
not Logical NOT {{#expr: not 7}} = 0
( ) Grouping operators {{#expr: (30 + 7) * 7 }} = 259

The boolean operators consider 0 to be false and 1 to be true. Numbers are given in decimal with "." for the decimal point. FORTRAN-style scientific notation is not supported.

Example:

{{ #expr: (100 - 32) / 9 * 5 round 0 }}

gives:

38

which is 100°F in °C, rounded to the nearest whole number.

if

The if function is an if-then-else construct. The syntax is:

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

If the condition is an empty string or consists only of whitespace, then it is considered false, and the else text is returned. Otherwise, the then text is returned. The else text may be omitted, in which case the result will be blank if the condition is false.

An example:

                      {{Template|parameter=something}}  {{Template}} {{Template|parameter=}}
                                     |                        |                |
                                     |                        |                |
                                     |                        |                |
{{ #if: {{{parameter|}}} | Parameter is defined. | Parameter is undefined, or empty }}

Note that the if function does not support "=" signs or mathematical expressions. {{#if: 1 = 2|yes|no}} will return "yes", because the string "1 = 2" is not blank. It is intended as an "if defined" structure. To compare strings, use ifeq. To compare numbers, use ifexpr.

ifeq

ifeq compares two strings, and returns another string depending on the result of that comparison. The syntax is:

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

ifexpr

ifexpr evaluates a mathematical expression and returns one of two strings depending on the result.

{{ #ifexpr: <expression> | <then text> | <else text> }}

If the expression evaluates to zero, then the else text is returned, otherwise the then text is returned. Expression syntax is the same as for expr.

switch

switch compares a single value against multiple others, returning a string if a match is found. The syntax is basically:

{{ #switch: <comparison value>
| <value1>=<result1>
| <value2>=<result2>
| ...
| <valuen>=<resultn> 
| <default result>
}}

switch will search through each value passed until a match is found with the comparison value. When found, the result for that value is returned (the text string after the equal sign). If no match is found, but the last item has no equal sign in it, it will be returned as the default result.

Note that it's also possible to have "fall through" for values (reducing the need to duplicate results). For example:

{{ #switch: <comparison value>
| <value1>
| <value2>
| <value3>=<result3>
| ...
| <valuen>=<resultn> 
| <default result>
}}

Note how value1 and value2 contain no equal sign. If they're matched, they are given the result for value3 (that is, whatever is in result3).

Installation

Download both of these files and put them in a new directory called ParserFunctions in your extensions directory.

Then put the following at the end of your LocalSettings.php:

require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" );

You can also browse the code tree here: