FANDOM


(ParserFunctions extension: trying to make the relationship between string functions and parser functions a little clearer)
m (Further help and feedback: Mass editing of links)
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{enabled by default}}
 
{{enabled by default}}
 
 
{{Tocright}}
 
{{Tocright}}
A '''parser function''' is a special piece of code in [[Help:MediaWiki|MediaWiki]] which extends the [[Help:Template|template]] functionality to perform logical operations. When rendered, parser functions use the parameters included in the definition to print out a specific result.
+
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.
   
Parser functions are written out the same way templates are, 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).
+
For instance, instead of simply repeating the same statement on every page where the template <nowiki>{{tlx|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 list of all parser functions for use on a wiki is visible at [[:Special:Version]].
+
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.
   
== Step-by-step use ==
+
== Basic usage ==
<!-- Note, this section uses the examples from http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23ifexpr. Credit goes to the original authors of that page. -->
+
To ''parse'' something is merely to examine a statement for its logical components and then take action upon it. For instance:
# Identify the parser function that is appropriate for the task you are trying to do. There is a wide variety of parser functions, so be sure to do your research.<br />Below this step-by-step is extra information with links to help direct you to full-fledged documentation of each parser function.
 
# Identify the proper syntax for the parser function you wish to use.<br />In this step-by-step guide, we will be using the [[mediawikiwiki:Help:Extension:ParserFunctions#.23ifexpr|<code><nowiki>{{#ifexpr:}}</nowiki></code>]] (if expression) parser function, which is enabled on each Community by default. Its syntax is:<br /><tt><nowiki>{{#ifexpr:</nowiki> ''expression'' <nowiki>|</nowiki> ''value if true'' <nowiki>|</nowiki> ''value if false'' <nowiki>}}</nowiki></tt>
 
# Fill in the syntax.<br /><tt><nowiki>{{#ifexpr: 1 > 0 | yes | no }}</nowiki></tt><br />In the example above, we filled in the mathematical expression (1 > 0), set the function to say "yes" if 1 > 0 is mathematically correct and say "no" if 1 > 0 is mathematically incorrect.
 
# Save the page and verify that the function seems to be returning the expected value. In our example, "yes" should be returned because 1 is indeed greater than 0. <br /><tt><nowiki>{{#ifexpr: 1 > 0 | yes | no }}</nowiki></tt> → '''{{#ifexpr: 1 > 0 | yes | no }}'''.<br />Note what would have happened if we had tweaked the expression but kept the returned values the same. The equation proves false because 0 is not greater than 1 and "no" is returned.<br /><tt><nowiki>{{#ifexpr: 1 < 0 | yes | no }}</nowiki></tt> → '''{{#ifexpr: 1 < 0 | yes | no }}'''.
 
   
While the above example might seem silly, because one should always know 1 > 0, the power of the parser function is revealed when it is passed a parameter. Let's say you are designing an infobox for a wiki about a TV show. You want to include a character's age and determine if they are an adult or not.
+
:'''If''' this article's name is "Supergirl 23", '''then''' make the "next issue" field in the infobox be "Supergirl 24".
   
If within the infobox you have <code><nowiki>{{#ifexpr: {{{Age}}} >= 18 | This character is an adult. | This character is not an adult. }}</nowiki></code>, the code would say "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."
+
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 <code><nowiki>{{#if:}}</nowiki></code> 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:
  +
  +
:<tt><nowiki>{{#if:{{PAGENAME}}|Supergirl| |Go see [[Supergirl]]}}</nowiki></tt>
  +
  +
This means, "If the page I'm on is called 'Supergirl' (exactly), print nothing. Otherwise, print, 'Go see <nowiki>[[Supergirl]]</nowiki>'."
  +
  +
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 <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 ==
 
== Built-in MediaWiki parser functions ==
Line 37: Line 36:
 
== ParserFunctions extension ==
 
== ParserFunctions extension ==
 
[[File:Ifelselogic.png|thumb|The ParserFunctions extension relies on simple logic to return useful code.]]
 
[[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.
+
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!)
 
Parser functions can perform mathematical operations, evaluate if/else statements, and even manipulate time itself! (Well, the expression of time, anyway!)
Line 43: Line 42:
 
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]].
 
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 ===
 
=== 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!
+
[[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:
 
String functions are a series of specialised parser functions that work on text strings. They allow you to do things like:
Line 52: Line 51:
   
 
== Other extensions ==
 
== 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]].
+
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]].
 
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 ==
 
== See also ==
 
* [[Help:Magic words|Information about magic words, a related MediaWiki feature]]
 
* [[Help:Magic words|Information about magic words, a related MediaWiki feature]]
Line 63: Line 62:
 
== Further help and feedback ==
 
== Further help and feedback ==
 
{{Help and feedback section}}
 
{{Help and feedback section}}
 
 
[[de:Hilfe:ParserFunctions]]
 
[[de:Hilfe:ParserFunctions]]
 
[[es:Ayuda:Funciones parser]]
 
[[es:Ayuda:Funciones parser]]
Line 73: Line 71:
 
[[pt:Ajuda:Funções parser]]
 
[[pt:Ajuda:Funções parser]]
 
[[ru:Справка:Синтаксический анализатор]]
 
[[ru:Справка:Синтаксический анализатор]]
  +
[[tr:Yardım:Ayrıştırıcı işlevler]]
 
[[uk:Довідка:Синтаксичний аналізатор]]
 
[[uk:Довідка:Синтаксичний аналізатор]]
 
[[vi:Trợ giúp:Hàm phân tích cú pháp]]
 
[[vi:Trợ giúp:Hàm phân tích cú pháp]]

Revision as of 07:05, December 1, 2019

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

Community content is available under CC-BY-SA unless otherwise noted.