Login.png
您还没有登录,请http://wiki.sosg.net/skins/monobook/user.gif点此登录后进行编辑。
如果您还没有在SOSG WIKI注册,请http://wiki.sosg.net/skins/monobook/user.gif点此注册
请注意SOSG WIKI的账号是不同于SOSG论坛账号的。


帮助:解析器函数

来自SOSG Wiki
跳转至: 导航搜索

25px 维基百科使用手册目录 · 關於 · 阅读 · 编辑 · 管理 · 开发 · 附錄 · 元-{zh-hans:帮助;zh-hant:幫助;zh-tw:說明;}-

模板:Meta

解析器函数是一个MediaWiki扩展,包含多个解析函数解释器。本扩展的典型语法是:

{{#函数名: 参数1 | 参数2 | 参数 3 …}}

目前有预定义的函数:exprififeqifexprswitchrand暂时被废除)。

各函数名都对大小写不敏感。

语句中的空格、换行等空白字符将被省略。

函数

expr

参见:Help:计算

expr函数,计算数学表达式。语法为:

{{ #expr: 表达式 }}

表达式支持的运算符有:

运算符 名称 优先级 元数 结合性 样例
+ 9 1 {{#expr: + 7}} = {{#expr: + 7}}
- 9 1 {{#expr: - 7}} = {{#expr: - 7}}
not 逻辑非 9 1 {{#expr: not 7}} = {{#expr: not 7}}
* 8 2 {{#expr: 30 * 7}} = {{#expr: 30 * 7}}
/ 8 2 {{#expr: 30 / 7}} = {{#expr: 30 / 7}}
div 8 2 {{#expr: 30 div 7}} = {{#expr: 30 div 7}}
mod 8 2 {{#expr: 30 mod 7}} = {{#expr: 30 mod 7}}
+ 6 2 {{#expr: 30 + 7}} = {{#expr: 30 + 7}}
- 6 2 {{#expr: 30 - 7}} = {{#expr: 30 - 7}}
round 舍入(最大9) 5 2 {{#expr: 30 / 7 round 7}} = {{#expr: 30 / 7 round 7}}
= 等于 4 2 {{#expr: 30 = 7}} = {{#expr: 30 = 7}}
< 小于 4 2 {{#expr: 30 < 7}} = {{#expr: 30 < 7}}
> 大于 4 2 {{#expr: 30 > 7}} = {{#expr: 30 > 7}}
<= 小于等于 4 2 {{#expr: 30 <= 7}} = {{#expr: 30 <= 7}}
>= 大于等于 4 2 {{#expr: 30 >= 7}} = {{#expr: 30 >= 7}}
<> 不等于 4 2 {{#expr: 30 <> 7}} = {{#expr: 30 <> 7}}
 != 不等于 4 2 {{#expr: 30 != 7}} = {{#expr: 30 != 7}}
and 逻辑与 3 2 {{#expr: 30 and 7}} = {{#expr: 30 and 7}}
or 逻辑或 2 2 {{#expr: 30 or 7}} = {{#expr: 30 or 7}}

round运算对运算数正负,位数正负都有不同的表现,参见下例。

逻辑运算符把假映射为0,把真映射为非0,且返回值只有0或1。

同一表达式中先计算高优先级运算。括号优先级高于一切。

if

if函数是一个if-then-else结构。语法是:

{{#if: <判断字符串> | <then字符串> | <else字符串> }}
{{#if: <判断字符串> | <then字符串> }}

若判断字符串为非空字符串(忽略前导或后缀空格),则函数返回then字符串,否则函数返回else字符串。else字符可被省略而不会造成错误,但函数在判断字符串为空时便会返回空字符串。

ifeq

ifeq比较两个字符串,返回比较结果。语法为:

{{#ifeq: <字符串1> | <字符串2> | <相等时返回的字符串> | <不相等时返回的字符串> }}

注意:两个空字符串是相等的。

示例:

假设模板Ifeq test的内容是

{{#ifeq:{{{lang}}}|zh|我使用中文|I don't use Chinese}}

则{{Ifeq test|lang=zh}}的结果是我使用中文;而{{Ifeq test|lang=en|I prefer English}}的结果是I don't use Chinese

ifexist

ifexist根據指定名稱的頁面是否存在,返回兩個參數中的一個。用法:

{{#ifexist: <待測頁面標題> | <存在文字> | <不存在文字> }}

示例:

  • {{#ifexist:test|有test頁面|無test頁面}} 得到{{#ifexist:test|有test頁面|無test頁面}}
  • {{#ifexist:User:Sex|該用戶存在|該用戶不存在}} 得到 {{#ifexist:user:sex|該用戶存在|該用戶不存在}}
  • {{#ifexist:维基百科||×}} 得到 {{#ifexist:维基百科||×}}

ifexpr

ifexpr计算数学表达式,并根据计算结果返回字符串。

{{ #ifexpr: <表达式> | <then字符串> | <else字符串> }}

若表达式经计算不为0,则函数返回then字符串,否则函数返回else字符串。表达式语法与expr<tt>相同。

switch

<tt>switch将一个值与多个预设值比较,若有匹配时则返回指定字符串,即双射。语法是:

{{ #switch: <比较值>
| <预设值1> [= <结果1>]
| <预设值2> [= <结果2>]
| ...
| <预设值n> [= <结果n>]
| [#default = ]<缺省结果> 
}}

switch将从左往右逐一尝试,直到出现匹配。函数将返回第一个匹配值对应的结果,而忽略后面的匹配值。如果没有匹配,函数将返回缺省结果。如果缺省结果没有设置,函数将返回空串。

注意:“缺省结果”是最后一个没有等号的预设值或“#default”预设值对应的结果;如果期望把一个包含“=”号的字符串作为缺省结果,则必须采用“#default”预设值形式。例如:

#default = <span style="color:red;">red</span>

switch也可用作满射(多对一,避免重复设置结果)。即某预设值后未设置结果,这样如果该预设值与比较值匹配,则函数返回第一个有结果的预设值的结果。例如:

{{ #switch: <比较值>
| <预设值1>
| <预设值2>
| <预设值3> = <结果3>
| <缺省结果>
}}

如果比较值与预设值1或预设值2匹配,都将返回结果3。注意:“#default”后必须有“=”,但其他预设值可以使用“#default”的结果。

字符串函数(暂时不可用)

字符串函数有:

函数名称 len pos rpos sub pad replace explode urlencode urldecode
用途 返回字符串长度                

All of these functions operate in O(n) time complexity, making them safe against DoS attacks.

Note:

  1. Some parameters of these functions are limited through global settings to prevent abuse. See section Limits below.
  2. For functions which are case sensitive, you may use the magic word {{lc:your_string_here}} as a workaround in some cases.

#len

#len函数返回所给字符串长度。语法是:

{{#len:string}}

返回的值总是字符串的字符总数(The return value is always a number of characters in the string)。如果没有字符串指定,返回值为0。( If no string is specified, the return value is zero.)

注意:

  • 字符串后空格讲被忽略:(Trailing spaces are not counted)例子:{{#len:Icecream }}返回8。
  • 函数接受UTF-8字符串(This function is safe with utf-8 multibyte characters)例子:{{#len:Žmržlina}}返回8。
  • 像<nowiki>的标签和其他标签的长度总是1。If one, since their content is hidden from the parser. Example: {{#len:<nowiki>This is a </nowiki>test}}返回5。

#pos:

#pos函数返回所给字符在字符串中出现的位置(function returns the position of a given search term within the string)语法是:

{{#pos:string|search term|offset}}

offset参数,指定函数查找的起始位置(if specified, tells a starting position where this function should begin searching)

如果search term被找到,返回值是一个zero-based integer of the first position within the string. If the search term is not found, the function returns an empty string.

注意:

  • 函数区分大小写。
  • The maximum allowed length of the search term is limited through the $wgStringFunctionsLimitSearch global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#pos:Žmržlina|lina}} returns 4.
  • As with #len, <nowiki> and other tag extensions are treated as having a length of 1 for the purposes of character position. Example: {{#pos:<nowiki>This is a </nowiki>test|test}} returns 1.

#rpos:

The #rpos function returns the last position of a given search term within the string. The syntax is:

 {{#rpos:string|search term}}

If the search term is found, the return value is a zero-based integer of its last position within the string. If the search term is not found, the function returns -1.

Tip: When using this to search for the last delimiter, add +1 to the result to retrieve position after the last delimiter. This also works when the delimiter is not found, because "-1 + 1" is zero, which is the beginning of the given value.

Notes:

  • This function is case sensitive.
  • The maximum allowed length of the search term is limited through the $wgStringFunctionsLimitSearch global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#rpos:Žmržlina|lina}} returns 4.
  • As with #len, <nowiki> and other tag extensions are treated as having a length of 1 for the purposes of character position. Example: {{#rpos:<nowiki>This is a </nowiki>test|test}} returns 1.

#sub:

The #sub function returns a substring from the given string. The syntax is:

{{#sub:string|start|length}}

The start parameter, if positive (or zero), specifies a zero-based index of the first character to be returned. Example: {{#sub:Icecream|3}} returns cream.

If the start parameter is negative, it specifies how many characters from the end should be returned. Example: {{#sub:Icecream|-3}} returns eam.

The length parameter, if present and positive, specifies the maximum length of the returned string. Example: {{#sub:Icecream|3|3}} returns cre.

If the length parameter is negative, it specifies how many characters will be omitted from the end of the string. Example: {{#sub:Icecream|3|-3}} returns cr.

Notes:

  • If the length parameter is zero, it is not used for truncation at all.
    • Example: {{#sub:Icecream|3|0}} returns cream.
  • If start denotes a position beyond the truncation from the end by negative length parameter, an empty string will be returned.
    • Example: {{#sub:Icecream|3|-6}} returns an empty string.
  • This function is safe with utf-8 multibyte characters. Example: {{#sub:Žmržlina|3}} returns žlina.
  • As with #len, <nowiki> and other tag extensions are treated as having a length of 1 for the purposes of character position. Example: {{#sub:<nowiki>This is a </nowiki>test|1}} returns test.
  • If you string contains a colon ("like:this"), removing just the text that precedes the colon has the effect of putting the remaining text indented on a new line. i.e. {{#sub:me:test|2|0}}.

#pad:

The #pad function returns the given string extended to a given width. The syntax is:

{{#pad:string|length|padstring|direction}}

The length parameter specifies the desired length of the returned string.

The padstring parameter, if specified, is used to fill the missing space. It may be a single character, which will be used as many times as necessary; or a string which will be concatenated as many times as necessary and then trimmed to the required length. Example: {{#pad:Ice|10|xX}} returns xXxXxXxIce.

If the padstring is not specified, spaces are used for padding.

The direction parameter, if specified, can be one of these values:

  • left - the padding will be on the left side of the string. Example: {{#pad:Ice|5|x|left}} returns xxIce.
  • right - the padding will be on the right side of the string. Example: {{#pad:Ice|5|x|right}} returns Icexx.
  • center - the string will be centered in the returned string. Example: {{#pad:Ice|5|x|center}} returns xIcex.

If the direction is not specified, the padding will be on the left side of the string.

The return value is the given string extended to length characters, using the padstring to fill the missing part(s). If the given string is already longer than length, it is neither extended nor truncated.

Notes:

  • The maximum allowed value for the length is limited through the $wgStringFunctionsLimitPad global setting.
  • This function is only partially safe with utf-8 multibyte characters. These characters will be treated appropriately if they appear in the original string, but will not be respected if they appear in the padding. Examples:
    {{#pad:Zmrzlina|12|z}} returns zzzzZmrzlina
    {{#pad:Žmržlina|12|z}} returns zzzzŽmržlina
    {{#pad:Žmržlina|12|ž}} returns žžŽmržlina
  • Tags such as <nowiki> and other tag extensions are not permitted in the padding. If the padstring contains such a tag, it will be truncated.

#replace:

The #replace function returns the given string with all occurrences of a search term replaced with a replacement term.

{{#replace:string|search term|replacement term}}

If the search term is unspecified or empty, a single space will be searched for.

If the replacement term is unspecified or empty, all occurrences of the search term will be removed from the string.

Notes:

  • This function is case sensitive.
  • The maximum allowed length of the search term is limited through the $wgStringFunctionsLimitSearch global setting.
  • The maximum allowed length of the replacement term is limited through the $wgStringFunctionsLimitReplace global setting.
  • Even if the replacement term is a space, an empty string is used. This is a side-effect of the MediaWiki parser. To use a space as the replacement term, put it in nowiki tags.
    • Example: {{#replace:My_little_home_page|_|<nowiki> </nowiki>}} returns My little home page.
    • Note that this is the only acceptable use of nowiki in the replacement term, as otherwise nowiki could be used to bypass $wgStringFunctionsLimitReplace, injecting an arbitrarily large number of characters into the output. For this reason, all occurrences of <nowiki> or any other tag extension within the replacement term are replaced with spaces.
  • This function is safe with utf-8 multibyte characters. Example: {{#replace:Žmržlina|ž|z}} returns Žmrzlina.
Case insensitive replace

Currently the syntax doesn't provide a switch to toggle case sensitivity setting. But you may make use of magic words of formatting (e.g. {{lc:your_string_here}} ) as a workaround. For example if you want to remove the word "Category:" from the string regardless of its case, you may type:

{{#replace:{{{1}}}|category:|}}

But the disadvantage is the output will become all lower cases. If you want to keep the casing after replacement, you have to use multiple nesting level (i.e. multiple replace calls) to achieve the same thing.

#explode:

The #explode functions splits the given string into pieces and then returns one of the pieces. The syntax is:

{{#explode:string|delimiter|position}}

The delimiter parameter specifies a string to be used to divide the string into pieces. This delimiter string is then not part of any piece, and when two delimiter strings are next to each other, they create an empty piece between them. If this parameter is not specified, a single space is used.

The position parameter specifies which piece is to be returned. Pieces are counted from 0. If this parameter is not specified, the first piece is used (piece with number 0). When a negative value is used as position, the pieces are counted from the end. In this case, piece number -1 means the last piece. Examples:

  • {{#explode:And if you tolerate this| |2}} returns you.
  • {{#explode:String/Functions/Code|/|-1}} returns Code.

The return value is the position-th piece. If there are less pieces than the position specifies, an empty string is returned.

Notes:

  • This function is case sensitive.
  • The maximum allowed length of the delimiter is limited through $wgStringFunctionsLimitSearch global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#explode:Žmržlina|ž|1}} returns lina.

#urlencode: and #urldecode:

These two functions operate in tandem: #urlencode converts a string into an URL-safe syntax, and #urldecode converts such a string back. The syntax is:

{{#urlencode:value}}
{{#urldecode:value}}

Notes:

  • These functions work by directly exposing PHP's urlencode() and urldecode() functions.
  • For anchors within a page use {{anchorencode}} instead of {{#urlencode}}. The results of a call to {{anchorencode}} are compatible with intra-page references generated with [[#link]] syntax, while {{#urlencode}}-generated values are not necessarily so.

time

time是一个时间日期格式化函数,它的语法为:

{{ #time: 格式参数 }}

或者

{{ #time: 格式参数 | 时间参数 }}


如果时间参数未指定,则使用该条目被转换为HTML的时间(值)。注意到由于缓存的缘故,这与条目被浏览的时间可能会有高达1星期的偏差。因此可能需要手工更新,方法是不作任何更改而保存该页面(一次“零编辑”)。

格式参数是一种格式字符,与在PHP的data中的用法相似。

下列格式代码与在PHP中的意义一样。所不同的是...


下表以北京时间2008年4月6日(星期日)10时02分05秒(UTC时间2008-04-06 02:03:05)为例说明各格式参数的作用。

格式参数 说明 显示结果
A 显示AM或PM 2008-4-6 02:03:05}}
a 显示am或pm 2008-4-6 02:03:05}}
c 显示长日期 2008-4-6 02:03:05}}
D 星期数,以一个汉字显示 2008-4-6 02:03:05}}
d 日期日数,有0补齐, 2008-4-6 02:03:05}}
F或M 月份 2008-4-6 02:03:05}}
G或g 当前UTC时间小时数,1位或2位数字 2008-4-6 02:03:05}}
H或h 小时数,2位数字 2008-4-6 02:03:05}}
i 分钟数,2位数字 2008-4-6 02:03:05}}
j 日数,2位数字 2008-4-6 02:03:05}}
L 日期星期数,1位数字,星期日为1,星期六为7 2008-4-6 02:03:05}}
l 日期星期数,1位汉字 2008-4-6 02:03:05}}
m 月份数,2位数字 2008-4-6 02:03:05}}
N 星期数,星期一为1,星期日为7 2008-4-6 02:03:05}}
n 月份数,1位或2位数字 2008-4-6 02:03:05}}
r 英文长日期格式 2008-4-6 02:03:05}}
s 秒数 2008-4-6 02:03:05}}
t 该月天数 2008-4-6 02:03:05}}
U 時間序號,1970-1-1 0:0:1为1 2008-4-6 02:03:05}}
W 日期周数,显示日期为当年第几周 2008-4-6 02:03:05}}
w 星期数,星期日为0,星期六为6 2008-4-6 02:03:05}}
Y 日期年份,4位数字 2008-4-6 02:03:05}}
y 日期年份,2位数字 2008-4-6 02:03:05}}
z 显示日期为当年第几周 2008-4-6 02:03:05}}

系统默认的时间参数为当前UTC+0时间,可以使用{{#time:参数|+8 hours}}得到当前北京时间(UTC+8时间)。

时间参数可以使用绝对时间,如“2008-12-31 23:59:59”,也可以使用相对时间,如“+7 days”或者“-5 hours”得到默认时间7日之后或默认时间5小时之前的时间。也可以二者混合使用,比如{{#time:Y-m-d H:i:s|2001-2-3 04:05:06 +1 year +2 months +3 days +4 hours +5 minutes +6 seconds}}返回 {{#time:Y-m-d H:i:s|2001-2-3 04:05:06 +1 year +2 months +3 days +4 hours +5 minute +6 seconds}}

使用xr可以在其后显示罗马数字,如{{#time:xrY年xrm月xrd日|2008-12-31}}显示为{{#time:xrY年xrm月xrd日|2008-12-31}}

language

#language得到指定語言代碼的該語言名稱。

{{#language:de}} 得到 Deutsch
{{#language:en}} 得到 English
{{#language:ja}} 得到 日本語
{{#language:nl}} 得到 Nederlands
{{#language:zh}} 得到 中文
{{#language:zh-cn}} 得到 中文(中国大陆)‎
{{#language:zh-tw}} 得到 中文(台灣)‎
{{#language:zh-hk}} 得到 中文(香港)‎
{{#language:zh-sg}} 得到 中文(新加坡)‎

subst

应用subst:到解析器函数,必须在subst:和#之间不能有空格,才可以正常工作。

表格

解析器函数中由于使用了“|”管道符做参数分隔符,所以不能包括表格所需要的“|”符。要想在输出中包含表格,可以通过以下两个办法达到:

  1. 通过嵌套模板来达到隐藏“|”的目的。比如:{{!}}
  2. 使用HTML语法。

参见

{{#switch:|subgroup|child=</div>|none=|#default=