User:SuperDragonXD1/draft/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 paramaters, 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:FandomDestop", then add "You are now in the FandomDestop 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 be able to 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:

Now, it may look complicated, so a step-by-step guie is written below:


 * 1) The parser function is named as , which means "if equals". Now, 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 FandomDestop help page.", if the page name + namespace is equal to Help:FandomDesktop

Now, here is how you read the statement:
 * If the page name and namespace is equal to Help:FandomDesktop, output "You are now in the FandomDestop help page."

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


 * But I'm still confused!!!
 * Well, means: If the page is exactly named "Help:FandomDesktop", put "You are now in the FandomDestop help page." to the page


 * I still don't understand...
 * Well, here is a simplier guide to parser functions, which is a YouTube video . :)

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

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

Now, when you save 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:

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

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

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.