FANDOM

47,925 Pages

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.

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.