Community Central

READ MORE

Community Central
Advertisement

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

A namespace-dependent template

Now, let us create a basic template, that changes behavior depending on the namespace.

Create Template:Namespace checker with the following code:

{{#switch: {{{demospace|{{{1| {{NAMESPACE}} }}}}}}
| Template = You are in a template page.
| File     = You are now in an image, video, or file page
| Category = You are now in a Category page. 
| #default = You are in an article page.
}}<noinclude>
{{Documentation}}
</noinclude>

This introduces the #switch: parser function, which switches depending on the values passed.

Alongside that, it introduces template documentation, which can be used to tell people how to use the template.

After saving the page, click the Template:Namespace checker/doc link. It may vary, so if available, try clicking "create" or "create this documentation".

Then, replace all contents with:

; Description
: This template shows different outputs depending on namespace
:
; Syntax 
: {{t|Namespace checker|optional namespace}}
:
; Sample output
: {{t|Namespace checker}}{{Namespace checker}}
:
: {{t|Namespace checker|File}}{{Namespace checker|File}}

Save the page, and go back to the main template page. It should look something similar to:

You are in a template page.

Documentation
Description
This template shows different outputs depending on namespace
Syntax
{{Namespace checker|optional namespace}}
Sample output
{{Namespace checker}} → You are in a template page.
{{Namespace checker|File}} → You are now in an image, video, or file page.

Congratulations! You have created a template that uses parser functions, and has documentation!

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 }}
#ifeq:
{{#ifeq: a | b | a = b | a != b }}
#ifexist:
{{#ifexist: page | shows this if page exists | otherwise , this shows}}

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

Advertisement