FANDOM


Parser functions used in templates with parameters

#if function

The basic premise is, the function checks to see if a variable is not empty:

If (variable) IS NOT EMPTY then TRUE else FALSE

Named parameters

If the parameters are named then the if function takes the following form:

     {{#if: {{{x}}}
     | true
     | false
     }}
    

Seems, innocuous enough, but if used in a template then the very fact that you are using a variable will cause there to be something that exists in the template unless you swap it with an incoming parameter value (a non null value or null value) i.e., the variable is always there even though the parameter in the calling function may not be there. This means that all it is good for is checking whether the variable is empty / null:

     |x=3      => {{#if: 3 | true | false }} TRUE
    
     |x=       => {{#if:  | true | false }} FALSE 
    
               => {{#if: {{{x}}} | true | false }} TRUE
    
     |y=cheese => {{#if: {{{x}}} | true | false }} TRUE
     

The #if function can be made useful within the process of calling templates by using the "default value" operator |. This operator is a feature of calling templates; the | has different meanings outside of this context. Within the context of calling templates, it will work thusly: if there is nothing passed in to the variable before the | operator then whatever is after the | operator is "transcluded" (overwritten) in place of the code. If there is something in the parameter passed to the variable before the | operator it will use that. It will then go on to evaluate whatever has been "transcluded".

Examples

     If x=3, then passing it to a template with the following code {{{x|}}}, will transclude it to just 3, which will evaluate as 3.
     If x=, then passing it to a template with the following code {{{x|}}}, will see that x is empty and thus transclude it with the null after the | which evaluates as just null.
     If x is not among the parameters passed in to the template, then the following code {{{x|}}}, will see that x is empty so transclude the null again which evaluates as just null.
     If x=, then passing it to a template with the following code {{{x|"an arbitrary value used for this example"}}}, will see that x is empty and thus transclude it with "an arbitrary value used for this example" which will leave the template to evaluate the string.
     

Use in templates

     {{#if: {{{x|}}}
     | true
     | false
     }}
    

In the above code example ,the first thing that happens is that any parameter that exists, has its value passed in and is "transcluded" with x. If x does not exist then it will "transclude" the value after the |, which in this case is null. The null value now "transcludes" the variable. The #if function will now only pass TRUE if there is a parameter value other than null, passed to the variable:

     If x=3 => {{#if: {{{3|}}} | true | false }} => {{#if: 3 | true | false }} TRUE
    
     If x= => {{#if: {{{}}} | true | false }} => {{#if:  | true | false }} FALSE
    
     If => {{#if: {{{}}} | true | false }} => {{#if:  | true | false }} FALSE
    
     If y=cheese => {{#if: {{{}}} | true | false }} => {{#if:  | true | false }} FALSE
     

Practical use of the #if function

You can hide table rows which contain empty parameters by using:

     {{#if: {{{varInput|}}} | {{!}}- {{!}} {{{varInput}}} }}
     

The above code produces the following output only if varInput is defined and has a value.

|-
| varInput

Outside of the context of template variables/parameters, the | operator is a variable seperator or "OR" operator. So in the above example; the first | after "varInput" belongs to the template variable and is the "default" operator, the second | is a seperator for the #if statement (a third seperator could have been used if you need different output based on excluding the "varInput" parameter, but in this case it will output nothing).

The | is used in the above example instead of | as the "Parser function extension" would otherwise confuse it with the parameter seperators of the #if function. | is a template (which can be seen at Template:!) which transcludes (adds in) | into the code after the parser function has been validated.

What is |- ?

I'm not sure how this works as I can't find any documentation on it but this operator will allow you to test whether a parameter was specified in a template call:

     {{#ifeq: {{{v|}}} | {{{v|-}}}
     | v was specified (and may be empty)
     | v was not specified
     }}
     

Testing results:

If V=5 THEN true
If v= THEN true
If v=piece THEN true
If no variable THEN false
If u=5 THEN false


Logical functions

Details can be found for logical functions on the Mediawiki site

  • #expr
    • Rounding
  • #if
  • #ifeq
  • #iferror
  • #ifexpr
  • #ifexist
  • #rel2abs
  • #switch
    • Default
    • Grouping results
    • Comparison behaviour
    • Raw equal signs
  • #time
  • #timel
  • #titleparts


String functions

Details can be found on the Mediawiki site string functions 1 String operations

  • #len:
  • #pos:
  • #rpos:
  • #sub:
  • #pad:
  • #replace:
  • #explode:
  • #urlencode: and #urldecode:

2 Limits

  • $wgStringFunctionsLimitSearch
  • $wgStringFunctionsLimitReplace
  • $wgStringFunctionsLimitPad

Template Sandbox

The following template has been set up to test things like parser functions Template:ParserFunctions.


See also


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.