Help:Parser function

From QED

(Redirected from Help:Urlencode)
Jump to: navigation, search
Help
Related topics

In the context of MediaWiki markup, a parser function allows a template-like expression to be evaluated based on one or more function parameters.

For example, {{lc:AbCdEf}} yields the value: abcdef.

Since the colon is always used, parser functions are also affectionately known as colon functions.

Contents

Syntax

A parser function named "functionname" is invoked as though it were a template except that the initial separator is a colon, e.g.

{{functionname: parameter 1}}

Additional parameters are separated using the vertical bar "|", e.g.

{{functionname: parameter 1 | parameter 2}}

The function name is not case-sensitive. Many parser function names have a leading hash character "#".

Standard Parser Functions

LC

Converts text to lowercase.

{{lc:AbCdEf}} gives abcdef.
{{lc:Ä Β Ç}} gives ä β ç, expected ä β ç.

UC

Converts text to uppercase.

{{uc:AbCdEf}} gives ABCDEF.
{{uc:ä β ß}} gives Ä Β ß. Don't expect too much.

LCFIRST

Converts the first character to lowercase if required: {{lcfirst:Ab Cd}} gives ab Cd.

UCFIRST

Converts the first character to uppercase if required: {{ucfirst:aB cD}} gives AB cD.

PLURAL

See Help:PLURAL.

URLENCODE

Encodes the string into a format suitable for using as a URL by replacing spaces with "+" and other url formatting characters with their escaped equivalents: {{urlencode:hello world ?&=#/:}} gives hello+world+%3F%26%3D%23%2F%3A.

Warnings:

  • urlencode: encodes spaces as "+", not "_" or the generally supported "%20".
  • Both leading and trailing spaces are stripped:
{{urlencode:  @ !  }} yields %40+%21.
  • "/" is also encoded, e.g. {{urlencode:/path/file}} yields %2Fpath%2Ffile

The pair of characters "}}" must be encoded indirectly (e.g. using Template:))), and similarly for "{{" (using Template:((). "|" can be encoded using Template:!.

For example, to encode the string "{{ and }}", one could write {{urlencode:{{((}} and {{))}}}}, which produces %7B%7B+and+%7D%7D

NS

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}} QED Project
{{ns:5}} {{ns:project_talk}} QED_talk Project_Talk
{{ns:6}} {{ns:image}} Image Image
{{ns:7}} {{ns:image_talk}} Image_talk Image_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
Namespaces 100 and higher are customized by each site
{{ns:100}} {{ns:Princeton}} Princeton Princeton
{{ns:101}} {{ns:Princeton_talk}} Princeton_Talk Princeton_Talk

{{ns:{{ns:12}}_talk}} gives Help_talk.

Note: {{ns:{{SITENAME}}}} produces QED

URLs

{{localurl:fullpagename}} /main/Fullpagename
{{localurl:page|query=x}} /index.php?title=Page&query=x
{{localurl:a&b! c}}
{{localurle:a&b! c}}
/main/A%26b%21_c
/main/A%26b%21_c
{{urlencode:a&b! c}} a%26b%21+c
{{fullurl:fullpagename}} http://qed.princeton.edu/main/Fullpagename
{{fullurl:ab}} http://qed.princeton.edu/main/Ab
{{fullurl:a b}} http://qed.princeton.edu/main/A_b
{{fullurl:ab|u=v}} http://qed.princeton.edu/index.php?title=Ab&u=v
{{fullurl:m:a b|u=v}} http://qed.princeton.edu/index.php?title=M:a_b&u=v
{{fullurl:m:ab|u=v}} http://qed.princeton.edu/index.php?title=M:ab&u=v
{{fullurl:a b|u=v}} http://qed.princeton.edu/index.php?title=A_b&u=v
{{fullurl:a&b!|u=v}}  http://qed.princeton.edu/index.php?title=A%26b%21&u=v
{{fullurle:a&b!|u=v}} http://qed.princeton.edu/index.php?title=A%26b%21&u=v


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 !}} gives /main/A_%21. It is usually preferable to use the more concise fullurl: constructs. {{fullurl:page}} is a shorthand for {{SERVER}}{{localurl:page}}.

Warning: neither fullurl: nor localurl: encode spaces or special characters in the query-string parameter. This can result in broken URLs:

{{fullurl:m:Special:Contributions|target=Jimbo Wales&limit=10}}
http://qed.princeton.edu/index.php?title=M:Special:Contributions&target=Jimbo Wales&limit=10

With target={{urlencode:Jimbo Wales}}&limit=10 as query-string it works:

http://qed.princeton.edu/index.php?title=M:Special:Contributions&target=Jimbo+Wales&limit=10

"Hash" Parser Functions

The "hash" or "#" parser function package has been installed on QED. This package provides a wide variety of functions including:

  • #expr for arithmetic expressions
  • #if, #ifeq, #ifexist, #ifexpr, and #iferror for "logical" functions
  • #switch for string comparisons
  • #rel2abs and #titleparts for string manipulation
  • #time for time/date functionality

See ParserFunctions for documentation.

#language

{{#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 }} gives Dansk
{{ #language:fo }} gives Føroyskt
{{ #language:zh }} gives 中文
{{ #language:zh-min-nan }} gives Bân-lâm-gú
{{ #language:ang }} gives Anglo Saxon
{{ #language:tlh }} gives tlh (missing, incomplete list)
{{ #language:frr }} gives frr (missing, introduced 2006)
{{ #language:mw }} gives mw (no language code as of 2006)

#macro

The #macro parser function is intended for use in templates in which template variables must be expanded before XML-style tags are processed. See Help:macro for details.

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:

the name of a template {{ {{lc:ABC}}}} Template:Abc
the name of a variable {{ {{uc:currenttime}}}} 18:59
the name of a parser function {{ {{uc:lc}}:ABC}} abc
the name of a template parameter {{t2|{{#expr:3-1}}=a}} ( {{{1}}}, a )
the value of a template parameter {{t2|{{#expr:3-1}}}} ( 2, {{{2}}} )
a template parameter default value {{{1|{{#expr:7-2}}}}} 5
a template parameter definition ( name = value ) {{pab|{{lc:AB=CD}}}} {{{ab}}}
the name of a switch parameter {{#switch:2|{{#expr:3-1}}=a}} a
the value of a parser function parameter {{#ifeq:{{lc:AbC}}|abc|yes}} yes
a switch parameter definition ( name = value ) {{#switch:ab|{{lc:AB=CD}}}} cd
the target or label of an internal link [[{{lc:ABc}}|{{uc:ABc}}]] ABC
the target or label of an external link [http://{{lc:ABc}} {{uc:ABc}}] ABC

Parser functions cannot be used inside HTML-style extension tags, e.g.

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

produces

Failed to parse (lexing error): {{#expr:3-1}} \over 3


The workaround is to use #macro:

{{#macro:math|{{#expr:3-1}} \over 3 }}

produces 2 \over 3

Interpretation of a parser function as a template

In some cases an incorrect call to a parser function is interpreted as a valid invocation of a template. This can result in various anomalies, including anomalous entries in the templatelinks table.

When writing templates that use parser functions, it is therefore advisable to take certain precautions as described here.

Substitution

See also: Help:subst and Help:Substitution.

Applying "subst:" to a parser function works:

{{subst:LC:AbC}} gives abc

See also

Personal tools