FANDOM


Parser Functions are extensions to the MediaWiki software, mainly allowing IF statements.

Read more at http://meta.wikimedia.org/wiki/ParserFunctions


#expr

#expr simply computes a mathematical expression. It uses standard operators, including: + - * / = > < <= >= != <> as well as not, or, and, mod, round, and brackets ( ) can be used to group operations.

{{ #expr: expression }}

#if

The #if statement shows something if the condition evaluates to anything other than zero, with the option of displaying alternative text.

{{ #if: <condition string> | <code if true> }}
{{ #if: <condition string> | <code if true> | <code if false> }}

Putting anything in the <condition string> will output the <code if true> section. If the <condition string> is empty, null, zero or white space, the <code if false> section will output (if it exists). If the <code if false> section is not included, and the <condition string> is null/zero/whitespace, nothing will output.

#ifexist

#ifexist returns one of two results based on whether or not a particular page exists.

{{ #ifexist: <page name> | <page exists> | <page does not exist>  }}

Standard naming convention applies: the page Vehicles is accessible from Vehicles or vehicles, but not VEHICLES. Interwiki links and special pages will output as <page does not exist>.

#ifexpr

The function #ifexpr evaluates a mathematical expression (like #expr) and returns one of two strings depending on the result.

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

For example, {{ #ifexpr: 10 > 9 | yes | no }} outputs yes.

if expression is valid

If both the <then text> and <else text> are missing from #ifexpr, nothing is output except error messages (such as division by zero). This can be used with #if to determine if an expression is valid, rather than attempting to output the result:

{{ #if: {{#ifexpr: 1=2 }} | invalid | valid }}

The above expression is "valid". The condition {{#ifexpr: 1=2 }} will not output any error because it is a valid equation, so the output will be blank (as opposed to an error message). The if statement sees no output, so reports the <code if false> statement, which proves the expression is "valid".

If the expression was invalid (such as {{#ifexpr: 2/0 }}), then the #ifexpr would output an error message. The #if statement would see positive output (not zero/null/whitespace) and so output the <code if true>. This proves the expression is "invalid".

#switch

The function #switch compares a single value against multiple others, returning a string if a match is found.

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

If <comparison value> is <value1>, then <result1> will output. If it is <value2>, then <result2> will output. If it is none of these, then <default result> will output.

It is possible to have a fall-through, allowing multiple <values> to have one <result> entry:

{{#switch: <comparison value>
 | <value1>
 | <value2>
 | <value3> = <result1,2,3>
 | <default result>
 }}

If <comparison value> is <value1> or <value2> or <value3>, then <result1,2,3> will output.

String comparison is case-sensitive. To be made case-insensitive, use the Magic Word {{lc:}} to force everything to lowercase (or {{uc:}} for upper case):

 {{ #switch: A | a=lower | A=UPPER  }} gives UPPER
 {{ #switch: {{lc:A}} | a=lower | A=UPPER  }} gives lower

#time

The function #time outputs the time, using a format string similar to that of PHP Date:

 {{ #time: format }}
 {{ #time: format | time }}

For example:

 {{ #time: l j F Y | 20070304 }} gives "Sunday 4 March 2007"
 {{ #time: l j F Y | -32 days }} gives "Sunday 6 April 2008" (32 days ago)
 {{ #time: H:i | +6 hours }} gives "20:17" (6 hours later than UTC)

#rel2abs

The function #rel2abs converts a relative path to an absolute path:

 {{ #rel2abs: path }}
 {{ #rel2abs: path | base path }}
  • If standing in Help:Foo/bar and is calling {{ #rel2abs: ../baz }}, the result will be Help:Foo/baz
  • If standing in Help:Foo and is calling {{ #rel2abs: ../baz }}, the result will be baz

If you try to go up too many levels (an invalid relative path) an error will be output.

If relative path exists

This can be used in combination with #ifexist to determine whether a path exists:

 {{ #ifexist: {{#rel2abs: .. }} | '..' exist | '..' does not exist }} gives '..' does not exist
   {{ #ifexist: {{#rel2abs: . }} | '.' exist | '.' does not exist }} gives '.' exist

#titleparts

The function #titleparts returns a number of slash-separated segments of given a page name, starting from a specified segment.

{{ #titleparts: pagename | num | starting_from }}

This returns num slash-separated segments of pagename, starting from the specified starting segment.

 "{{#titleparts:Help:Link/a/b|0|}}" gives "Help:Link/a/b" 
 "{{#titleparts:Help:Link/a/b|2|}}" gives "Help:Link/a"
 "{{#titleparts:Help:Link/a/b|2|2}}" gives "a/b"
 "{{#titleparts:Help:Link/a/b|1|3}}" gives "b"
 "{{#titleparts:Help:Link/a/b|1|-1|}}" gives "b"


Notes

  • Applying "subst:" to a parser function works, provided that there is no space between "subst:" and "#".
  • The page to which #ifexist is applied (the page you're checking) is registered as link target of a link from the page with the #ifexist code on
  • div is not integer division and is redundant, use / (slash) for real divisions.
  • mod uses PHP's % operator, which is different from modulo-operators in all other programming languages, see also template:mod ( talk edit history links ) and 6068.
  • mod sometimes returns wrong results for the same input values, see 6356 and /MOD10000. Update: values less than 1E+12 are apparently not affected.
  • Under certain conditions round 0 results in -0 instead of 0. For an expression x using 0+(x) fixes this oddity.


Table Syntax

Currently wiki pipe table syntax doesn't work inside conditionals, but this can be worked around by using {{pipe}} to replace using | for the table syntax.

This extension is enabled by default on Fandom.

A parser function is a form of basic coding that happens within the MediaWiki language, wikitext. It allows you to make a template more robust and adaptable. Written well enough, a parser function definition produces different results in different situations.

For instance, instead of simply repeating the same statement on every page where the template {{tlx|My Great Template}} is used, you can use a parser function to print one result if the page is in Category:Files, and a completely different one if it's in Category:Opinions.

The ways in which you can use parser functions are far too varied for us to share here. But hopefully we can help you start exploring them.

Basic usage

To parse something is merely to examine a statement for its logical components and then take action upon it. For instance:

If this article's name is "Supergirl 23", then make the "next issue" field in the infobox be "Supergirl 24".

That's a parser function.

How do you write one out? Parser functions look like templates, surrounded by curly brackets. They are most easily identified by having either a colon or a hash sign (#) in the function name (or both, as is in the case of the {{#if:}} function).

Let's say that you wanted to send people to the page called Supergirl, unless you were already on that page. Here's one simple way of doing it:

{{#if:{{PAGENAME}}|Supergirl| |Go see [[Supergirl]]}}

This means, "If the page I'm on is called 'Supergirl' (exactly), print nothing. Otherwise, print, 'Go see [[Supergirl]]'."

Maybe, though, you are designing an infobox for a wiki about a TV show, and you want to do some simple math to act upon the value of the {{{Age}}} variable.

By having {{#ifexpr: {{{Age}}} >= 18 | This character is an adult. | This character is not an adult. }}, the infobox could print, "This character is an adult," if the given age of the character is at or above 18. If the given age was less than 18, the code would instead say, "This character is not an adult."

Built-in MediaWiki parser functions

The MediaWiki software itself includes multiple parser functions at the core. A full list of these are available here on MediaWiki.org. This page will illustrate the most useful.

{{fullurl:}}
Returns the full URL of a given page.
{{fullurl:Help:Contents}}/wiki/Help:Contents
{{#language:}}
Converts a language code to the language name.
{{#language:es}} → Español
{{lc:}}
Makes a string lowercase (opposite of {{uc:}}).
{{lc:CASE}} → case
{{ns:}}
Takes a namespace number and tells you what the namespace is.
{{ns:4}} → Community Central
{{plural:}}
If a given expression equals one, returns the singular value given. If a given expression equals two, returns the plural value given.
{{plural:1|is|are}} → is
{{plural:2|is|are}} → are

ParserFunctions extension

Ifelselogic

The ParserFunctions extension relies on simple logic to return useful code.

The ParserFunctions extension is a popular MediaWiki extension that adds a number of useful parser functions on top of the MediaWiki ones listed above. This extension is enabled by default across all of Fandom, so you don't need to ask for it.

Parser functions can perform mathematical operations, evaluate if/else statements, and even manipulate time itself! (Well, the expression of time, anyway!)

To view a complete list of these functions and extremely useful documentation on how to use them, please visit the extension's official help page.

String functions

String functions are now a part of the ParserFunctions extension. But they used to be a part of their own, separate extension. So don't get confused when you read about it. Only the StringFunctions extension is deprecated. String functions themselves are alive and well!

String functions are a series of specialised parser functions that work on text strings. They allow you to do things like:

  • determine the length of a string
  • figure out which position in the string a particular character occupies
  • find a whole word in a string

On their own, string functions may seem arcane, but they're vital when you're trying to do something like turn Ant-Man (Earth-616) into just Ant-Man. They allow you to detect the parenthetical (Earth-616) and chop it off!

Other extensions

Some other extensions that are popular on Fandom add their own parser functions. Two of the more common are Arrays and Variables.

To find out more about these, please consult that extension's page on MediaWiki.org.

Which parser functions are available here?

Because different wikis can have different parser functions, it may be useful to figure out precisely what's on a particular wiki. Look on the Special:Version of your wiki for the full list.

See also

Further help and feedback

Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+