Help:ParserFunctions

This is an archived version of this page, as edited by Rotemliss (talk | contribs) at 07:19, 12 April 2006 (External Links: Fix). It may differ significantly from the current version.

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 four functions at present: expr, if, ifeq and rand.

expr

The expr function computes mathematical expressions. The supported operators (roughly in order of precedence) are:

*
Multiplication
/ or div
Division
+
Addition
-
Subtraction (or negation)
mod
Modulo, gives the remainder of a division
round
Rounds off the number on the left to the specified number of digits after the decimal place, given on the right
=
Equality
<> or !=
Inequality
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
and
Logical AND
or
Logical OR
not
Logical NOT
( )
Grouping operators

The boolean operators consider 0 to be false and 1 to be true.

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 0 or an empty string (ignoring leading and trailing space), 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.

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> }}

rand

The rand function gives random numbers in a specified range.

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

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: