Community Central
Register
Community Central

A parser function is a form of wikitext that allows you to make templates more robust and adaptable. When written well enough alongside template parameters, 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 {{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:Help.

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.

Basics[]

Explanation[]

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

For instance:

If the name of this article is "Help:FandomDesktop", then add "You are now in the FandomDesktop help page."

This is the basics of parser functions.

Writing parser functions[]

Note: This page uses the source editor for writing the parser functions. You can technically edit them using the VisualEditor, but the interface is very limited.

Parser functions are written by adding curly braces ({{), similar to templates. However, most of them usually require you to add a hash sign (#), to easily differentiate them.

Now, returning to the above example, the code for this is:

{{#ifeq: {{FULLPAGENAME}} | Help:FandomDesktop
 | You are now in the FandomDesktop help page.
}}

It may look complicated, so a step-by-step guide is written below:

  1. The parser function #ifeq: means "if equals". That may not make any sense, but continue reading.
  2. The first part is a magic word, which outputs the "full page name" (meaning the namespace alongside the name of the page).
  3. The second part inputs "Help:FandomDesktop" to the parser function. Note that the "inputs" of parser functions are separated by the bar |, like template parameters.
  4. The third part outputs "You are now in the FandomDesktop help page.", if the full page name is equal to Help:FandomDesktop.

Here is how you read the statement:

If the full page name is equal to Help:FandomDesktop, output "You are now in the FandomDesktop help page."

There we go! Now, you have now understood how a parser function works.

Additional examples[]

A template that checks if a character is an adult[]

This template uses the #ifexpr parser function. It also details template parameters, too.

So, simply put this to the code:

{{#ifexpr: {{{age|}}} > 18
| The character is an adult
| The character is '''not''' an adult.
}}

In basic terms, the above code means "If age= is greater than 18, the character is an adult. Otherwise, no."

Let's break it down step by step:

{{#ifexpr: {{{age|}}} > 18
This means: If the "age" parameter is greater than 18.
| The character is an adult
When the above statement is true (or Yes), put "The character is an adult" in the page
| The character is '''not''' an adult
Otherwise, put "The character is not an adult" in the page.
}}
Ends the parser function.

Repeating, but:

If age= is greater than 18, add "The character is an adult" to the page. Otherwise, add "The character is '''not''' an adult" instead.

A better version of the box template[]

If you remember the box template on Help:Template parameters, it basically creates a simple box to display on a page. However, with the use of parser functions, it can be vastly improved.

Edit Template:Box so it has the following code:

<div style="background-color: {{{bgcolor|#DDD2}}}; border: 1px solid #5556; padding: 2em; text-align: center; width: 10em;">
{{#if: {{{title|}}}
| '''{{{title|}}}'''
}}
{{{text}}}
</div>

This now introduces the #if: parser function, which is used to check if a parameter exists.[note 1]

So, typing:

{{Box
|title = My title
|text = Explanatory text here
}}

will result in:

My title Explanatory text here

On the other hand, if this is typed:

{{Box
|text = Explanatory text here
}}

this will show up:

Explanatory text here

See that the bold title is gone? So, if you dont specify | title=, it will show nothing in the title.

Let's just say that we will remove the parser function:

<div style="background-color: {{{bgcolor|#DDD2}}}; border: 1px solid #5556; padding: 2em; text-align: center; width: 10em;">
'''{{{title|}}}'''
{{{text}}}
</div>

and not add a | title= parameter. This will show up:

' Explanatory text here

It looks broken, right?[note 2] So this is why ParserFunctions are great! It can help us do logic like this.

In short:

If the {{{title|}}} parameter is stated, output the title in bold.

Common parser functions[]

Now that you have learned what a parser function does and seen examples of it, we'll continue to the reference part.

Built-in[]

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

{{fullurl:}}
Returns the full URL of a given page.
{{fullurl: Help:Contents}}https://community.fandom.com/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[]

This extension is enabled by default on Fandom.

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!)

Some of them are even used in the examples in the previous sections!

Here is a subset of them; to view a complete list and full documentation on how to use them, please visit the extension's official help page.

#if:
{{#if: value to check | if value is not empty or whitespace | otherwise }}
{{#if: {{{1|true}}} | yes | no}} = yes
#ifeq:
{{#ifeq: a | b | a = b | a != b }}
{{#ifeq: {{{weapon|Axe}}} | Sword | You are prepared | You need a sword!}} = You need a sword!
#ifexist:
{{#ifexist: page | shows this if page exists | otherwise, this shows}}
#iferror:
{{#iferror: {{#time: qwerty}} | Error!}} = q4UTCThu, 14 Mar 2024 14:15:23 +00003124
#expr:
{{#expr: 1/6 round 5 }} = 0.16667
#switch:
{{#switch: sword | sword = weapon | axe = tool | #default = item}} = weapon
#time:
{{#time: g:i, j F Y (e) | 2000-01-10T02:00:30Z}} = 2:00, 10 January 2000 (UTC)
#titleparts:
{{#titleparts: Main Page/Top section/video | 1 | 2}} = Top section

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[]

Notes[]

  1. Technically, the #if: parser function checks if the first "parameter" is not empty. A string containing only white space is considered to be empty. This section is written that the main function doesn't exist, for the sake of simplicity.
  2. Note: "template" is intentionally broken. Removing the parser function will simply add a random ' before the body text

Further help and feedback[]