Help:Extension:解析器函數

This page is a translated version of the page Help:Extension:ParserFunctions and the translation is 98% complete.
Outdated translations are marked like this.

解析器函數 扩展提供11个額外的解析器函数以补充MediaWiki固有的“魔术字 ”。 (有可能会配置到额外的解析器函数以支持运行;这些字符串函数已在其他文档页面 记载。) 此扩展提供的所有解析器函数均采用以下形式:

{{#函数名: 参数1 | 参数2 | 参数3... }}
PD 注意:當您編輯本頁面時,即同意以CC0協議授權您的貢獻。您可以在公有領域帮助页面找到更多信息。 PD

#expr

关于a more in-depth manual on the finer points of how the expression evaluator works, including some additional operators not covered here,参见:Manual:Expr parser function syntax
类型 运算符号
分组(括弧) ( )
数字 1234.5   e (2.718)   pi (3.142)
二元运算符 e   一元运算符 +,-
一元 not ceil trunc floor abs exp ln sin cos tan acos asin atan sqrt
二元 ^
* / div mod fmod
+ -
取整 round
逻辑 = != <> > < >= <=
and
or

这个函数计算数学表达式并返回计算值。 在Scribunto 中可以通过mw.ext.ParserFunctions.expr来使用此函数。

{{#expr: 表达式 }}

Basic example

{{#expr: 1 + 1 }}2

右表依优先级列出了支持的运算符,运算符的详细说明请见[[Manual:Expr parser function syntax |帮助:计算]]。运算结果的精度和格式受运行wiki服务器操作系统和站点语言的数字格式影响可能存在差异。 The accuracy and format of the result returned will vary depending on the operating system of the server running the wiki and the number format of the site language.

使用布尔代数时,0表示false,其他任何非0数值(无论正负)均表示true

{{#expr: 1 and -1 }}1
{{#expr: 1 and 0 }}0
{{#expr: 1 or -1 }}1
{{#expr: -1 or 0 }}1
{{#expr: 0 or 0 }}0

空表达式返回空值,无效的表达式返回错误信息,使用#iferror函数检查错误:

{{#expr: }}
{{#expr: 1+ }}Expression error: Missing operand for +.
{{#expr: 1 = }}Expression error: Missing operand for =.
{{#expr: 1 foo 2 }}Expression error: Unrecognized word "foo".

置于数字前或数字后的加减号是有意义的,会被视为正负号而不会报错:

{{#expr: +1 }}1
{{#expr: -1 }}-1
{{#expr: + 1 }}1
{{#expr: - 1 }}-1

注意:使用魔术字输出数值时,首先要将其格式化以去除逗号,获得纯数字。 例如{{NUMBEROFUSERS}}得到17,986,221,这里我们想要17986221,可以用{{formatnum :{{NUMBEROFUSERS}}|R}}得到。 这在某些需要翻译数字的语言中尤为重要。 例如,在孟加拉语中,{{NUMBEROFUSERS}} 生成 ৩০,০৬১。

{{#expr:{{NUMBEROFUSERS}}+100}} Expression error: Unrecognized punctuation character ",".
{{#expr:{{formatnum:{{NUMBEROFUSERS}}|R}}+100}}17986321
  警告: 运算符mod會對第二參數的某些值給出錯誤的结果:
{{#expr: 123 mod (2^64-1)}}Division by zero. (輸出一個空字符串;應該是123)
如果你想要进行基于日期时间的计算(如检测当前日期时间是否超过指定日期时间),首先用{{#time: xNU }}将日期时间转换为距1970年1月1日(UTC)的秒数,再对秒数进行加减运算。

四舍五入

将左边的数字四舍五入为1/10的幂的倍数,指数等于右边给出的数字的截断值(truncated value)。

向上、向下取整分别使用ceilfloor

案例 结果 舍入方式
{{#expr: 1/3 round 5 }} 0.33333 最终的数字小于5,所以没有明显的取整发生 (0.333333… → 0.33333)
{{#expr: 1/6 round 5 }} 0.16667 最终的数字大于5,所以被向上取整 (0.166666… → 0.16667)
{{#expr: 8.99999/9 round 5 }} 1 同理,最后一位数因后一位而加1,进而引起了前面的进位 (0.999998… → 1.00000 → 1)
{{#expr: 1234.5678 round -2 }} 1200 舍入至最近的整百数,负值在小数点左边。
{{#expr: 1234.5678 round 2 }} 1234.57 舍入至最近的整1/100数,因为正值会舍入至小数点右边。
{{#expr: 1234.5678 round 2.3 }} 1234.57 舍入指数的小数点在舍入结果中不起作用
{{#expr: trunc 1234.5678 }} 1234 小数部分被全部舍弃(截断)
四舍五入至最近的整数
{{#expr: 1/3 round 0 }} 0 向下取整至0
{{#expr: 1/2 round 0 }} 1 向上取整至1
{{#expr: 3/4 round 0 }} 1 向上取整至1
{{#expr: -1/3 round 0 }} -0 向上取整至0
{{#expr: -1/2 round 0 }} -1 向下取整至-1
{{#expr: -3/4 round 0 }} -1 向下取整至-1
使用ceil和floor向上或向下取整
{{#expr: ceil(1/3) }} 1 向上取整至1
{{#expr: floor(1/3) }} 0 向下取整至0
{{#expr: ceil(-1/3) }} -0 向上取整至0
{{#expr: floor(-1/3) }} -1 向下取整至-1
{{#expr: ceil 1/3 }} 0.33333333333333 没有取整,因为1本来就是整数
  警告: 注意会被解析为(ceil 1)/3,而不是你想象中的ceil(1/3)
Rounding large numbers
{{#expr: 1e-92 round 400 }} 1.0E-92 Rounding to a very large number leads to infinity. Hence, the original value without the infinity is given as the answer.
{{#expr: 1e108 round 200 }} 1.0E+108 Same as above.

字符串

表达式函数只用于仅含有数字和运算符号的表达式,而字母字符串和其它符号不在此列。若需要比较字符串,可以用#ifeq替代。

{{#expr: "a" = "a" }}Expression error: Unrecognized punctuation character """.
{{#expr: a = a }}Expression error: Unrecognized word "a".
{{#ifeq: a | a | 1 | 0 }}1

#if

这个函数判断一个字符串是否为空。只包含空格的字符串被视为空字符串。

{{#if: 测试字符串 | 测试字符串非空时的取值 | 测试字符串为空或仅包含空白字符时的取值}}
{{#if: 参数1 | 参数2 | 参数3 }}

这个函数首先判断参数1是否为空。如果参数1不为空,则输出参数2。如果参数1为空或只包含空白内容(空格、换行等),则输出参数3。

{{#if: | yes | no}}no
{{#if: string | yes | no}}yes
{{#if: &nbsp;&nbsp;&nbsp;&nbsp; | yes | no}}no
{{#if: | yes | no}}no

字符串内容会被处理为纯文本,因此不会计算数学表达式(参见#ifexpr):

{{#if: 1==2 | yes | no }}yes
{{#if: 0 | yes | no }}yes

最后一个参数(false)可以忽略:

{{#if: foo | yes }} yes
{{#if: | yes }}
{{#if: foo | | no}}

该函数可嵌套。为了做到这一点,嵌套内层的#if函数需要按规则完整填写所有参数以符合#if的参数形式。一般至多可嵌套7层,该数也可能为wiki自身限制和内存限制而有变动。

{{#if:测试字符串
|字符串非空时的值
|{{#if:测试字符串
  |字符串非空时的值
  |字符串为空(或只有空格)的值
  }}
}}

字符串非空时的值

你可以在#if条件句中以字符串变量代替测试字符串。在这里,你需要在变量名的后面加上|(分隔符)以正确引用字符串。 (因此如果参数没有值,则计算结果为一个空字符串而非"{{{1}}}"。)

{{#if:{{{1|}}}|你在变量1里面输入了文本|变量1里面没有文本}}

参见幫助:模板中的解析器函数 以了解更多关于解析器函数中变量的相关例子。

#ifeq

这个函数判断两个輸入字符串是否相同,並根據結果輸出兩個字符串的其中一個。 如果需要更多的比較和輸出字符串,請考慮使用#switch

{{#ifeq: string 1 | string 2 | value if identical | value if different }}

如果两个字符串均为数字,则函数会进行数值的比较:

{{#ifeq: 01 | 1 | equal | not equal}}equal
{{#ifeq: 0 | -0 | equal | not equal}}equal
{{#ifeq: 1e3 | 1000 | equal | not equal}}equal
{{#ifeq: {{#expr:10^3}} | 1000 | equal | not equal}}equal

否则,函数会进行文本的比较(大小写敏感):

{{#ifeq: foo | bar | equal | not equal}}not equal
{{#ifeq: foo | Foo | equal | not equal}}not equal
{{#ifeq: "01" | "1" | equal | not equal}}not equal  (对比上面没有引号的例子)
{{#ifeq: 10^3 | 1000 | equal | not equal}}not equal  (对比上面带有#expr的例子,會先回傳一個有效的整數)

作为例子,考虑一个已存在的模板 Template:Timer,该模板利用解析器来選擇兩個標準時間,short和long。 它以参数作为第一个输入来比较字符串“short”–这没有约定顺序,但是如果参数在第一个则更容易理解。 模板代码定义为:

{{#ifeq: {{{1|}}} | short | 20 | 40 }}

会产生如下结果:

{{timer|short}}20
{{timer|20}}40
{{timer}}40
  警告: 任何的解析器标签和其它的解析器函数,用在解析器函数中时,都一定会被一个临时生成的独一无二的标识码 替代。这会影响如下比较:
{{#ifeq: <nowiki>foo</nowiki> | <nowiki>foo</nowiki> | equal | not equal}}not equal
{{#ifeq: <math>foo</math> | <math>foo</math> | equal | not equal}}not equal
{{#ifeq: {{#tag:math|foo}} | {{#tag:math|foo}} | equal | not equal}}not equal
{{#ifeq: [[foo]] | [[foo]] | equal | not equal}}equal
如果被比较的字符串由相等的模板 调用提供,且模板包含这些标签,则条件视为真,但如果是两个不同的模板,即使有包含这些内容的相同内容,也是视为假。
  警告: 随网站配置的变化,含有页面名字的魔术字 的字符串的字面比较可能会发生错误。 比如说,{{FULLPAGENAME}}这一魔术字随wiki的变化,可能在返回时使开头字母变成大写,也会用下划线替代空格。

为了解决这个问题,将这个魔术字应用到两个参数上:

{{#ifeq: {{FULLPAGENAME: L'Aquila}} | {{FULLPAGENAME}} | equal | not equal}}equal

#iferror

这个函数接收一个输入字符串,返回两个结果中的一个。如果输入字符串包含一个由其他解析器函数(比如#expr#time#rel2abs)、模板错误(比如模板循环和模板递归)或其他解析器“软错误”生成的class="error"的HTML对象,那么视为真。

{{#iferror: test string | value if error | value if correct }}

待返回字符串参数可以省略。若省略correct(正确)字符串参数,则在test string(测试字符串)不出错的情况下函数将返回被测字符串本身。若省略error(错误)字符串参数,则函数将在被测字符串出错时返回空字符串:

{{#iferror: {{#expr: 1 + 2 }} | error | correct }}correct
{{#iferror: {{#expr: 1 + X }} | error | correct }}error
{{#iferror: {{#expr: 1 + 2 }} | error }}3
{{#iferror: {{#expr: 1 + X }} | error }}error
{{#iferror: {{#expr: 1 + 2 }} }}3
{{#iferror: {{#expr: 1 + X }} }}
{{#iferror: {{#expr: . }} | error | correct }}correct
{{#iferror: <strong class="error">a</strong> | error | correct }}error

某些错误可能会导致添加一个追踪分类,使用{{#iferror:}}不会阻止添加此分类。

#ifexpr

此函数会判断数学表达式并根据其布尔值结果返回对应的字符串:

{{#ifexpr: expression | value if true | value if false }}

这里expression输入串将原封不动的作为上面#expr的参数进行调用,且表达式运算符是通用的,返回值也将作为布尔表达式进行处理。

输入表达式为空时将视为false

{{#ifexpr: | yes | no}}no

如上面所提,0将视为false,非零值将视为true,因此这个函数与下面的仅用#ifeq#expr的表述等价:

{{#ifeq: {{#expr: expression }} | 0 | value if false | value if true }}

除了下面这种情况:所输入表达式为空或者是一个错误表达式(空串会返回一条错误信息,而它不等于0,所以在后者我们会得到value if true)。

{{#ifexpr: = | yes | no }} Expression error: Unexpected = operator.

相对的

{{#ifeq: {{#expr: = }} | 0 | no | yes }} yes

两个返回值都可以省略,当合适的对应分支被省略时,函数不输出:

{{#ifexpr: 1 > 0 | yes }}yes
{{#ifexpr: 1 < 0 | yes }}
{{#ifexpr: 0 = 0 | yes }} yes
{{#ifexpr: 1 > 0 | | no}}
{{#ifexpr: 1 < 0 | | no}} no
{{#ifexpr: 1 > 0 }}

支持相等或不等的布尔运算。

{{#ifexpr: 0 = 0 or 1 = 0 | yes}}yes
{{#ifexpr: 0 = 0 and 1 = 0 | | no}}no
{{#ifexpr: 2 > 0 or 1 < 0 | yes}}yes
{{#ifexpr: 2 > 0 and 1 > 0 | yes | no}}yes
  警告: 使用#ifexpr进行数字比较的结果不总是与#ifeq#switch的结果一致。 后面两种比较相对于#ifexpr而言更加精确,因而未必返回相同结果。

考虑比较下面两个仅有最后一位数不同的数:

{{#ifeq: 12345678901234567 | 12345678901234568 | equal | not equal}}not equal
{{#switch: 12345678901234567 | 12345678901234568 = equal | not equal}}not equal

因为#ifeq#switch使用的PHP会比较两个整数类型的数,会正确返回预期结果。 然而用#ifexpr比较相同的两个数时:

{{#ifexpr: 12345678901234567 = 12345678901234568 | equal | not equal}}equal

对于不同的数字,结果实际上是不正确的。

造成#ifexpr出错行为的原因是MediaWiki将两个数依字面表达理解成了浮点数,而对于像这样的大数,转换为浮点数由于精度误差会存储为一样的数值,导致出错。

#ifexist

See Manual:Checking for page existence for other methods of checking if a page exists with different limitations

这个函数将一组字符串作为输入,并翻译成页面标题,然后根据在本地wiki上是否存在该页面而返回对应的值。

{{#ifexist: page title | value if exists | value if doesn't exist }}

只要页面存在就会判定为true(真值),即便那个页面看上去是空白的(比如像是分类链接或者是魔术字 解释页之类的却不包含任何可视内容的页面),或者是重定向页 ,或者它就是空白页。当且仅当页面是红链时判定为false(假值),包括那些曾经存在却被删除的页面。

{{#ifexist: Help:Extension:ParserFunctions/zh | exists | doesn't exist }}exists
{{#ifexist: XXHelp:Extension:ParserFunctions/zhXX | exists | doesn't exist }}doesn't exist

函数在系统消息 被自定义时返回true,对特殊页面 的判定则取决于本地软件自身。

{{#ifexist: Special:Watchlist | exists | doesn't exist }}exists
{{#ifexist: Special:CheckUser | exists | doesn't exist }}exists (因为Checkuser 扩展已经安装于此wiki)
{{#ifexist: MediaWiki:Copyright | exists | doesn't exist }}exists (因为MediaWiki:Copyright已自定义)

如果一个页面使用了#ifexist:来检查目标页面,则这个检查页面将出现在被检查页面的Special:WhatLinksHere里。所以如果本页面(Help:Extension:ParserFunctions/zh)使用了代码 {{#ifexist:Foo}},那么Special:WhatLinksHere/Foo将列出Help:Extension:ParserFunctions/zh。

若wiki有其在使用的对应的共享媒体库,#ifexist:就可用于检查一个文件是否在媒体库中,而不仅仅只是在wiki本体上检查:

{{#ifexist: File:Example.png | exists | doesn't exist }}doesn't exist
{{#ifexist: Image:Example.png | exists | doesn't exist }}doesn't exist
{{#ifexist: Media:Example.png | exists | doesn't exist }}exists

如果文件有一个已创建的对应的本地描述页面,上面的结果将全部是exists

#ifexist:不会对跨wiki链接起作用。

ifexist 限制

#ifexist:被视为“高开销(expensive)解析器函数”,每个页面调用这类函数的次数(包括包含于嵌入式模板的函数)存在一个限制。 当达到该限制时,更多的#ifexist:函数,无论其目标页面是否存在,只会自动返回错误值false,且该页面会被分类到Category:Pages with too many expensive parser function calls中。 追踪分类 的名稱可能因您的wiki内容的语言而异。

一些情况下,在css中使用选择器a.new(以选出链接到不存在的页面的链接)或者是a:not(.new)(以选出已存在页面的链接),是可以达到模仿ifexist的效果的。 另外,$wgExpensiveParserFunctionLimit 可以调整对高开销解析器函数数量的限制,如果有需要,也可以增加页面LocalSettings.php中的限制值。

ifexisit和需要的页面

一个不存在的页面被#ifexist检测后会被计数在待创建页面中。 请参阅工單T14019了解原因,并查看w:Template:Linkless exists以了解解决方法。

#rel2abs

这个函数将文件的相对路径转换为绝对路径。

{{#rel2abs: path }}
{{#rel2abs: path | base path }}

在输入项path中,可以使用以下类型的句法:

  • . → 本级路径
  • .. → 回到上一级
  • /foo → 向下一级进入子目录/foo

base path没有指定,将默认的填入函数所在页面的绝对路径:

{{#rel2abs: /quok | Help:Foo/bar/baz }}Help:Foo/bar/baz/quok
{{#rel2abs: ./quok | Help:Foo/bar/baz }}Help:Foo/bar/baz/quok
{{#rel2abs: ../quok | Help:Foo/bar/baz }}Help:Foo/bar/quok
{{#rel2abs: ../. | Help:Foo/bar/baz }}Help:Foo/bar

无效语法,如/././,将被忽略。 由于不允许超过两个连续的句号,因此可以使用诸如此类的序列来分隔连续的语句:

{{#rel2abs: ../quok/. | Help:Foo/bar/baz }}Help:Foo/bar/quok
{{#rel2abs: ../../quok | Help:Foo/bar/baz }}Help:Foo/quok
{{#rel2abs: ../../../quok | Help:Foo/bar/baz }}quok
{{#rel2abs: ../../../../quok | Help:Foo/bar/baz }}Error: Invalid depth in path: "Help:Foo/bar/baz/../../../../quok" (tried to access a node above the root node).

針對一個類似的函數群組,另見Help:Magic words#URL data。 內建的解析器函數包含:'localurl:', 'fullurl:', 'anchorencode:' 等等。

#switch

参见: w:Help:Switch parser function

该函数将一个输入值同若干个测试用例(test cases)做比较,如果找到匹配,返回有关联的字符串。

{{#switch: comparison string
 | case = result
 | case = result
 | ...
 | case = result
 | default result
}}

例如:

{{#switch: baz | foo = Foo | baz = Baz | Bar }} Baz
{{#switch: foo | foo = Foo | baz = Baz | Bar }} Foo
{{#switch: zzz | foo = Foo | baz = Baz | Bar }} Bar

含有部分嵌入包含标记的#switch会影响到能让不熟悉模板代码的编辑者查看和编辑可配置元素的配置文件。

默认

如果comparison string不能与任何case匹配,则返回default result

{{#switch: test | foo = Foo | baz = Baz | Bar }} Bar

在这种语法中,默认返回值必须是函数的最后一个参数,而且不能包含原始等号(不帶{{}}的等號)。 否则,就会解析为样例比较,无匹配的样例时不显示文本。 这是因为默认值没有定义(是空的)。 如果有样例匹配,则返回关联的字符串。

{{#switch: test | Bar | foo = Foo | baz = Baz }} →
{{#switch: test | foo = Foo | baz = Baz | B=ar }} →
{{#switch: test | test = Foo | baz = Baz | B=ar }} → Foo

你也可以将case字符串设为#default从而清楚地声明默认值。

{{#switch: comparison string
 | case = result
 | case = result
 | ...
 | case = result
 | #default = default result
}}

这种方式声明的默认结果可以放在函数的任何地方:

{{#switch: test | foo = Foo | #default = Bar | baz = Baz }} Bar

如果"default"参数被省略,且没有找到匹配,则不返回结果:

{{#switch: test | foo = Foo | baz = Baz }}

分组结果

该函数允许有“跌落”值,多个"case"字符串返回相同的"result"字符串,从而减少重复。

{{#switch: comparison string
 | case1 = result1
 | case2 
 | case3 
 | case4 = result234
 | case5 = result5
 | case6 
 | case7 = result67
 | #default = default result
}}

在本例中,第二、三、四个分支都会返回"result234",第六和第七个分支都会返回"result67"。 上述案例中,最后一个参数中的“#default = ”可以省略。

带有参数使用

该函数允许将参数用作测试字符串。 在本例中,不必将管道符放在参数名称后面,因为你不太可能需要选择字符串“{{{parameter name}}}”作为样例。 ((如果没有管道符,且参数不存在或没有值,则参数就会显示为这样。) 参见幫助:模板中的解析器函数 。)

{{#switch: {{{1}}} | foo = Foo | baz = Baz | Bar }}

上面的例子中,如果{{{1}}}等于foo,函数返回Foo。 如果等于baz,函数返回Baz。 如果参数是空的或者不存在,函数返回Bar

上面的这些中,也可以将样例组合起来以返回单个结果。

{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | Bar }}

如果{{{1}}}等于foozooroo,函数返回Foo。 如果等于baz,函数返回Baz。 如果参数是空的或者不存在,函数返回Bar

而且,如果你希望在测试参数不能匹配任何样例时不返回任何内容,可以省略默认结果。

{{#switch: {{{1}}} | foo = Foo | bar = Bar }}

在本例中,如果{{{1}}}存在且等于foobar,则分别返回FooBar,否则不返回任何内容。

这样做相当于将默认值声明为空。

{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | }}

如果由于某些原因需要将样例设置为{{{parameter name}}},则函数在参数不存在或没有值的情况下返回该样例的结果。 参数需要存在且拥有不同于字符串“{{{parameter name}}}”的值以返回函数的默认结果。

(若{{{1}}}不存在或者是空的):
{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }} Foo
(若{{{1}}}的值为test):
{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }} Bar
(若{{{1}}}的值为{{{1}}}):
{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }} Foo


在这样的例子中,你需要给参数添加管道符({{{1|}}})。

比较行为

如同#ifeq那样,若被比较字符串和测试用例字符串都是数字,那么按照数值进行比较;反之若存在一个非纯数字串,都会按照字符串比较规则进行:

{{#switch: 0 + 1 | 1 = one | 2 = two | three}} → three
{{#switch: {{#expr: 0 + 1}} | 1 = one | 2 = two | three}} → one
{{#switch: 02 | +1 = one | +2 = two | three}} → two
{{#switch: 100 | 1e1 = ten | 1e2 = hundred | other}} → hundred
{{#switch: a | a = A | b = B | C}} → A
{{#switch: A | a = A | b = B | C}} → C

case字符串可以是空的:

{{#switch: | = Nothing | foo = Foo | Something }}Nothing

只要找到一个匹配,后面的case都会忽略:

{{#switch: b | f = Foo | b = Bar | b = Baz | }}Bar
  警告: #switch#ifeq的数值比较结果未必一致(参见上面#ifeq部分):
{{#switch: 12345678901234567 | 12345678901234568 = A | B}} → B
{{#ifexpr: 12345678901234567 = 12345678901234568 | A | B}} → A

原始等号

“case”字符串不能包含原始等号。可以使用魔術字符{{=}}創建一個仅包含一个等号: $code1的模板$tpl去繞開它,或者是用HTML标识码&#61;来代替。

例如:

您输入的 您输出的
{{#switch: 1=2
 | 1=2 = raw
 | 1<nowiki>=</nowiki>2 = nowiki
 | 1{{=}}2 = template
 | default
}}
template
{{#switch: 1=2
 | 1&#61;2 = html
 | default
}}
html
为助您理解,您可以检查模板:NBA球队代表色Template:Extension/zh w:Template:BOTREQ是两个使用该函数的复杂例子。

替换#ifeq

#switch可以用于减少扩展深度

例如:

  • {{#switch:{{{1}}} |condition1=branch1 |condition2=branch2 |condition3=branch3 |branch4}}

等效于

  • {{#ifeq:{{{1}}}|condition1 |branch1 |{{#ifeq:{{{1}}}|condition2 |branch2 |{{#ifeq:{{{1}}}|condition3 |branch3 |branch4}}}}}}

也就是线性的深度迭代判断:

{{#ifeq:{{{1}}}|condition1
  |<!--then-->branch1
  |<!--else-->{{#ifeq:{{{1}}}|condition2
                |<!--then-->branch2
                |<!--else-->{{#ifeq:{{{1}}}|condition3
                              |<!--then-->branch3
                              |<!--else-->branch4}}}}}}

另一方面,对于嵌套在两个分支中的if(以缩进形式呈现,两侧都缩进),替换成switch可能很复杂/不切实际,从而形成对称树:

{{#ifeq:{{{1}}}|condition1
 |<!--then-->branch1t{{
  #ifeq:{{{1}}}|condition2
   |<!--then-->branch1t2t{{#ifeq:{{{1}}}|condition4|<!--then-->branch1t2t4t|<!--else-->branch1t2t4e}}
   |<!--else-->branch1t2e{{#ifeq:{{{1}}}|condition5|<!--then-->branch1t2e5t|<!--else-->branch1t2e5e}}
  }}
 |<!--else-->branch1e{{#ifeq:{{{1}}}|condition3
   |<!--then-->branch1e3t{{#ifeq:{{{1}}}|condition6|branch1e3t6t|branch1e3t6e}}
   |<!--else-->branch1e3e{{
    #ifeq:{{{1}}}|condition7
     |branch1e3e7t
     |branch1e3e7t
    }}
  }}
}}

#tag

Within spaces, tabs, and new lines, using the #tag:poem parser function, the formatting of your text is preserved with specific rules:

  • Single New Line: A single new line in your text will be preserved exactly as written, keeping line breaks intact.
  • Spaces and Tabs: Tabs or extra spaces at the start of a line or between words will be rendered as a single space.

For example:

Wikitext:

{{#tag:poem|abc    [[Sandbox|abc]] abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
def def def {{tc}} def def def def def def def def def def def def def def def def def def def def def def
ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi}}

Rendering:


abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
def def def in def def def def def def def def def def def def def def def def def def def def def def
ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi


In the example above, a tab after the first word "abc" and the new line after the last word "abc" are both rendered as a space.

By following these guidelines around spaces, tabs, and single new lines, you can control how your text appears when rendered with the #tag:poem function.

#time

该解析器函数接收一个(公历的)日期或者时间,并根据给定的语法将其格式化。可以指定日期/时间对象,默认则为魔术字Special:MyLanguage/Help:Magic words#Date and time的当前值——也就是说,页面最后被渲染为HTML时的时间。

{{#time: format string }}
{{#time: format string | date/time object }}
{{#time: format string | date/time object | language code }}
{{#time: format string | date/time object | language code | local }}

右表列举了支持的格式化代码。 任何不被识别的格式化字符串都不会被修改,这同样适用于空白字符(系统不使用空白字符来解释代码)。 If no character is recognized in the formatting string, and the date/time object is without error, then the formatting string is returned as output. 格式化字符串内有两种方法来转义字符:

  1. 反斜杠后跟随一个格式化字符串会被解释为单个原始的字符
  2. 用英文双引号括起来的字符视为原始字符,引号将被移除。

此外,xx会被解释为单个原始的x。

由于格式化代码列表会持续发展(支持新日历,或支持以不同方式计算和格式化的新日期字段),您应该转义所有需要传入而保持不变的文字字符(不仅仅是格式化代码当前使用的ASCII字母)。

不幸的是,目前,ASCII单引号仍未被视为当前已经受支持的ASCII双引号(可标记文字文本)和反斜杠(在许多语言使用的字符串常量中也必须转义,包括JSON、C、C++、PHP、JavaScript、Lua)的简单替代方法(例如,双引号在其他用途​​中是强制性的,例如JSON、C、C++等语言中的字符串值的定界)。 因此,您仍然无法在不使用反斜杠转义的情况下嵌入任何文字双引号(或者也可以使用其他弯引号、角形引号或方引号)。

{{#time: Y-m-d }}2024-09-01
{{#time: [[Y]] m d }}2024 09 01
{{#time: [[Y (year)]] }}2024 (24UTCamSun, 01 Sep 2024 01:23:55 +0000)
{{#time: [[Y "(year)"]] }}2024 (year)
{{#time: i's" }}23'55"

date/time object可以是任何PHP strtotime()函数接受的格式。 绝对(如20 December 2000)、相对(如+20 hours)和混合时间(如30 July +1 year)格式均是可以的。

{{#time: r|now}}Sun, 01 Sep 2024 01:23:56 +0000
{{#time: r|+2 hours}}Sun, 01 Sep 2024 03:23:56 +0000
{{#time: r|now + 2 hours}}Sun, 01 Sep 2024 03:23:56 +0000
{{#time: r|20 December 2000}}Wed, 20 Dec 2000 00:00:00 +0000
{{#time: r|December 20, 2000}}Wed, 20 Dec 2000 00:00:00 +0000
{{#time: r|2000-12-20}}Wed, 20 Dec 2000 00:00:00 +0000
{{#time: r|2000 December 20}}Error: Invalid time.

ISO 639-3(?)中的language code允许字符串显示为指定语言。

{{#time:d F Y|1988-02-28|nl}}28 februari 1988
{{#time:l|now|uk}}неділя
{{#time:d xg Y|20 June 2010|pl}}20 czerwca 2010

local参数指定date/time object是指本地时区还是UTC时间。

这是一个布尔参数,其值是通过转换(casting)参数值来确定的(有关如何将字符串转换为布尔值的详细信息,请参阅官方PHP文档)。

请注意,如果$wgLocaltimezone设为UTC,则当local设为truefalse时输出无区别。

参考下面的详细示例:

{{#time: Y F d H:i:s|now|it|0}}2024 settembre 01 01:23:57
{{#time: Y F d H:i:s|now|it|1}}2024 settembre 01 01:23:57
{{#time: Y F d H:i:s|+2 hours||0}}2024 9月 01 03:23:57
{{#time: Y F d H:i:s|+2 hours||1}}2024 9月 01 03:23:57
{{#time:c|2019-05-16T17:05:43+02:00|it}}2019-05-16T15:05:43+00:00
{{#time:c|2019-05-16T17:05:43+02:00|it|0}}2019-05-16T15:05:43+00:00
{{#time:c|2019-05-16T17:05:43+02:00|it|true}}2019-05-16T15:05:43+00:00

如果你已经计算了Unix时间戳,你可以通过添加@符号前缀来在日期计算中使用。

{{#time: U | now }}1725153837
{{#time: r | @1725153835 }}Sun, 01 Sep 2024 01:23:55 +0000
  警告: 若不在數字格式的時間戳值的前面包含@前綴,結果通常會出錯或是未預期的數值:
{{#time: r | 1970-01-01 00:16:39 }}Thu, 01 Jan 1970 00:16:39 +0000
{{#time: U | 1970-01-01 00:16:39 }}999
{{#time: r | @999 }}Thu, 01 Jan 1970 00:16:39 +0000 (正確)
{{#time: r | 999 }}Error: Invalid time. (不支援的年份格式)
{{#time: r | 1970-01-01 00:16:40 }}Thu, 01 Jan 1970 00:16:40 +0000
{{#time: U | 1970-01-01 00:16:40 }}1000
{{#time: r | @1000 }}Thu, 01 Jan 1970 00:16:40 +0000 (正確)
{{#time: r | 1000 }}Mon, 01 Sep 1000 00:00:00 +0000 (被解讀為某年的当前月份和日期)
{{#time: r | 1970-01-01 02:46:39 }}Thu, 01 Jan 1970 02:46:39 +0000
{{#time: U | 1970-01-01 02:46:39 }}9999
{{#time: r | @9999 }}Thu, 01 Jan 1970 02:46:39 +0000 (正確)
{{#time: r | 9999 }}Wed, 01 Sep 9999 00:00:00 +0000 (被解讀為某年的当前月份和日期)
{{#time: r | 1970-01-01 02:46:40 }}Thu, 01 Jan 1970 02:46:40 +0000
{{#time: U | 1970-01-01 02:46:40 }}10000
{{#time: r | @10000 }}Thu, 01 Jan 1970 02:46:40 +0000 (正確)
{{#time: r | 10000 }}Error: Invalid time. (不支援的年分格式)
  警告: 可接受的范围为0111年1月1日(1 January 0111)到9999年12月31日(31 December 9999)。对于年份100到110,输出不连贯,Y和闰年类似于年份100-110,而r、D、I和U可能会将这些年份解释为2000-2010。
{{#time: d F Y | 29 Feb 0100 }}01 3月 0100
(正確,沒有閏年),但是
{{#time: r | 29 Feb 0100 }}Mon, 01 Mar 0100 00:00:00 +0000 (錯誤,即使100也被解讀為2000,因为那是閏年)
{{#time: d F Y | 15 April 10000 }}Error: Invalid time.
{{#time: r | 10000-4-15 }}Sat, 15 Apr 2000 10:00:00 +0000

年份数字0-99解释为2000-2069和1970-1999,除非添加前缀0以写成四位数格式:

{{#time: d F Y | 1 Jan 6 }}01 1月 2006
{{#time: d F Y | 1 Jan 06 }}01 1月 2006
{{#time: d F Y | 1 Jan 006 }}01 1月 2006
{{#time: d F Y | 1 Jan 0006 }}01 1月 0006 (4-digit format)
年份100-110和1753之后的年份支持工作日,对于年份111-1752,r的输出显示“Unknown”,l输出“<>”,结果,这些年份不支持r输出。

可以指定完整的或部分绝对的日期;函数会使用“当前”值“填充”日期中未被指定的部分。

{{#time: Y | January 1 }}2024
  警告: “填充”功能并不连贯,有些部分会使用当前值填充,其他的则不会:
{{#time: Y m d H:i:s | June }}2024 06 01 00:00:00 给予这一天的开始,而不是当前月份中的日期和当前年份。
{{#time: Y m d H:i:s | 2003 }}2003 09 01 00:00:00 给予这一天的开始,而不是这一年的当前日期。

填充日期有特例:

{{#time: Y m d H:i:s | June 2003 }}2003 06 01 00:00:00 给予这一天的开始和这个月的开始。

4位數字總是被解讀為年分,而不是小時和分鐘:[1]

{{#time: Y m d H:i:s | 1959 }}1959 09 01 00:00:00

6位數字儘量被解讀為小時、分鐘、秒,不然會被解讀為錯誤(而不是年份和月份):

{{#time: Y m d H:i:s | 195909 }}2024 09 01 19:59:09 輸入被視為時間而不是年+月代碼。
{{#time: Y m d H:i:s | 196009 }}Error: Invalid time. 雖然19:60:09不是有效時間,但196009不被解讀為1960年9月。

該函數執行一定數量的日期算數:

{{#time: d F Y | January 0 2008 }}31 12月 2007
{{#time: d F | January 32 }}Error: Invalid time.
{{#time: d F | February 29 2008 }}29 2月
{{#time: d F | February 29 2007 }}01 3月
{{#time:Y-F|now -1 months}}2024-8月

#time調用的格式字符串的總長度限制為6000個字符[2]

时区问题

#time解析器函数有个bug(尤其是PHP DateTime),不允许传入非整数的相对时区偏移。这个问题不适用于使用基于小时的时区,例如EDT。例如:

  • {{#time:g:i A | -4 hours }} → 9:23 PM

但是,印度(India)是UTC +5.5个小时的时间偏移,因此使用时区不会正常地计算相对时区偏移。因此:

  • {{#time:g:i A | +5.5 hours }} → 1:23 AM

要解决这个问题,可将时间简单转换为分钟或者秒,像这样:

  • {{#time:g:i A | +330 minutes }} → 6:53 AM
  • {{#time:g:i A | +19800 seconds }} → 6:53 AM

(Tim Starling,该函数的开发者,提供了该解决方案的准确语法。)

#time format like in signatures

Sometimes it is useful to construct a timestamp, which looks like the automatic timestamp generated by signatures in discussions on talk pages. On an English-language wiki, it can be created with:

  • {{#timel:H:i, j xg Y (e)|+330 minutes}} → 06:53, 1 9月 2024 (UTC)

#timel

该函数是一个语法缩写,作用等价于将{{#time: ... }}local参数设置为true,因此它始终使用用户的首选时区或wiki的配置时区(在$wgLocaltimezone 中设置)。

该函数的语法为:

{{#timel: format string }}
{{#timel: format string | date/time object }}
{{#timel: format string | date/time object | language code }}
请注意,如果变量$wgLocaltimezone设为UTC,则当local设为truefalse时输出无区别。
 
使用#time和#timel解析器函数的示例,其中服务器处于非UTC时区

例如,参考以下示例:

{{#time:c|now|it}}2024-09-01T01:23:57+00:00
{{#time:c|now|it|0}}2024-09-01T01:23:57+00:00
{{#time:c|now|it|1}}2024-09-01T01:23:57+00:00
{{#timel:c|now|it}}2024-09-01T01:23:57+00:00
 
https://no.wikipedia.org/wiki/Maldiskusjon:Sommertid的警告範例
  警告: 注意对于time和timel,U会都返回自1970-01-01 00:00:00 UTC(即GMT)的秒数,无论时区是否为UTC。
U Unix时间。自1970年1月1日 00:00:00(GMT)以来的秒数。
Z 时区偏移(秒)。
{{#time: U}}1725153835
{{#timel: U}}1725153835
{{#time: Z}}0
{{#timel: Z}}0

#titleparts

此函数将页面标题根据斜杠划分成多个分段,然后输出返回这些分段中的部分。

{{#titleparts: 页面名称 | 需要返回的分段数量 | 要返回的第一个分段 }}

如果没有指定要返回的分段数量参数,则默认为“0”,会从要返回的第一个分段(包含)开始返回所有分段。如果要返回的第一个分段参数没有指定或者为“0”,则默认为“1”。

{{#titleparts: Talk:Foo/bar/baz/quok }}Talk:Foo/bar/baz/quok
{{#titleparts: Talk:Foo/bar/baz/quok | 1 }}Talk:Foo 参见{{ROOTPAGENAME }}。
{{#titleparts: Talk:Foo/bar/baz/quok | 2 }}Talk:Foo/bar
{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 2 }}bar/baz
{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 3 }}baz/quok
{{#titleparts: Talk:Foo/bar/baz/quok | 3 | 2 }}bar/baz/quok
{{#titleparts: Talk:Foo/bar/baz/quok | | 2 }}bar/baz/quok
{{#titleparts: Talk:Foo/bar/baz/quok | | 5 }}

两个值都接受负值。要返回的分段数量参数的负值会从字符串的最后开始“剥离”分段。要返回的第一个分段参数的负值解释为“从这一分段开始,从右边开始计数”:

{{#titleparts: Talk:Foo/bar/baz/quok | -1 }}Talk:Foo/bar/baz 从字符串的末尾剥离一段 参见{{BASEPAGENAME}}。
{{#titleparts: Talk:Foo/bar/baz/quok | -4 }} 从字符串的末尾剥离所有4段
{{#titleparts: Talk:Foo/bar/baz/quok | -5 }} 从字符串的末尾剥离5段(多于已存在的)
{{#titleparts: Talk:Foo/bar/baz/quok | | -1 }} quok 返回最后一段。 参见{{SUBPAGENAME}}。
{{#titleparts: Talk:Foo/bar/baz/quok | -1 | 2 }} bar/baz 从字符串的末尾剥离一段,然后返回第二段及以后的段
{{#titleparts: Talk:Foo/bar/baz/quok | -1 | -2 }} baz 从倒数第二个元素开始复制;从字符串的末尾剥离一段

处理前,页面名称参数是HTML解码的:如果包含一些标准的HTML字符实体,将被转换为纯字符(内部使用UTF-8编码,也就是与使用解析器函数的MediaWiki源页面中的编码相同)。

例如,页面名称中出现了&quot;&#34;&#x22;的地方,都会替换为"
不会执行从HTML到纯文本的其他转换,因此即使HTML标签在页面标题中无效,在此初始步骤中也会保持不变。
MediaWiki的一些魔术字或解析器函数(例如{{PAGENAME }}和类似)会返回用多余的HTML编码的字符串,即使其自身的输入参数不是HTML编码的:

titleparts解析器函数可以用作一个解决办法,以转换返回的字符串,因此可以被其他的一些解析器函数正确处理,如果这些解析器函数参数中也会接收页面名称,比如{{PAGESINCAT: }}(但对于HTML编码的字符串仍然不能正确处理)。

例如,如果当前页面是Category:Côte-d'Or,那么:

  • {{#ifeq: {{FULLPAGENAME}} | Category:Côte-d'Or | 1 | 0 }}{{#ifeq: {{FULLPAGENAME}} | Category:Côte-d&apos;Or | 1 | 0 }}都会返回1;(#ifeq解析器函数不会对输入参数进行HTML解码)。
  • {{#switch: {{FULLPAGENAME}} | Category:Côte-d'Or = 1 | #default = 0 }}{{#switch: {{FULLPAGENAME}} | Category:Côte-d&apos;Or = 1 | #default = 0 }}都会返回1;(#switch解析器函数不会对输入参数进行HTML解码)。
  • {{#ifexist: {{FULLPAGENAME}} | 1 | 0 }}{{#ifexist: Category:Côte-d'Or | 1 | 0 }}甚至{{#ifexist: Category:Côte-d&apos;Or | 1 | 0 }}都会返回1,如果这个分类页面存在(#ifexpr解析器函数不会对输入参数进行HTML解码);
  • {{PAGESINCAT: Côte-d'Or }}返回非零数,如果该分类包含页面或子分类,但是
  • {{PAGESINCAT: {{CURRENTPAGENAME}} }}可能会无条件地返回0,就像:
  • {{PAGESINCAT: {{PAGENAME:Category:Côte-d'Or}} }}
  • {{PAGESINCAT: {{PAGENAME:Category:Côte-d&apos;Or}} }}

这种非预料行为的原因是,对于当前版本的MediaWiki,有两个警告(caveats):

  • {{FULLPAGENAME}},或者甚至{{FULLPAGENAME:Côte-d'Or}}可能会返回实际HTML编码的字符串Category:Côte-d&apos;Or而不是预期中的Category:Côte-d'Or
  • {{PAGESINCAT: Côte-d&apos;Or }}无条件地返回0(PAGESINCAT魔术字不对输入参数进行任何的HTML解码)。

使用titleparts的简单解决方法(如果上述两个警告在MediaWiki的更高版本中修复了,也会继续生效):

  • {{PAGESINCAT: {{#titleparts: {{CURRENTPAGENAME}} }} }}
  • {{PAGESINCAT: {{#titleparts: {{PAGENAME:Category:Côte-d'Or}} }} }}
  • {{PAGESINCAT: {{#titleparts: {{PAGENAME:Category:Côte-d&apos;Or}} }} }},总是返回相同分类中的实际页面数量。

解码后的页面名称会尽可能标准化为MediaWiki支持的标准页面名称:

  1. 所有下划线都自动替换为空格:
    {{#titleparts: Talk:Foo/bah_boo|1|2}}bah boo 不是bah_boo,尽管原来的内容有下划线。
  2. 字符串最多分割25此;更多的斜杠都会忽略,第25个元素将会包含字符串的剩余内容。 字符串也限制在255个字符内,因为要被视为页面标题
    {{#titleparts: a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/aa/bb/cc/dd/ee | 1 | 25 }}y/z/aa/bb/cc/dd/ee
    如果出于某些原因确实要将此函数推到极限(尽管不太可能),可以通过嵌套函数调用来绕过只能分割25此的限制:
    {{#titleparts: {{#titleparts: a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/aa/bb/cc/dd/ee| 1 | 25 }} | 1 | 2}}z
  3. 最后,第一个子字符串根据本地wiki的大小写设置进行大写(如果该子字符串也以本地命名空间名称开头,则该命名空间名称也被标准化)。
    {{#titleparts: talk:a/b/c }}Talk:A/b/c
您可以把#titleparts用作一个小的“字符串解析器和转换器”,但要考虑到返回第一个子字符串会大写:
{{#titleparts: one/two/three/four|1|1 }}One
{{#titleparts: one/two/three/four|1|2 }}two

如果需要小写,使用lc:函数以控制输出:

{{lc: {{#titleparts: one/two/three/four|1|1 }} }}one

你可以在字符串开头添加一个“伪”斜杠以修正第一个子字符串的大小写。对于要返回的第一个分段,应使用2而不是1

{{#titleparts: /one/two/three/four|1|2 }}one
{{#titleparts: /One/two/three/four|1|2 }}One
  警告: 一些字符在页面标题中是非法的,会导致#titleparts不解析字符串。
{{#titleparts: {one/two} | 1 | 1 }}{one/two}. 不产生预期的: {one
{{#titleparts: [[page]]/123 | 1 | 2 }}page/123. 不起作用,因为页面标题中的中括号是非法的,并且此解析器函数不处理嵌入在其输入的页面名称参数中的链接,即使使用了MediaWiki语法或任何其他HTML或MediaWiki标签。
{{#titleparts: red/#00FF00/blue | 1 | 3 }} → "". 不起作用,因为“#”在页面标题中是非法的。
  警告: 如果标题中的某个部分只是“.”或“..”,那么#titleparts不会解析字符串:
{{#titleparts: one/./three | 1 | 1 }}one/./three. 返回整个字符串。不产生预期的: one
  警告: 如果输入超过255个UTF-8字节,则此函数不会正常降级(degrade gracefully)。如果输入字符串为256字节或更多,则返回整个字符串。

字符串函数

The ParserFunctions extension optionally defines various string functions (#len, #pos, #rpos, #sub, #count, #replace, #explode, #urldecode) if $wgPFEnableStringFunctions is set to true.

  警告: In 2013, it was decided that these functions will never be enabled on any Wikimedia wiki, because they are inefficient when used on a large scale (see phab:T8455 for some history). These functions do NOT work on Wikimedia wikis!

If you are here to write something on a Wikimedia project, you are looking for something else: if your home wiki has string functions, it probably uses Lua . For example, the English Wikipedia uses Module:String, which does some of the same things with wildly different syntax. There are also individual String-handling templates.

示例见

以下是Module:String函数的简要概述:

  • #len (字符串长度): {{#invoke:String|len|target_string}}
  • #sub (子字符串): {{#invoke:String|sub|target_string|start_index|end_index}}
  • #match: {{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
  • #pos (目标位置): {{#invoke:String|pos|target_string|index_value}}
  • #find: {{#invoke:String|find|source_string|target_string|start_index|plain_flag}}
  • #replace: {{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
  • #rep (重复): {{#invoke:String|rep|source|count}}
  • #escapePattern: {{#invoke:String|escapePattern|pattern_string}}
  • #count: {{#invoke:String|count|source_str|pattern_string|plain_flag}}
  • #join: {{#invoke:String|join|separator|string1|string2|...}}

See also Manual:Performing string operations with parser functions for a different set of hacks used to perform string functions when these are disabled, which were used on Wikimedia wikis before the Scribunto was developed.

一般帮助

替换引用

在井号前面添加subst:可以将字符串函数替换引用

{{subst:#ifexist: Help:Extension:ParserFunctions/zh | [[Help:Extension:ParserFunctions/zh]] | Help:Extension:ParserFunctions/zh }} → 将在文本中插入代码[[Help:Extension:ParserFunctions/zh]],因为Help:Extension:ParserFunctions/zh存在。
  警告: 如果表单时包含替换引用的可变代码,例如变量 或者其他解析器函数,那么被替换引用的解析器函数的结果是未定义的。为使结果恒定,需要运算的表达式中的所有易变代码都必须被替换引用。参见帮助:替换引用

‎<ref>‎</ref> 内的替换引用不起作用,如要使用,可以使用{{subst:#tag:ref|}}

重定向

特别地,{{#time:…|now-…}}可以方便地重定向到包含日期的页面,但这不起作用。

在表格中转义管道符

解析器函数会破坏wikitable 语法和管道字符(|),将所有的原始管道字符视为参数分隔符。 为了避免这个,很多wiki使用模板Template:!,其内容仅为一个管道符(|),自MediaWiki 1.24开始被{{!}} 魔术字 取代。 这会从MediaWiki解析器中“隐藏”管道符号,确保在展开页面上的所有模板和变量之前不考虑它。 然后将被解释为表格的行或列分隔符。 你也可以使用原始的HTML表格语法,尽管这并不直观而且容易出错。

对于需要显示的管道字符,可以使用HTML实体进行转义,显示为纯粹的、未被解释的字符:&#124;

说明 您输入的 您输出的
将管道字符转义为表格行/列分隔符
{{!}}
|
将管道字符转义为纯字符
&#124;
|

除去空白字符

空白字符(whitespace),包括换行符、制表符和空格,在这些解析器函数的所有参数的开头和结尾处,都会被除去。如果不希望这样,可以将字符串放在引号中后进行比较。

{{#ifeq: foo           |           foo | equal | not equal }}equal
{{#ifeq: "foo          " | "          foo" | equal | not equal }}not equal

要避免修整then和else部分,参见m:Template:If。一些人为达到目的,会使用<nowiki > </nowiki>而非空格。

foo{{#if:|| bar }}foofoobarfoo
foo{{#if:||<nowiki /> bar <nowiki />}}foofoo bar foo

然而,这种方法只能用于渲染单个空白字符,因为解析器函数会将一行中的多个空白字符挤压成一个。

<span style="white-space: pre;">foo{{#if:||<nowiki/>      bar      <nowiki/>}}foo</span>
foo bar foo

在本例中,white-space: pre样式用来强制浏览器保留空白,但即使使用它,也不会显示空格。发生这种情况是因为这些空格在发送到浏览器之前,就已经被软件除去了。

要解决此问题,可以将空白字符替换为&#32;可折行空格)或者&nbsp;不可折行空格),因为这些字符不会被软件修改:

<span style="white-space: pre;">foo{{#if:||&#32;&#32;&#32;bar&#32;&#32;&#32;}}foo</span>foo bar foo
foo{{#if:||&nbsp;&nbsp;&nbsp;bar&nbsp;&nbsp;&nbsp;}}foofoo   bar   foo

请注意,并非所有参数都是平等的。 在解析器函数中,开头和结尾的空格始终是忽略的。 在模板 中,命名参数和添加数字的非命名参数的开头和结尾的空格是被忽略的,但非命名参数的首尾空格不会被忽略:

foo{{1x|content= bar}}foofoobarfoo
foo{{1x|1= bar}}foofoobarfoo
foo{{1x| bar }}foofoo bar foo

Other parser functions

Case conversion functions

  • Lowercase: "{{lc: AbC}}" → "abc" [1]
  • Uppercase: "{{uc: AbC}}" → "ABC" [2]
  • Lowercase first character: "{{lcfirst: AbC}}" → "abC" [3]
  • Uppercase first character: "{{ucfirst: abc}}" → "Abc" [4]

Encoding functions

  • URL encoding:
"{{urlencode: AbC
dEf ghi}}"

renders as


"AbC%0AdEf+ghi"


So inner new lines convert into %0A, and inner spaces convert into +.

Anchor encoding

{{anchorencode: AbC dEf ghi}}

renders as


AbC_dEf_ghi


Padding functions

  • "{{padleft: bc d |8|a}}" gives "aaaabc d" [5]
  • "{{padright: bc d |8|a}}" gives "bc daaaa" [6]

Formatting functions

  • "{{formatnum: 1234567890}}" gives "1,234,567,890" [7]

參見

參考資料

  1. 在2011年r86805之前,情況並非如此。
  2. phabricator.wikimedia.org的ParserFunctions.php