Help:Parser function

A parser function is a 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 "|". Parser functions differ by a leading hash character (#):


 * 1) * Core parser functions (standardly built into MediaWiki); this is for backwards compatibility
 * 2) * Optionally for parser function extensions
 * 3) * By default for parser function extensions, e.g. ParserFunctions extension, to prevent collision with namespaces and interwiki prefixes
 * 1) * By default for parser function extensions, e.g. ParserFunctions extension, to prevent collision with namespaces and interwiki prefixes
 * 1) * By default for parser function extensions, e.g. ParserFunctions extension, to prevent collision with namespaces and interwiki prefixes

The distinction with variables is that the latter do not have any arguments which are separated by a colon.

For each language there is a file like MessagesEn.php specifying for each variable and parser function whether the name is case-sensitive.

Usually variables are case-sensitive and parser functions are not. To some variables ":R" can be added ("raw" modifier), but, although now a parser function, the name is still case-insensitive. Exceptions are the function names DISPLAYTITLE:, PAGESINNAMESPACE:, PAGESINNS:, and DEFAULTSORT: that are case-sensitive and the variable name CURRENTTIME that is case-insensitive.

MediaWiki's core parser functions are listed below and in Help:Magic words.

LC
Makes a text lowercase and removes newlines and spaces from the start and end.

abc def gives "abc def".

ä β ç gives ä β ç, expected ä β ç.

UC
Makes a text uppercase and removes newlines and spaces from the start and end.

ABCDEF gives "ABCDEF".

Ä Β SS gives Ä Β SS, expected: Ä Β SS. Don't expect too much.

LCFIRST
Makes the first character lowercase and removes newlines and spaces from the start and end: ab Cd gives "ab Cd".

UCFIRST
Makes the first character uppercase and removes newlines and spaces from the start and end: AB cD gives "AB cD".

URLENCODE
Removes newlines and spaces from the start and end, and converts the remaining text into URL-friendly format by replacing spaces with "+" and other url formatting characters with their escaped equivalents: gives.


 * urlencode: "#$%&'*,;?[]^`{}
 * urlencode:< (with the old preprocessor not the percent code %3C of the symbol itself, but the percent code of the HTML code &amp;lt;)
 * urlencode:> (with the old preprocessor not the percent code %3E of the symbol itself, but the percent code of the HTML code &amp;gt;)
 * ("|" needs to be given indirectly, using )

NB: For anchors within a page use

ANCHORENCODE
For anchors within a page use instead of. The results of a call to are compatible with intra-page references generated for  s, while  -generated values are not necessarily so.

Note that (contrary to urlencode) the encoding made by "anchorencode", which uses the dot charactor as a prefix before a byte repesented in hexadecimal, is not fully reversible. "anchorencode" is then NOT suitable for passing query parameters. However the encoding made by "urlencode" (which uses "%" characters) is not compatible with the more restricted character sets allowed in HTML and XML for ids. tHe two encodings also differ in the way they encode the space.

Compare:
 *  #  return: "#" (valid only for anchors to sections in MediaWiki pages), when
 *   returns: "" (for paths or query parameters in URLs, excluding anchors)

The encoding made by anchorencode is suitable to give anchor names within pages made with MediaWiki.

It is not guaranteed to work with other web sites: if you need to specify an anchor in a page not hosted on a server running MediaWiki, the anchor is not warrantied to work as expected. So, use "anchorencode" only for targetting a MediaWiki web site, such as those from the Xikimedia Foundation projects.

NS
Gives the namespace name for a given namespace number or name:

{| class="wikitable" ! Code with Number !! Code with Name !! Returns with Number !! Returns with Name ! colspan="4" align="center" | Namespaces 100 and higher are customized by each site
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * ( not applicable )
 * " gives "   ".
 * " gives "   ".
 * " gives "   ".
 * " gives "   ".
 * " gives "   ".
 * " gives "   ".
 * " gives "   ".
 * " gives "   ".

.

See Help:Variable for more canonical names like "project" and "talk".

NS:1
, just as ns:_talk. The canonical name for the talk namespace of articles is "talk" and not "_talk". Appending "_talk" to the namespace name works for all but the main namespace.

The following constructs work:
 * " " gives "   ".
 * ns:01.
 * ns:+1.
 * ns:TALK.

ns:0 gives null string
ns:0, an empty string. This is often handy, and less vulnerable to vandalism by a compromised administrator account than a template void or similar.

The cryptic magic word  __END__  is no longer supported. It formerly allowed for trailing whitespace to be included in the page save, but was removed in revision 19213.

In constructs like, where parameter T is normally the name of a template to be evaluated, but can be undefined or empty, is then a clean empty string. Without it  would result in  –.

" " gives "" .

, because NS cannot handle an empty  argument.

Empty or invisible section headers.
The wikitext line ===     === creates an entry in the TOC coded in the output HTML as &lt;li class="toclevel-2">&lt;a href="#">&lt;span class="tocnumber">2.4&lt;/span> &lt;/a>&lt;/li> attempting but failing to link to the null anchor &lt;p>&lt;a name="">&lt;/a>&lt;/p> and with an edit link similar to this: [&lt;a href="/w/index.php?title=Help:Parser_function&amp;amp;action=edit&amp;amp;section=12">edit&lt;/a>] The next line is. Note the edit link to the right.

An empty section title (with an edit link to the right) as above is obtained using, in this case,. This is dubious, but arguably better than using the same trick to get invisble section headers twice on a page. At least  /*&#160;   &#160;*/  makes sense in the edit history, if there's only one section using this particular trick.

As shown in the table of contents this and similar tricks result in non-functional links. In conjunction with  it's less harmful, and maybe useful to get edit links for invisible sections with categories and interlanguage links. Other possibilities for different invisible section headers:

URLs etc.
Note that with the interwiki prefix the format "wiki/" is used even for edit pages etc., while it does not write the page name in canonical form.

For interwiki links localurl and fullurl give the same:

The parser function localurl: replaces spaces by underscores and special characters by escape codes, e.g. {&#123;localurl:a !&#125;} gives. The main purpose was to create a link to a given page in external link style in the same project or a project which uses the same string in the URL between the server name and the page name. This function is now better served by less verbose fullurl:</tt> constructs,  is a shorthand for.

Both fullurle:</tt> and localurle:</tt> performed additional character escaping on the resulting link, but no example is known where that still has any additional effect.

Note that fullurl:</tt> and localurl:</tt> don't encode special characters and spaces in the optional query-string parameter. At least for spaces this will result in broken URLs:

 </tt>

With  target=&amp;limit=10 </tt> as query-string it works&#58;

The new urlencode:</tt> encodes spaces as "+</tt>", not "_</tt>" or the generally supported "%20</tt>", this might have interesting effects, take care. Leading and trailing spaces are stripped, but all internal spaces are preserved:  </tt> yields .

For obvious reasons encoding "&#125;}</tt>" or "|</tt>" cannot work directly. Don't encode "<tt>=</tt>", "<tt>&amp;</tt>", or "<tt>;</tt>" in structured query-strings unless you really want it.

Example
Say you want to create a link in a template to the block log for blocked usernames -> " ". This won't work with spaces in the name like "Example blocked user" (unless they are manually underlined) as you'd get: "'", so what you can do is escape it with: " ", giving you "'".


 * See also: bugzilla:5720

Also, the magic words for page name like   etc all have 'escaped' versions:    etc.

#language:
gives the local language name of selected RFC 3066 language codes, otherwise it returns the input value as is. For a complete list see the IANA registry.
 * (missing, incomplete list)
 * (missing, introduced 2006)
 * (no language code as of 2006)
 * (missing, incomplete list)
 * (missing, introduced 2006)
 * (no language code as of 2006)
 * (missing, introduced 2006)
 * (no language code as of 2006)

This function will replace and some of the Special:Prefixindex/Template:Lang name - templates. For actually supported alpha2 and alpha3 codes see, the 27*26*26 template calls may take some time.

Use of result
The result, like that of a variable or template, and like a template parameter value, can be used in the following ways: gives:
 * the name of a template:
 * the name of a variable:
 * the name of a parser function:
 * the name of a template parameter:
 * the value of a template parameter:
 * a template parameter default value:
 * the name of a switch parameter:
 * the value of a parser function parameter:
 * the target or label of an internal link: ABC
 * the target or label of an external link: [http://abc ABC]
 * the name of a template:
 * the name of a variable:
 * the name of a parser function:
 * the name of a template parameter:
 * the value of a template parameter:
 * a template parameter default value:
 * the name of a switch parameter:
 * the value of a parser function parameter:
 * the target or label of an internal link: ABC
 * the target or label of an external link: [http://abc ABC]

No longer possible with the new preprocessor: gives:
 * a template parameter definition ( name = value ):
 * a switch parameter definition ( name = value ):
 * a template parameter definition ( name = value ):
 * a switch parameter definition ( name = value ):

The result cannot be used inside XML-style extension tags:
 * $$ \over 3 $$ gives $$ \over 3 $$

Expansion
The first step in expanding any wikitext is identifying the templates, parser functions, variables and parameters from the braces, pipes and semicolons. For a parser function the next step is expanding the wikitext for the name of the parser function.

Suppose for example that it is #ifexpr. Then the next step is expanding the condition. Based on that either the wikitext for the then-part or that for the else-part is expanded.

This is illustrated with  giving. A page with this wikitext transcludes ta and td only (with the old preprocessor also tb and tc).

Interpretation of a parser function as a template
In some cases a "wrong" parser function call is interpreted as a template call, which results in an entry in the templatelinks table, giving an entry in the list of pages included in a page (on the edit box of the source page) and What links here of the target page.

This applies e.g. for localurl and fullurl if the parameter is not a valid page name, while "Template:localurl:" and "Template:fullurl:" followed by the parameter is. This is the case if the parameter is a namespace prefix followed by a colon, e.g.:
 * giving
 * giving
 * giving
 * giving

This entry in the templatelinks table occurs even if the result produced is not rendered due to a conditional parser function. This may occur in practice using e.g.:
 * in the case that the parameter is equal to the empty string; it does not occur if the parameter is undefined, because "Template:fullurl:Template:" is not a valid page name.
 * in the case that the parameter is equal to the empty string; it does not occur if the parameter is undefined, because "Template:fullurl:Template:" is not a valid page name.

Thus the list of templates transcluded in a page may be polluted with the entries of the non-existing template Template:fullurl:Template: and similarly for other namespace names instead of the second "Template", and for "localurl" instead of "fullurl". A remedy is e.g.:
 * because "Template:fullurl:Template:[]" is not a valid page name.

Substitution

 * See also: Help:Substitution.

Applying "subst:" to a parser function works:

gives abc.

Note that substituting a template which uses a parser function does not replace that parser function with its result. For full recursive substitution use Special:ExpandTemplates.

Terminology and history
Core parser functions were originally collectively unnamed in the MediaWiki codebase, although they were informally referred to simply as "functions" by developers. At some time around February 2006, editors of this documentation labelled them "colon functions", an unusual term which was unknown to the developers and the majority of users. In April 2006, the ParserFunctions extension was introduced, along with a new extension interface allowing efficient addition of arbitrary numbers of these entities, now labelled "parser functions". The syntax for the new parser functions was modelled on that of the old core parser functions, the main difference being the addition of the leading hash "#".

In July 2006, the parser function interface was extended to allow old-style hashless functions, and the old core parser functions were converted from using special-case code to the new parser function interface. They were moved to a CoreParserFunctions module. Thus today, there is no difference at all in syntax or functionality between core parser functions and extension parser functions, besides the leading hash convention.

Despite this, some users are in the habit of reserving the term "parser function" for members of the ParserFunctions extension. This is confusing and inaccurate -- any extension may define a parser function. The parser function interface has diverse applications. Perhaps it was confusing to call the extension ParserFunctions in the first place, but it was imagined as a fairly general repository of parser functions useful to Wikimedia.