Help:Parser functions

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

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:

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


 * 1) The parser function   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.
 * 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.

A template that checks if a character is an adult
This template uses the  parser function. It also details template parameters, too.

So, simply put this to the code:

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

Let's break it down step by step:


 * This means: If the "age" parameter is greater than 18.
 * This means: If the "age" parameter is greater than 18.


 * When the above statement is true (or Yes), put "The character is an adult" in the page
 * When the above statement is true (or Yes), put "The character is an adult" in the page


 * Otherwise, put "The character is not an adult" in the page.
 * Otherwise, put "The character is not an adult" in the page.


 * Ends the parser function.
 * Ends the parser function.

Repeating, but:
 * If  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:

This now introduces the  parser function, which is used to check if a parameter exists.

So, typing:

will result in:  My title Explanatory text here

On the other hand, if this is typed:

this will show up:  Explanatory text here

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

Let's just say that we will remove the parser function: and not add a  parameter. This will show up:   Explanatory text here It looks broken, right? So this is why ParserFunctions are great! It can help us do logic like this.

In short:
 * If the 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  with the following code:

This introduces the  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  link. It may vary, so if available, try clicking "create" or "create this documentation".

Then, replace all contents with:

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

You are in a template 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.


 * Returns the full URL of a given page.
 * Returns the full URL of a given page.


 * Converts a language code to the language name.
 * Converts a language code to the language name.


 * Makes a string lowercase (opposite of ).
 * → case
 * → case


 * Takes a namespace number and tells you what the namespace is.
 * Takes a namespace number and tells you what the namespace is.


 * If a given expression equals one, returns the singular value given. If a given expression equals two, returns the plural value given.
 * → 1 is
 * → 2 iss
 * → 2 iss

ParserFunctions extension


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.







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.

Further help and feedback
de:Hilfe:ParserFunctions es:Ayuda:Funciones parser fr:Aide:Fonctions parseur it:Aiuto:Funzioni parser ja:ヘルプ:パーサー関数 ko:도움말:파서 함수 pl:Pomoc:Funkcje parsera pt:Ajuda:Funções parser ru:Справка:Синтаксический анализатор tr:Yardım:Ayrıştırıcı işlevler uk:Довідка:Синтаксичний аналізатор vi:Trợ giúp:Hàm phân tích cú pháp zh:Help:解析器函數