Help:Functions

From Festipedia, hosted by the FR Heritage Group

This wiki has a range of built-in functions that are used as predefined templates. Unlike normal templates, the function name is separated from the first parameter by a colon.

lc

Converts text to lower case.

{{lc:AbC DeF}} gives abc def.

uc

Converts text to upper case.

{{uc:AbC DeF}} gives ABC DEF.

lcfirst

Converts the first character to lower case.

{{lcfirst:AbC DeF}} gives abC DeF.

ucfirst

Converts the first character to upper case.

{{ucfirst:aBc dEf}} gives ABc dEf.

padleft

Pads the text to the required width with a string or character added to the left of the text.

{{padleft:bcd|6|a}} gives aaabcd.
{{padleft:bcd|6|az}} gives azabcd.

padright

Pads the text to the required width with a string or character added to the right of the text.

{{padright:bcd|6|a}} gives bcdaaa.
{{padright:bcd|6|az}} gives bcdaza.

formatnum

Formats a number by adding commas as appropriate. Leading plus and minus are recognised.

{{formatnum:-12345678.901}} gives −12,345,678.901

urlencode

Encodes the text to a form suitable for use in a url by converting spaces to + and escaping other characters as required.

{{urlencode:Hello world ~;(*)&}} gives Hello+world+%7E%3B%28%2A%29%26.

anchorencode

Encodes the text to a form suitable for use as an anchor by converting spaces to _ and escaping other characters as required. Note that escaping for anchors produces different results to escaping for urls.

{{anchorencode:Hello world ~;(*)&}} gives Hello_world_~;(*)&.

ns

Returns the name of a namespace. The namespace can be specified as either a number or a name. Specifying a name which is not a namespace will return a blank string. The following table sets out all the possible parameters and return values for this function.

Media Media Media Media
Special Special Special Special
( main namespace ) ( empty string )
Talk Talk Talk Talk
User User User User
User talk User talk User talk User talk
Festipedia Festipedia Festipedia Festipedia
Festipedia talk Festipedia talk Festipedia talk Festipedia talk
File File File File
File talk File talk File talk File talk
MediaWiki MediaWiki MediaWiki MediaWiki
MediaWiki talk MediaWiki talk MediaWiki talk MediaWiki talk
Template Template Template Template
Template talk Template talk Template talk Template talk
Help Help Help Help
Help talk Help talk Help talk Help talk
Category Category Category Category
Category talk Category talk Category talk Category talk

localurl, localurle

These functions return the local url for the specified page. They do not check that the page name is valid. In both cases, the return value is escaped appropriately so that it can be used from any browser. If a second parameter is passed following a pipe character, this is treated as a query string and incorporated in the url appropriately. Note that localurl and localurle should always produce identical output.

{{localurl:fullpagename}} /wiki/Fullpagename {{localurl:page|query=x}} /w/index.php?title=Page&query=x
{{localurl:a&b!}} /wiki/A%26b! {{localurle:a&b!}} /wiki/A%26b!

fullurl, fullurle

These functions are similar to localurl and localurle but, instead of returning a local url, they return the full url.

Examples:

filepath

Returns the full url of the specified image. For example:

{{filepath:abc.jpg}} gives https://upload.wikimedia.org/wikipedia/commons/9/9b/Glory_Day_celebration_of_the_Poumai_Naga.jpg

PAGESIZE

Returns the length of the wikitext for the specified page. For example:

{{PAGESIZE:Main Page}} gives 2,040

PAGESINCATEGORY

Returns the number of pages in the specified category. For example:

{{PAGESINCATEGORY:Locations}} gives {27

DEFAULTSORT

This function allows the default sort key for categories to be specified. Normally, if no sort key is specified in a category tag, the page name is used as the sort key. Using DEFAULTSORT allows a different sort key to be specified. For example, {{DEFAULTSORT:Spooner, James}} would specify that the page is to be sorted as "Spooner, James" in any category for which no other sort key is specified.

Note that the name of this function is case sensitive. It can also be invoked as DEFAULTSORTKEY or DEFAULTCATEGORYSORT.

If several default sort keys are specified on a page, only the last one specified will be used.

DISPLAYTITLE

Set the display title for the page. This function can only be used to alter the capitalisation of the title. It cannot be used to substitute a completely different title. In most cases, the page should be moved in preference to using this function. However, it does provide a way of displaying the first character of the title in lower case. For example, {{DISPLAYTITLE:de Winton}} on the "De Winton" page forces that page to be displayed with the title "de Winton".

#categorytree

As an alternative to the categorytree tag (see Help:Categories), you can use {{#categorytree:category|options}}. This allows you to use templates or variables to specify the category or option values.

The options are mode, depth, hideroot, onlyroot and style. They are specified using the same syntax as template parameters. Each option is specified as:

option name=value

The options are separated by pipe characters (|).

mode

The mode can be:

  • category - display only subcategories
  • page - display subcategories and pages but not images
  • all - display subcategories, pages and images

If mode is not specified or an invalid value is given, it will default to category.

depth

This must be numeric. It specifies how many levels of the tree are shown initially. The default is 1, showing the root category and its members. Setting depth to 0 will display only the root category. This has no effect on the number of levels that can be viewed by the user clicking on the [+] links to the left of each category. Note that, if the mode is category, the maximum depth is 2. For other modes, the maximum depth is 1.

hideroot

If this is set to on, yes, true or 1, the root category will not be displayed. Only the members of the root category will appear. If hideroot is on, setting depth to 0 or turning on onlyroot will have no effect.

onlyroot

If this is set to on, yes, true or 1, only the root category will not be displayed. This is equivalent to setting depth to 0.

style

Any styles to be applied to the tree can be specified.

#if

This is an if-then-else operator. To use it, type:

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

If condition is blank or consists only of white space, the else text will be returned (or, if there is no else text, an empty string will be returned). If condition is not blank, the then text will be returned.

Note that the test is purely whether or not condition is blank. This means that, for example, {{ #if: 3=1 | true | false }} returns "true" because "3=1" is not an empty string.

#iferror

{{#iferror:expression|error|success}} gives error if the expression contains a <strong class="error"> as generated by some functions such as #expr, #ifexpr, #time and #rel2abs. This is most useful in catching bad input passed to these functions. For example:

  • {{#iferror:{{#expr:1+1}}|bad input|valid expression}} gives valid expression
  • {{#iferror:{{#expr:1+Z}}|bad input|valid expression}} gives bad input

The last two parameters are also optional. If the success parameter is omitted, then the expression is returned on success. If the error parameter is also omitted, then no output is generated on error, and expression is returned on success.

  • {{#iferror:{{#expr:1+1}}|bad input}} gives 2
  • {{#iferror:{{#expr:1+Z}}|bad input}} gives bad input
  • {{#iferror:{{#expr:1+1}}}} gives 2
  • {{#iferror:{{#expr:1+Z}}}} gives

Thus:

  • {{#iferror:wikitext|alternative}} means "render wikitext if it is correct, and otherwise alternative".
  • {{#iferror:wikitext}} means "render wikitext if it is correct".

#ifeq

This is another if-then-else operator. It compares two strings or numbers and returns a string depending on the result of that comparison. To use it, type:

{{#ifeq: <text 1> | <text 2> | <then text> | <else text>}}
{{#ifeq: <text 1> | <text 2> | <then text> }}

If text 1 and text 2 are both numbers, the comparison is numerical. If either of them cannot be interpreted as a number, a string comparison will be performed. Comparison of strings is case sensitive.

{{ #ifeq: +07 | 007 | 1 | 0 }} gives 1
{{ #ifeq: "+07" | "007" | 1 | 0 }} gives 0
{{ #ifeq: A | a | 1 | 0 }} gives 0

#ifexist

Yet another if-then-else operator, this determines which result to return depending on whether or not the specified page exists within the wiki.

{{#ifexist: <page> | <then text> | <else text>}}
{{#ifexist: <page> | <then text> }}

The normal rules for case sensitivity of page names apply, i.e. the first character is not case sensitive, the remaining characters are.

{{ #ifexist: Blanche | yes | no }} gives yes because Blanche exists
{{ #ifexist: blanche | yes | no }} gives yes because blanche is the same as Blanche
{{ #ifexist: BLANCHE | yes | no }} gives no because BLANCHE does not exist

#ifexpr

The final if-then-else operator, this one evaluates a mathematical expression to determine which string to return. If the result is non-zero, the then text is returned. If the result is zero, the else text is returned.

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

See #expr for more details on expressions.

{{ #ifexpr: 2 + 2 | true | false }} gives true
{{ #ifexpr: 2 - 2 | true | false }} gives false

#expr

This function evaluates a numerical expression. It does not work with strings.

{{ #expr: <expression> }}

Operator precedence is (1) grouping (parentheses), (2) unary operators (+/- signs, trigonometric functions, exp, ln, floor, trunc, ceil and NOT), (3) exponentiation (^), (4) multiplication and division (*, /, div, mod), (5) addition and subtraction (+ and -), (6) round, (7) comparisons (=, !=, <, >, etc.), (8) logical AND, (9) logical OR. Within the same precedence group operators are evaluated left to right. As always some redundant parentheses are better than erroneous terse code. The operators supported are:

Operator Operation Example
none {{#expr: 123456789012345}} = 1.2345678901234E+14
{{#expr: 0.000001}} = 1.0E-6
{{#expr: 5e2}} = 500
( ) Grouping operators {{#expr: (30 + 7) * 7 }} = 259
+ Unary + sign {{#expr: +30 * +7}} = 210
- Unary - sign (negation) {{#expr: -30 * -7}} = 210
e e {{#expr: e}} = 2.718281828459
pi pi {{#expr: pi}} = 3.1415926535898
sin Sine of angle in radians {{#expr: sin 0.5}} = 0.4794255386042
cos Cosine of angle in radians {{#expr: cos 0.5}} = 0.87758256189037
tan Tangent of angle in radians {{#expr: tan 0.5}} = 0.54630248984379
asin Inverse sine - the result is given in radians {{#expr: asin 0.5}} = 0.5235987755983
acos Inverse cosine - the result is given in radians {{#expr: acos 0.5}} = 1.0471975511966
atan Inverse tangent - the result is given in radians {{#expr: atan 0.5}} = 0.46364760900081
exp e to the power {{#expr: exp 2}} = 7.3890560989307
ln Natural log {{#expr: ln 4}} = 1.3862943611199
abs Absolute value {{#expr: abs -4}} = 4
floor Round down {{#expr: floor -4.1}} = -5
trunc Integer - remove decimals {{#expr: trunc -4.1}} = -4
ceil Round up {{#expr: ceil -4.1}} = -4
not Unary NOT, logical NOT {{#expr: not 0 * 7}} = 7

{{#expr: not 30 + 7}} = 7

^ Exponentiation {{#expr: 2^3}} = 8
* Multiplication {{#expr: 30 * 7}} = 210
/ Division, same as div {{#expr: 30 / 7}} = 4.2857142857143
div Division, same as /,
no integer division
{{#expr: 30 div 7}} = 4.2857142857143

{{#expr: 5 div 2 * 2 + 5 mod 2}} = 6

mod "Modulo", remainder of division after truncating both operands to an integer. {{#expr: 30 mod 7}} = 2

{{#expr: -8 mod -3}} = -2
{{#expr: -8 mod +3}} = -2
{{#expr: 8 mod 2.7}} = 0
{{#expr: 8 mod 3.2}} = 2
{{#expr: 8.9 mod 3}} = 2

+ Addition {{#expr: 30 + 7}} = 37
- Subtraction {{#expr: 30 - 7}} = 23
round Rounds off the number on the left to the power of 1/10 given on the right {{#expr: 30 / 7 round 3}} = 4.286

{{#expr: 30 / 7 round 0}} = 4
{{#expr: 3456 round -2}} = 3500

= Equality (numerical incl. logical) {{#expr: 30 = 7}} = 0
<> Inequality, same as != {{#expr: 30 <> 7}} = 1
!= Inequality, same as <>, logical xor {{#expr: 1 != 0}} = 1
< Less than {{#expr: 30 < 7}} = 0
> Greater than {{#expr: 30 > 7}} = 1
<= Less than or equal to <nowiki0</nowiki> = 0
>= Greater than or equal to {{#expr: 30 >= 7}} = 1
and Logical AND {{#expr: 4 < 5 and 4 mod 2}} = 0
or Logical OR {{#expr: 4 < 5 or 4 mod 2}} = 1

The boolean operators consider 0 to be false and any other value to be true. On output, true is shown as 1. Values (both input and output) may be expressed in scientific notation (e.g. 1E-06).

#language

{{#language: code}} gives the language name of the selected RFC3066 language code. If the code is not one that is recognised, it simply returns the code unchanged.

{{#language:cy}} (Welsh) gives Cymraeg
{{#language:fr}} (French) gives français
{{#language:zh}} (Chinese) gives 中文

#rel2abs

rel2abs converts a relative path to an absolute path.

{{ #rel2abs: <path> }}
{{ #rel2abs: <path> | <base path> }}

If base path is not specified, the current page is taken as the base.

  • If you are in Help:Foo/bar and you call {{ #rel2abs: ../baz}}, the result will be Help:Foo/baz
  • If you are in Help:Foo and you call {{ #rel2abs: ../baz}}, the result will be baz
  • If you are in Help:Foo and you call {{ #rel2abs: ../../baz}}, the result will be Error: Invalid depth in path: "Help:Foo/../../baz" (tried to access a node above the root node).
  • {{ #rel2abs: ../baz | Help:Bar/foo }} gives Help:Bar/baz
  • {{ #rel2abs: Help:Foo/bar/../baz }} gives Help:Foo/baz

#special

This function checks that there is a special page with the name specified. If there is, it returns the full name of the special page. If no such special page exists, an appropriate message is returned.

  • {{ #special: Recentchanges }} gives Special:RecentChanges
  • {{ #special: Doesnotexist }} gives Special:Doesnotexist

#switch

The switch function allows you to compare one value against several other values, returning a string if a match is found. The syntax is:

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

If the comparison value matches one of the values, the result associated with that value (i.e. the string after the equals sign) will be returned. If no match is found, the final string is taken as the default value and returned unless it contains an equals sign. If you need to use a default result which includes an equals sign, you can use #default to flag the value as the default.

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

You can use #default to place the default value anywhere you want within the list of values. However, this will make it harder for future editors to understand your code. The convention is to place the default as the final entry in the list of values.

switch also supports 'fall through' values, allowing several values to produce the same result without having to repeat the result.

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

Since value1 and value2 do not have any equals sign they will return the same result as value3.

Comparisons are numerical where possible. String comparisons are case sensitive.

{{ #switch: +07 | 7 = Yes | 007 = Bond | No }} gives Yes
{{ #switch:"+07"|"7"= Yes |"007"= Bond | No }} gives No
{{#switch: A | a=lower | A=UPPER }} gives UPPER
{{#switch: A | a=lower | UPPER }} gives UPPER
{{#switch: a | a=lower | UPPER }} gives lower

The matched value can be empty, therefore the following constructs are equivalent:

{{ #if: {{ns:0}} | not empty | empty }} gives empty
{{ #switch:{{ns:0}}|=empty|not empty }} gives empty

#time

This function allows you to format dates and times.

{{ #time: <format> }}
{{ #time: <format> | <time> }}

If no time is specified, the current time is used.

The format is a string of formatting codes. The following table lists the formatting codes.

Code Description Example output Current output
Year:
Y The 4-digit year. e.g. 2007 2024
y The 2-digit year. 00 to 99, e.g. 07 for year 2007. 24
Month:
n The month number, not zero-padded. 1 to 12 12
m The month number, zero-padded. 01 to 12 12
M The first three letters of the month name. Jan to Dec Dec
F The full month name. January December
Week:
W ISO 8601 week number (ISO years have full weeks from monday to sunday, and ISO week number 1 always contains January 4 or the first thursday of the civil year), zero-padded. 01 to 52 or 53 (depends on year) 51
Day:
j The day of the month, not zero-padded. 1 to 31 21
d The day of the month, zero-padded. 01 to 31 21
z The day of the year (starting from 0). 0 to 364, or 365 on a leap year 355
D The first three letters of the day of the week. Mon to Sun Sat
l The full weekday name. Monday to Sunday Saturday
N ISO 8601 day of the week (according to the ISO 8601 week). 1 (for Monday) through 7 (for Sunday) 6
w Number of the day of the week. 0 (for Sunday) through 6 (for Saturday) 6
Hour:
a am (between 01:00:00 and 12:59:59 on the same day) or pm in lowercase (used with the 12-hour format). am or pm pm
A Same as with code a above but in uppercase. AM or PM PM
g 12-hour format of the hour without leading zeros (one or two digits, used with am/pm or AM/PM). 1 to 12 1
h 12-hour format of the hour, with leading padding zero (two digits, used with am/pm or AM/PM). 01 to 12 01
G 24-hour format of the hour, without leading padding zero (one or two digits). 0 to 23 13
H 24-hour format of the hour, with leading padding zero (two digits). 00 to 23 13
Minutes and seconds:
i The minute, with leading padding zero (two digits). 00 to 59 59
s The second, with leading padding zero (two digits). 00 to 59 49
U Seconds since January 1 1970 00:00:00 GMT. 0 to infinity 1734789589
Miscellaneous:
L Whether it's a leap year. 1 if it is a leap year, 0 if not 1
t Number of days in the month. 28 to 31 31
c ISO 8601 formatted date, same as 2024-12-21UTC13:12:49+13:12. fixed length string 2024-12-21T13:59:49+00:00
r RFC 2822 formatted date, same as Sat, 21 Dec 2024 13:12:49 +13:12. variable length string Sat, 21 Dec 2024 13:59:49 +0000

The FestWiki implementation of this function does not support the xn, xN, xr, xg and xx format codes implemented in MediaWiki.

Any character in the format string which is not one of the formatting codes listed above is passed into the result unchanged. Placing a backslash in front of a character will place that character in the result, so \H in the format string gives H in the output. Characters enclosed in double quotes will always be passed through unchanged. Unmatched double quotes are treated as literal quotes. Backslash escaping works with double quotes, so \" gives ".

  • {{ #time: "The month is" F }} gives The month is December
  • {{ #time: i's" }} gives 59'49"

The time can be either an absolute date/time or a relative date/time. This should be used sparingly as it is slow and resource intensive. The implementation (using the standard Perl module Date::Manip) may give different results to MediaWiki. In particular, it does not seem too happy about mixing absolute dates/times with relative dates/times.

  • {{ #time: j F Y | -14 days }} gives 7 December 2024 (14 days ago)
  • {{ #time: H:i | +6 hours }} gives 19:59 (6 hours later than UTC)
  • {{ #time: m/Y | -1 months }} gives 11/2024 (1 month ago)

#timel

This function is identical to #time but returns the result in the server's local timezone (#time returns the result in UTC). For this wiki, #timel and #time always produce the same result.

#titleparts

{{#titleparts:page name|number of segments|starting segment}} returns number of segments slash separated segments of the page name starting with starting segment. If starting segment is omitted, it starts from the beginning.

  • {{#titleparts:Help:Link/a|0}} gives Help:Link/a (full pagename).
  • {{#titleparts:Help:Link/a|1}} gives Help:Link.
  • {{#titleparts:Help:Link/a|2}} gives Help:Link/a.
  • {{#titleparts:Help:Link/a|3}} gives Help:Link/a.
  • {{#titleparts:Help:Link/a|1|1}} gives Help:Link.
  • {{#titleparts:Help:Link/a|1|2}} gives a.