Community Central
Community Central
No edit summary
mNo edit summary
(59 intermediate revisions by 33 users not shown)
Line 1: Line 1:
 
{{enabled by default}}
 
{{enabled by default}}
{{tocright}}
+
{{Tocright}}
  +
A '''parser function''' is a form of basic coding that happens within the [[Help:MediaWiki|MediaWiki]] language, [[Help:Wikitext|wikitext]]. It allows you to make [[Help:Templates|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 <nowiki>{{My Great Template}}</nowiki> 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]].
A "'''parser function'''" is a [[Help:Template|template]]-like construct which returns a value based on at least one unnamed parameter, separated from the function name by a colon "''':'''"; there may be more parameters which, like those of ordinary templates, are each preceded by "'''|'''".
 
   
  +
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.
Parser functions allow you to use a little bit of programming logic in your templates, including "if" statements and basic calculations.
 
   
  +
== Basic usage ==
There are many simple ones that are built into MediaWiki itself. Some extensions also add a few for specific tasks. There is a large collection of commonly used ones by the name of [[mw:Extension:ParserFunctions|ParserFunctions]] which Wikia has enabled by default on all wikis. Another example of an extension adding parser functions is [[Help:StringFunctions|StringFunctions]], which adds string manipulation functions.
 
  +
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".
==Parser functions in action==
 
You can hide table rows which contain empty parameters by using a variation of:
 
   
  +
That's a parser function.
<pre><nowiki>{{#if: {{{input|}}} | {{!}}-
 
{{!}} {{{input}}} }}</nowiki></pre>
 
   
  +
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 <code><nowiki>{{#if:}}</nowiki></code> function).
Produces the following code only if input is defined.
 
   
  +
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:
<pre>
 
|-
 
| input
 
</pre>
 
   
  +
:<tt><nowiki>{{#if:{{PAGENAME}}|Supergirl| |Go see [[Supergirl]]}}</nowiki></tt>
<nowiki>{{!}}</nowiki> is used in the above parser function instead of | as the extension uses | to separate parameters. <nowiki>{{!}}</nowiki> is a template at Template:! which transcludes | into the code after the parser function has been validated.
 
   
  +
This means, "If the page I'm on is called 'Supergirl' (exactly), print nothing. Otherwise, print, 'Go see <nowiki>[[Supergirl]]</nowiki>'."
==See also==
 
* [[m:Help:Parser function|General info about parser function on Meta-Wiki]]
 
* [[mw:Extension:ParserFunctions|Information about the ParserFunctions extension]]
 
** [[mw:Help:Extension:ParserFunctions|Syntax for ParserFunctions functions]]
 
* [[Help:StringFunctions]]
 
   
  +
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.
[[de:Hilfe:Parser-Funktionen]]
 
  +
  +
By having <code><nowiki>{{#ifexpr: {{{Age}}} >= 18 | This character is an adult. | This character is not an adult. }}</nowiki></code>, 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 [[mediawikiwiki:Help:Magic words#Parser functions|here on MediaWiki.org]]. This page will illustrate the most useful.
  +
  +
;<code><nowiki>{{fullurl:}}</nowiki></code>
  +
:Returns the full URL of a given page.
  +
:<code><nowiki>{{fullurl:Help:Contents}}</nowiki></code> → {{fullurl:Help:Contents}}
  +
;<code><nowiki>{{#language:}}</nowiki></code>
  +
:Converts a language code to the language name.
  +
:<code><nowiki>{{#language:es}}</nowiki></code> → {{#language:es}}
  +
;<code><nowiki>{{lc:}}</nowiki></code>
  +
:Makes a string lowercase (opposite of <code><nowiki>{{uc:}}</nowiki></code>).
  +
:<code><nowiki>{{lc:CASE}}</nowiki></code> → {{lc:CASE}}
  +
;<code><nowiki>{{ns:}}</nowiki></code>
  +
:Takes a [[Help:Namespace|namespace number]] and tells you what the namespace is.
  +
:<code><nowiki>{{ns:4}}</nowiki></code> → {{ns:4}}
  +
;<code><nowiki>{{plural:}}</nowiki></code>
  +
:If a given expression equals one, returns the singular value given. If a given expression equals two, returns the plural value given.
  +
:<code><nowiki>{{plural:1|is|are}}</nowiki></code> → {{plural:1|is|are}}
  +
:<code><nowiki>{{plural:2|is|are}}</nowiki></code> → {{plural:2|is|are}}
  +
  +
== ParserFunctions extension ==
  +
[[File:Ifelselogic.png|thumb|The ParserFunctions extension relies on simple logic to return useful code.]]
  +
The [[mediawikiwiki:Extension:ParserFunctions|ParserFunctions]] extension is a popular [[mediawikiwiki:Help:Extensions|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 [[mediawikiwiki:Help:Extension:ParserFunctions|the extension's official help page]].
 
=== String functions ===
  +
[[mediawikiwiki:Extension:StringFunctions|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 [[Help:Extensions|extensions]] that are popular on Fandom add their own parser functions. Two of the more common are [[mediawikiwiki:Extension:Arrays|Arrays]] and [[mediawikiwiki:Extension:Variables|Variables]].
  +
  +
To find out more about these, please consult that extension's page on [[mediawikiwiki:MediaWiki|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 ==
  +
* [[Help:Magic words|Information about magic words, a related MediaWiki feature]]
  +
* [[Help:Extensions|Information about all extensions on Fandom and what they do]]
 
* [[m:Help:Parser function|General info about parser functions on Meta-Wiki]]
  +
  +
== Further help and feedback ==
  +
{{Help and feedback section}}
 
[[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:解析器函數]]
  +
[[Category:Editing]]
  +
[[Category:Source editing]]
 
[[Category:Help|{{PAGENAME}}]]
 
[[Category:Help|{{PAGENAME}}]]
[[Category:Expand|{{PAGENAME}}]]
+
[[Category:Tools & shortcuts|{{PAGENAME}}]]

Revision as of 04:52, 23 September 2020

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 {{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}}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

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