FANDOM


m (Further help and feedback: Removing interlanguage link)
Line 2: Line 2:
   
 
{{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 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.
   
 
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).
 
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).
Line 8: Line 8:
 
A list of all parser functions for use on a wiki is visible at [[:Special:Version]].
 
A list of all parser functions for use on a wiki is visible at [[:Special:Version]].
   
==Step-by-step use==
+
== Step-by-step use ==
 
<!-- 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. -->
 
<!-- 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. -->
#Identify the parser function that is appropriate for the task you are trying to do. There are 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 parser function that is appropriate for the task you are trying to do. There are 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>
+
# 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.
+
# 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 }}'''.
+
# 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.
 
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.
Line 19: Line 19:
 
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."
 
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."
   
==Built-in MediaWiki parser functions==
+
== 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.
 
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.
   
Line 39: Line 39:
 
:<code><nowiki>{{plural:2|is|are}}</nowiki></code> → {{plural:2|is|are}}
 
:<code><nowiki>{{plural:2|is|are}}</nowiki></code> → {{plural:2|is|are}}
   
==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 added a number of useful parser functions on top of the MediaWiki ones listed above. This extension is enabled by default across all of FANDOM. This extension also includes most of the parser functions found in the now deprecated extension [[mediawikiwiki:Extension:StringFunctions|StringFunctions]], most notably <tt><nowiki>{{#explode}}</nowiki></tt>.
 
The [[mediawikiwiki:Extension:ParserFunctions|ParserFunctions]] extension is a popular [[mediawikiwiki:Help:Extensions|MediaWiki extension]] that added a number of useful parser functions on top of the MediaWiki ones listed above. This extension is enabled by default across all of FANDOM. This extension also includes most of the parser functions found in the now deprecated extension [[mediawikiwiki:Extension:StringFunctions|StringFunctions]], most notably <tt><nowiki>{{#explode}}</nowiki></tt>.
Line 45: Line 45:
 
The ParserFunctions extension has functions for mathematical operations, if/else statements, and time functions. 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]].
 
The ParserFunctions extension has functions for mathematical operations, if/else statements, and time functions. 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]].
   
==Other Extensions==
+
== Other extensions ==
 
Quite a few other [[Help:Extensions|extensions]] that are popular on FANDOM add some parser functions. Here are some common ones.
 
Quite a few other [[Help:Extensions|extensions]] that are popular on FANDOM add some parser functions. Here are some common ones.
   
Line 56: Line 56:
 
So if an extension you are interested in has a parser function included, please consult that extensions page on [[mediawikiwiki:MediaWiki|MediaWiki.org]].
 
So if an extension you are interested in has a parser function included, please consult that extensions page on [[mediawikiwiki:MediaWiki|MediaWiki.org]].
   
==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]]
 
* [[Help:Extensions|Information about all extensions on FANDOM and what they do]]
 
* [[Help:Extensions|Information about all extensions on FANDOM and what they do]]
 
* [[m:Help:Parser function|General info about parser functions on Meta-Wiki]]
 
* [[m:Help:Parser function|General info about parser functions on Meta-Wiki]]
   
==Further help and feedback==
+
== Further help and feedback ==
 
 
{{Help and feedback section}}
 
{{Help and feedback section}}
   
Line 76: Line 76:
 
[[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]]
 
[[zh:Help:解析器函數]]
 
[[zh:Help:解析器函數]]
 
 
[[Category:Editing]]
 
[[Category:Editing]]
 
[[Category:Source editing]]
 
[[Category:Source editing]]

Revision as of 21:38, January 30, 2018

This extension is enabled by default on Fandom.

A parser function is a special piece of code in MediaWiki which extends the template functionality to perform logical operations. When rendered, parser functions use the parameters included in the definition to print out a specific result.

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 {{#if:}} function).

A list of all parser functions for use on a wiki is visible at Special:Version.

Step-by-step use

  1. Identify the parser function that is appropriate for the task you are trying to do. There are a wide variety of parser functions, so be sure to do your research.
    Below this step-by-step is extra information with links to help direct you to full-fledged documentation of each parser function.
  2. Identify the proper syntax for the parser function you wish to use.
    In this step-by-step guide, we will be using the {{#ifexpr:}} (if expression) parser function, which is enabled on each Community by default. Its syntax is:
    {{#ifexpr: expression | value if true | value if false }}
  3. Fill in the syntax.
    {{#ifexpr: 1 > 0 | yes | no }}
    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.
  4. 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.
    {{#ifexpr: 1 > 0 | yes | no }}yes.
    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.
    {{#ifexpr: 1 < 0 | yes | no }}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 within the infobox you have {{#ifexpr: {{{Age}}} >= 18 | This character is an adult. | This character is not an adult. }}, 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."

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 added a number of useful parser functions on top of the MediaWiki ones listed above. This extension is enabled by default across all of FANDOM. This extension also includes most of the parser functions found in the now deprecated extension StringFunctions, most notably {{#explode}}.

The ParserFunctions extension has functions for mathematical operations, if/else statements, and time functions. 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.

Other extensions

Quite a few other extensions that are popular on FANDOM add some parser functions. Here are some common ones.

So if an extension you are interested in has a parser function included, please consult that extensions page on MediaWiki.org.

See also

Further help and feedback

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