FANDOM


Help icon  Contents → 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 "|". The syntax can be written with or without a leading hash character (#):

{{functionname: argument 1 | argument 2 | argument 3...}}

for:

  • Core parser functions (standardly built into MediaWiki); this is for backwards compatibility; the function "language" is an exception; it is a core parser function, but in MessagesEn.php is specified that the wikitext name is "#language" anyway.
  • Optionally for parser function extensions.

or

{{#functionname: argument 1 | argument 2 | argument 3...}}
  • 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, and also no 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. Also the names DISPLAYTITLE and DEFAULTSORT are case-sensitive.

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

FormattingEdit

LCEdit

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

{{lc: AbC dEf }} yields abc def.
{{lc: Ä Β Ç }} yields ä β ç.

UCEdit

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

{{uc: AbCdEf }} yields ABCDEF.
{{uc:ä β ß}} yields Ä Β SS, expected: Ä Β SS. Don't expect too much.

LCFIRSTEdit

Makes the first character lowercase and removes newlines and spaces from the start and end: {{lcfirst: Ab Cd }} yields ab Cd.

UCFIRSTEdit

Makes the first character uppercase and removes newlines and spaces from the start and end: {{ucfirst: aB cD }} yields AB cD.

URLENCODEEdit

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: {{urlencode:hello world ?&=#/:}} yields hello+world+%3F%26%3D%23%2F%3A.

  • {{urlencode: "#$%&'()*,;?[]^`{}}} yields %22%23%24%25%26%27%28%29%2A%2C%3B%3F%5B%5D%5E%60%7B}
  • {{urlencode:<}} yields %3C (with the old preprocessor not the percent code %3C of the symbol itself, but the percent code of the HTML code &lt;)
  • {{urlencode:>}} yields %3E (with the old preprocessor not the percent code %3E of the symbol itself, but the percent code of the HTML code &gt;)
  • {{urlencode:{{!}}}} yields %7C ("|" needs to be given indirectly, using Template:!)

ANCHORENCODEEdit

For anchors within a page use {{anchorencode}} instead of {{urlencode}}. The results of a call to {{anchorencode}} are compatible with intra-page references generated for [[#link]]s, while {{urlencode}}-generated values are not necessarily so.

NSEdit

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

Code
with Number
Code
with Name
Returns
with Number
Returns
with Name
{{ns:-2}} {{ns:media}} Media Media
{{ns:-1}} {{ns:special}} Special Special
{{ns:0}} (not applicable) (not applicable)
{{ns:1}} {{ns:talk}} Talk Talk
{{ns:2}} {{ns:user}} User User
{{ns:3}} {{ns:user_talk}} User talk User talk
{{ns:4}} {{ns:project}} Memory Alpha Memory Alpha
{{ns:5}} {{ns:project_talk}} Memory Alpha talk Memory Alpha talk
{{ns:6}} {{ns:image}} File File
{{ns:7}} {{ns:image_talk}} File talk File talk
{{ns:8}} {{ns:mediawiki}} MediaWiki MediaWiki
{{ns:9}} {{ns:mediawiki_talk}} MediaWiki talk MediaWiki talk
{{ns:10}} {{ns:template}} Template Template
{{ns:11}} {{ns:template_talk}} Template talk Template talk
{{ns:12}} {{ns:help}} Help Help
{{ns:13}} {{ns:help_talk}} Help talk Help talk
{{ns:14}} {{ns:category}} Category Category
{{ns:15}} {{ns:category_talk}} Category talk Category talk
{{ns:102}} {{ns:portal}} Portal Portal
{{ns:103}} {{ns:portal_talk}} Portal talk Portal talk
{{ns:110}} {{ns:forum}} Forum Forum
{{ns:111}} {{ns:forum_talk}} Forum talk Forum talk

Also, for example, {{ns:{{ns:12}} talk}} yields Help talk.

NS:1 Edit

{{ns:{{ns:0}} talk}} works as well: Talk, as do the following constructs:

{{ns:01}} yields Talk, {{ns:+1}} yields Talk, {{ns:TALK}} yields Talk, and {{ns:{{ns:1}}}} also yields Talk. Note that the function is not case sensitive.

NS:0 Edit

#{{ns:0}}# yields ##, an empty string. This is often handy, less vulnerable than a template void or similar, and less cryptic than magic words like __END__.

In constructs like {{ {{#if: {{{T|}}} | {{{T}}} | ns:0}} }}, where the parameter T is normally the name of a template to be evaluated and can be undefined or empty, {{ns:0}} is then a clean empty string. Without it {{}} would result in {{}}.

Please note that {{ns:{{ns:0}}}} fails: . NS cannot handle an empty {{ns:0}} argument.

Edit

An empty section title like above is obtained using, in this case, ==== {{ns:0}} ====. This is dubious, but arguably better than using the same trick to get invisble section headers twice on a page. At least /* {{ns:0}} */ 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 __NOTOC__ 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:

  1. === __NOTOC__ ===
  2. === ===
  3. === &nbsp; ===
  4. === &#160; ===

URLs, etc.Edit

{{localurl:fullpagename}} /wiki/Fullpagename
{{localurl:page|query=x}} /wiki/Page?query=x
{{localurl:a&b! c}}
{{localurle:a&b! c}}
/wiki/A%26b!_c
/wiki/A%26b!_c
{{urlencode:a&b! c}} a%26b%21+c
{{fullurl:fullpagename}} https://memory-alpha.fandom.com/wiki/Fullpagename
{{fullurl:ab}} https://memory-alpha.fandom.com/wiki/Ab
{{fullurl:a b}} https://memory-alpha.fandom.com/wiki/A_b
{{fullurl:ab|u=v}} https://memory-alpha.fandom.com/wiki/Ab?u=v
{{fullurl:m:a b|u=v}} http://meta.wikimedia.org/wiki/a_b?u=v
{{fullurl:m:ab|u=v}} http://meta.wikimedia.org/wiki/ab?u=v
{{fullurl:a b|u=v}} https://memory-alpha.fandom.com/wiki/A_b?u=v
{{fullurl:a b|u=v w}} https://memory-alpha.fandom.com/wiki/A_b?u=v w
{{fullurl:a&b!|u=v}}  https://memory-alpha.fandom.com/wiki/A%26b!?u=v
{{fullurle:a&b!|u=v}} https://memory-alpha.fandom.com/wiki/A%26b!?u=v

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:

{{localurl:wikipedia:a}}
{{fullurl:wikipedia:a}}
http://en.wikipedia.org/wiki/a
http://en.wikipedia.org/wiki/a

The parser function localurl: replaces spaces by underscores and special characters by escape codes, e.g. {{localurl:a !}} yields /wiki/A_!. 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: constructs, {{fullurl:page}} is a shorthand for {{SERVER}}{{localurl:page}}.

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

Note that fullurl: and localurl: don't encode special characters and spaces in the optional query-string parameter. At least for spaces this will result in broken URLs:
{{fullurl:m:Special:Contributions|target=Jimbo Wales&limit=10}}
http://meta.wikimedia.org/wiki/Special:Contributions?target=Jimbo Wales&limit=10

With target={{urlencode:Jimbo Wales}}&limit=10 as query-string it works:
http://meta.wikimedia.org/wiki/Special:Contributions?target=Jimbo+Wales&limit=10

The new urlencode: encodes spaces as "+", not "_" or the generally supported "%20"; this might have interesting effects, so take care. Leading and trailing spaces are stripped, but all internal spaces are preserved: {{urlencode:  @  !  }} yields %40++%21.

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

ExampleEdit

Say you want to create a link in a template to the block log for blocked usernames: "{{fullurl:Special:Log/block|user=User:{{{1}}} }}". This won't work with spaces in the name like "Example blocked user" (unless they are manually underlined) as you'd get: "https://memory-alpha.fandom.com/wiki/Special:Log/block?user=User:Example blocked user", so what you can do is escape it with "{{fullurl:Special:Log/block|user=User:{{urlencode:{{{1}}}}} }}", giving you "https://memory-alpha.fandom.com/wiki/Special:Log/block?user=User:Example+blocked+user".

Also, the magic words for page name like {{PAGENAME}}, {{FULLPAGENAME}}, etc. all have "escaped" versions: {{PAGENAMEE}}, {{FULLPAGENAMEE}}, etc.

#language: Edit

{{#language: code}} 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.

{{#language:da}} yields Dansk
{{#language:fo}} yields Føroyskt
{{#language:zh}} yields 中文
{{#language:zh-min-nan}} yields Bân-lâm-gú
{{#language:ang}} yields Ænglisc
{{#language:tlh}} yields tlh (Klingonese)
{{#language:frr}} yields Nordfriisk (missing, introduced 2006)
{{#language:mw}} yields mw (no language code as of 2006)

Use of resultEdit

The result, like that of a variable or template, and like a template parameter value, can be used in the following ways:

*the name of a template: {{tl|{{lc:EXAMPLE}}}}
*the name of a variable: {{ {{uc:pagename}}}}
*the name of a parser function: {{ {{uc:lc}}:EXAMPLE}}
*the name of a template parameter: {{tl|{{#expr:3-2}}=Example}}
*the value of a template parameter: {{tl|Example|{{#expr:3-2}}}}
*a template parameter default value: {{{1|{{#expr:7-2}}}}}
*the name of a switch parameter: {{#switch:2|{{#expr:3-1}}=A}}
*the value of a parser function parameter: {{#ifeq:{{lc:ExAmPlE}}|example|yes}}
*the target or label of an internal link: [[Template:{{lc:EXAMPLE}}|{{uc:example}}]]
*the target or label of an external link: [http://memory-alpha.org/wiki/Template:{{lc:EXAMPLE}} {{uc:example}}]

gives:

  • the name of a template: {{example}}
  • the name of a variable: Parser function
  • the name of a parser function: example
  • the name of a template parameter: {{Example}}
  • the value of a template parameter: {{Example|1}}
  • a template parameter default value: 5
  • the name of a switch parameter: A
  • the value of a parser function parameter: yes
  • the target or label of an internal link: EXAMPLE
  • the target or label of an external link: EXAMPLE

The result cannot be used inside XML-style extension tags:

  • <math>{{#expr:3-1}} \over 3 </math> gives $ {{#expr:3-1}} \over 3 $

ExpansionEdit

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 {{#ifexpr:1|{{tl|example}}|{{bad example}}}} {{#ifexpr:0|{{bad example}}|{{tl|example}}}} giving {{example}} {{example}}. A page with this wikitext transcludes example only (with the old preprocessor also bad example).

Interpretation of a parser function as a templateEdit

In some cases a "wrong" parser function call is interpreted as a template call, which results in an entry in the templatelinks table, which in turn results in 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, for example, 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, such as:

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, for example:

  • {{ #if:{{{1|}}}|..{{ fullurl:Template:{{{1|}}} }}..}}
  • {{ #if:{{{1}}}|..{{ fullurl:Template:{{{1}}} }}..}} 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:{{{1}}}" 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". One remedy is:

  • {{ #if:{{{1|}}}|..{{ fullurl:Template:{{#if:{{{1}}}|{{{1}}}|[]}} }}..}} because "Template:fullurl:Template:[]" is not a valid page name.

SubstitutionEdit

Applying "subst:" to a parser function works:

{{subst:LC:AbC}} yields abc.

Note that unless a technique like optional recursive substitution is used, substituting a template which uses a parser function does not replace that parser function with its result.

See also Help: Substitution at MetaWiki and Manual: Substitution at MediaWiki.

See alsoEdit

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