帮助:解析器函数

来自心百科,心灵的百科全书

解析器函数(ParserFunction)是MediaWiki系统中的重要组成部分,这是MediaWiki系统区别于其他维基类软件和CMS系统(例如WordPress)的最大不同之一。解析器函数简单来说就是通过特定的语法来({{#}})调用PHP的底层函数,进行逻辑表达或是运算,实现一些复杂的功能和效果。典型语法是:

{{#函数名: 参数1 | 参数2 | 参数3 …}}
  • 各函数名都对大小写不敏感。
  • 语句中的空格、换行等空白字符将被省略。
  • 解析器函数实际上是MarkUp维基语言的一部分,同样需要由MediaWiki的解析器进行转译(Parser),因此运行效率受到一定限制。
    • 由Module提供的Lua脚本可以更高效的执行函数逻辑,可以全面取代解析器函数。

扩展

MediaWiki的解析器函数全部由扩展提供,扩展Extension:ParserFunctions现在是MediaWiki的基础部分,带有预设的基本函数:exprififeqifexpriferrorrel2abstitlepartsswitch等。

其他函数需要通过特定的插件提供,有些函数例如Loop循环等出于性能考虑灰机不予提供支持,可以通过Lua脚本来实现。

基础函数

expr

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

{{ #expr: 表达式 }}

表达式支持的运算符有:

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

取舍数位

round 舍入(最大9) {{#expr: 30 / 7 round 7}} = 4.2857143
ceil 向上取整 {{#expr: ceil(1/3) }} = 1
floor 向下取整 {{#expr: floor(1/3) }} =0

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

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

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

if

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

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

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

if函数还可用在模板语法中检查某个参数是否被使用。若满足条件,可在调用此模板的页面中显示或不显示特定内容。

{{#if:{{{1|}}}|匿名参数1不为空时输出的结果|匿名参数1为空时输出的结果}}
{{#if:{{{xx|}}}{{{yy|}}}|xx或yy参数不为空时输出的结果|xx和yy参数都为空时输出的结果}}

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页面}} 得到 有test页面
  • {{#ifexist:User:Sex|該用戶頁存在|該用戶頁不存在}} 得到 该用户不存在
  • {{#ifexist:心百科||×}} 得到 ×

ifexpr

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

{{ #ifexpr: <表达式> | <表达式不为0的结果> | <表达式为0的结果> }}

表达式语法与expr相同。

iferror

{{#iferror: 测试表达式 | 出错时显示结果 | 正确运算时显示结果 }}
{{#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

#rel2abs

rel2abs函数将页面的相对路径转化为绝对路径

{{#rel2abs: 路径 }}
{{#rel2abs: 路径 | Base路径 }}

输入合法的路径后,具体的语法为:

  • . → 本层
  • .. → 上一层
  • /foo → 指定到名为“foo”的那一层

如果 Base路径未指定,默认使用完整的页面路径{{FULLPAGENAME}}

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

转义与特例

/. or /./,等非法字符会被忽略,其他情况参考下列实例

{{#rel2abs: ../quok/. | 帮助:Foo/bar/baz }}帮助:Foo/bar/quok
{{#rel2abs: ../../quok | 帮助:Foo/bar/baz }}帮助:Foo/quok
{{#rel2abs: ../../../quok | 帮助:Foo/bar/baz }}quok
{{#rel2abs: ../../../../quok | 帮助:Foo/bar/baz }}错误:无效路径深度:“帮助:Foo/bar/baz/../../../../quok”(尝试访问根节点以上节点)

switch

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匹配,都将返回结果3。注意:“#default”后必须有“=”,但其他预设值可以使用“#default”的结果。

time

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

{{ #time: 格式参数 }}

或者

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

如果时间参数未指定,则使用该条目被转换为HTML的时间(值)。注意到由于缓存的缘故,这与条目被浏览的时间可能会有高达1星期的偏差。因此可能需要手工更新,方法是加上action=purge参数访问页面。

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

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

如果時間未被指定,则显示文章最后一次被转换成HTML的时间。由于缓存的关系,此时间和您浏览文章的时间可能有最长一个星期的差别。所以有时可能需要人工更新数据,方法是编辑文章但不做任何修改即保存。

参数format是表示格式的字符串,类似于PHP的时间格式.

以下格式代码和PHP中date()函数意义相同。除了国际化(主要是语言)造成的差别以外,所有和PHP的不同点都应当作为软件的错误进行报告。其中所有的数字输出都会被替换成当地语言的时间格式,可以使用xn(见下文)恢复成显示原来的数字。

代码 描述 输出(示例)
d 一个月中的第 n 天,不足两位补充0 04
D 星期的缩写,通常不国际化
j 一个月中的第 n 天,不足两位不补0 3
l 星期的全称,通常不国际化 星期四
F 月份的的全称,通常需要国际化 5月
m 数字表示的月份,不足两位补充0 01-12
M 月份的的缩写,通常需要国际化 5月
n 数字表示的月份,不足两位不补0 1-12
Y 四位年份 2006
y 二位年份 06
H 小时,不足两位补充0 00-23
i 分钟,不足两位补充0 00-59
s 秒,不足两位补充0 00-59

以下代码是对PHP作出的扩展:

代码 描述
xn 将接下来的数字代码恢复成ASCII中的阿拉伯数字例如,在印地语中,{{#time:H, xnH}}输出०६, 06。
xr 将接下来的数字代码显示成罗马数字
xg 输出月份名字的属格,只针对那些区分主格和属格的语言。
xx 输出"x"

任何其他字符都将不做处理直接输出。您也可以用引号来输出未经处理的字符串。

  • 引号中的字符直接输出(但不输出引号),没有配对的引号也直接输出。例如:
    • {{#time: "现在是" F}} → 现在是 5月
    • {{ #time:i's"}} → 20'11"
  • 像PHP的date()函数一样的反斜杠转义也是支持的。 \H 直接输出 H\" 直接输出 "

未来可能会增加更多格式代码,可能是完善PHP中已有功能,也可能是增加新功能。

参数time的格式参照PHP的strtotime()函数。它同时支持相对时间,如"+10 hours",用来表示时区转换。更多信息参见the GNU tar manual

下表以國際協調时间(UTC)2024年5月2日(星期四)14時44分33秒(北京時間2024年5月2日(星期四)22時44分33秒)为例说明各格式参数的作用。

格式参数 说明 显示结果
A 显示AM或PM PM
a 显示am或pm pm
c 显示长日期 2024-05-02T14:44:33+00:00
D 星期数,以一个汉字显示
d 日期日数,有0补齐, 02
F或M 月份 5月
G或g 当前UTC时间小时数,1位或2位数字 14
H或h 小时数,2位数字 14
i 分钟数,2位数字 44
j 日数,2位数字 2
L 日期星期数,1位数字,星期日为1,星期六为7 1
l 日期星期数,3位汉字 星期四
m 月份数,2位数字 05
N 星期数,星期一为1,星期日为7 4
n 月份数,1位或2位数字 5
r 英文长日期格式 Thu, 02 May 2024 14:44:33 +0000
s 秒数 33
t 该月天数 31
U 时间序号,1970-1-1 0:0:1为1 1714661073
W 日期周数,显示日期为当年第几周 18
w 星期数,星期日为0,星期六为6 4
Y 日期年份,4位数字 2024
y 日期年份,2位数字 24
z 显示日期为当年第几日 122

系统默认的时间参数为当前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}}返回 2002-04-06 08:10:12

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

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}} 得到 中文(新加坡)

titleparts

This function separates a page title into segments based on slashes, then returns some of those segments as output. {{#titleparts: pagename | number of segments to return | first segment to return }}

If the number of segments to return parameter is not specified, it defaults to "0", which returns all the segments from the first segment to return (included). If the first segment to return parameter is not specified or is "0", it defaults to "1":

{{#titleparts: 讨论:Foo/bar/baz/quok }}讨论:Foo/bar/baz/quok
{{#titleparts: 讨论:Foo/bar/baz/quok | 1 }}讨论:Foo See also {{模板:Ll}}.
{{#titleparts: 讨论:Foo/bar/baz/quok | 2 }}讨论:Foo/bar
{{#titleparts: 讨论:Foo/bar/baz/quok | 2 | 2 }}bar/baz
{{#titleparts: 讨论:Foo/bar/baz/quok | | 2 }}bar/baz/quok
{{#titleparts: 讨论:Foo/bar/baz/quok | | 5 }}

Negative values are accepted for both values. Negative values for the number of segments to return parameter effectively 'strips' segments from the end of the string. Negative values for the first segment to return translates to "start with this segment counting from the right":

{{#titleparts: 讨论:Foo/bar/baz/quok | -1 }}讨论:Foo/bar/baz Strips one segment from the end of the string. See also {{BASEPAGENAME}}.
{{#titleparts: 讨论:Foo/bar/baz/quok | -4 }} Strips all 4 segments from the end of the string
{{#titleparts: 讨论:Foo/bar/baz/quok | -5 }} Strips 5 segments from the end of the string (more than exist)
{{#titleparts: 讨论:Foo/bar/baz/quok | | -1 }} quok Returns last segment. See also {{SUBPAGENAME}}.
{{#titleparts: 讨论:Foo/bar/baz/quok | -1 | 2 }} bar/baz Strips one segment from the end of the string, then returns the second segment and beyond
{{#titleparts: 讨论:Foo/bar/baz/quok | -1 | -2 }} baz Start copying at the second last element; strip one segment from the end of the string

Before processing, the pagename parameter is HTML-decoded: if it contains some standard HTML character entities, they will be converted to plain characters (internally encoded with UTF-8, i.e. the same encoding as in the MediaWiki source page using this parser function).

For example, any occurence of&quot;, &#34;, or &#x22; in pagename will be replaced by "

No other conversion from HTML to plain text is performed, so HTML tags are left intact at this initial step even if they are invalid in page titles.

Some magic keywords or parser functions of MediaWiki (such as <tvar|PAGENAME>{{模板:Ll}}</> and similar) are known to return strings that are needlessly HTML-encoded, even if their own input parameter was not HTML-encoded:

The titleparts parser function can then be used as a workaround, to convert these returned strings so that they can be processed correctly by some other parser functions also taking a page name in parameter (such as <tvar|PAGESINCAT>{{模板:Ll}}</> but which are still not working properly with HTML-encoded input strings.

For example, if the current page is Category:Côte-d'Or, then:

  • {{#ifeq: {{FULLPAGENAME}} | Category:Côte-d'Or | 1 | 0 }} and {{#ifeq: {{FULLPAGENAME}} | Category:Côte-d&apos;Or | 1 | 0 }} are both returning <tvar|actual>1; (the #ifeq parser function does perform the HTML-decoding of its input parameters).
  • {{#switch: {{FULLPAGENAME}} | Category:Côte-d'Or = 1 | #default = 0 }}, and {{#switch: {{FULLPAGENAME}} | Category:Côte-d&apos;Or = 1 | #default = 0 }} are both returning 1; (the #switch parser function does perform the HTML-decoding of its input parameters).
  • {{#ifexist: {{FULLPAGENAME}} | 1 | 0 }}, {{#ifexist: Category:Côte-d'Or | 1 | 0 }} or even {{#ifexist: Category:Côte-d&apos;Or | 1 | 0 }} will all return 1 if that category page exists (the #ifexist parser function does perform the HTML-decoding of its input parameters);*{{PAGESINCAT: Côte-d'Or }}</> will return a non-zero number, if that category contains pages or subcategories, but:
  • {{PAGESINCAT: {{CURRENTPAGENAME}} }}</>, may still unconditionally return 0, just like:
  • {{PAGESINCAT: {{PAGENAME|Category:Côte-d'Or}} }}
  • {{PAGESINCAT: {{PAGENAME|Category:Côte-d&apos;Or}} }}The reason of this unexpected behavior is that, with the current versions of Mediawiki, there are two caveats:
  • {{FULLPAGENAME}}</>, or even <tvar|code1>{{FULLPAGENAME|Côte-d'Or}}</> may return the actually HTML-encoded string <tvar|actual>Category:Côte-d&apos;Or</> and not the expected <tvar|expected>Category:Côte-d'Or</>, and that:
  • {{PAGESINCAT: Côte-d&apos;Or }}</> unconditionally returns 0 (the PAGESINCAT magic keyword does not perform any HTML-decoding of its input parameter).</translate>

The simple workaround using titleparts (which will continue to work if the two caveats are fixed in a later version of MediaWiki) is:</translate>

  • {{PAGESINCAT: {{titleparts: {{CURRENTPAGENAME}} }} }}
  • {{PAGESINCAT: {{titleparts: {{PAGENAME|Category:Côte-d'Or}} }} }}
  • {{PAGESINCAT: {{titleparts: {{PAGENAME|Category:Côte-d&apos;Or}} }} }}</>, that all return the actual number of pages in the same category. Then the decoded pagename is canonicalized into a standard page title supported by MediaWiki, as much as possible:
  1. All underscores are automatically replaced with spaces:
    {{#titleparts: 讨论:Foo/bah_boo|1|2}}bah boo Not bah_boo, despite the underscore in the original.
  2. The string is split a maximum of 25 times; further slashes are ignored and the 25th element will contain the rest of the string. The string is also limited to 255 characters, as it is treated as a page title:
    {{#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
    If for whatever reason you needed to push this function to its limit, although very unlikely, it is possible to bypass the 25 split limit by nesting function calls:
    {{#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. The list of substrings is not cleaned up hierarchically (unlike with the <tvar|PAGENAME>{{模板:Ll}}</> magic keyword, the <tvar|code1>./</> and <tvar|code2>../</> relative path segments are left intact by #titleparts):
    {{#titleparts: ../c/d }}../c/d
    {{PAGENAME: ../c/d }} → '
  4. Finally the first substring is capitalized according to the capitalization settings of the local wiki (if that substring also starts by a local namespace name, that namespace name is also normalized).
    {{#titleparts: talk:a/b/c }}讨论:A/b/c

You can use #titleparts as a small "string parser and converter", but consider that it returns the first substring capitalized:

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

If lower case is needed, use lc: function to control output:

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

2</> instead of <tvar|p1>1</> for first segment to return:

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

Certain characters that are illegal in a page title will cause #titleparts to not parse the string:

{{#titleparts: {one/two} | 1 | 1 }}{one/two}. Does not produce the expected:</translate> {one
{{#titleparts: [[page]]/123 | 1 | 2 }}page/123. Does not work because brackets are illegal in page titles and this parser function does not process links embedded in its input pagename parameter, even when they use the MediaWiki syntax, or any other HTML or MediaWiki tags.
{{#titleparts: red/#00FF00/blue | 1 | 3 }} → "". Does not work because "#" is also illegal in page titles. This function does not degrade gracefully if your input exceeds 255 characters. If the inputted string is 256 characters long or more, this function will simply toss the string back at you.

invoke

invoke函数会调用位于Module命名空间下的Lua模块,书写方法如下:

  • {{#invoke: module | function | arg1 | arg2 | ... }} (参见帮助:模块

tag

tag函数即标签函数,用来替代解析器函数实现标签(<>)功能,在某些因解析时机造成的标签无法生效的情况下,需要用tag函数来改写解析器标签。

  • {{#tag:"标签名"| 内容 | 参数= | ... }} 例如{{#tag:rec| 内容 | ... }}等于<rec>内容</rec>这一方法主要用以解决时机不同造成的模板内部无法使用“<>”标签的问题。

replace

replace函数用来替换内容中特定的元素。格式为:

  • {{#replace:"内容"|需要替换的元素|用来替换的元素}} 例:
{{#replace:心wiki|wiki|百科}}

可以得到:

  • 心百科

var系列函数

模板:Deprecate “变量(Variables)”扩展允许您在当前维基页面上声明、创建和调用变量,非常类似程序语言中的对应概念,是数据结构化和复杂模板常用的函数之一。

vardefine

声明变量(定义)

  • {{#vardefine:变量名称|变量值}}
  • 例如: {{#vardefine:iconwidth|25}} 使得变量iconwidth = 25

vardefineecho

  • {{#vardefineecho:变量名称|变量值}} 作用与#vardefine相同, 区别是会在定义后将这一数值打印出来。
  • 例子:{{#vardefineecho:iconwidth|25}}除了会使得变量iconwidth的值为25外,还会留下“25”在页面上

var

调用变量的数值:

  • {{#var:变量名称}}
    • 如果变量没有定义,则返回一个空值而不会提示任何错误信息
    • 可以给数值为空或者没有定义的变量赋值,写法如下:
      • {{#var:变量名称|默认值}}
      • 等同于{{#if: {{#var:变量名称}} |{{#var:变量名称}} |默认值}} #var的数值可以应用于parser和函数中 ===varexists===
  • {{#varexists:变量名称}} 类似ifexist函数,如果变量存在则返回1(定义为空值时也会视为已存在),如果变量尚未被定义,则返回空值

var_final

在Variables 2.0版本中新加入的函数功能,这个函数类似#var,只不过返回的是变量在本页面的最终值(因为一个变量可能会被多次赋值)。

  • 注意,#var_final的工作机制决定了这个函数的数值只能在整个页面的markup被执行完毕后才能返回,因此他的数值无法在parser和函数中被调用。
  • {{#var_final:变量名称|默认值}} 默认值可以作为页面变量的“兜底”,即确保了即使没有任何赋值的情况下也能使得该变量有一个默认值。

Cargo系列函数

Page Forms函数

arraymap

arraymap是一个灵活的函数,用以展开一段内容中的每一个元素,具体方法:

  • {{#arraymap:<要展开的内容>|<分隔符>|<变量>|<变量公式>|<展开后的分隔符>}} 如果要展开的内容是一个逗号隔开的字符串,例如:关羽,张飞,马超,赵云,黄忠
  • {{#arraymap:关羽,张飞,马超,赵云,黄忠|,|@@@|{{CharacterInfo|@@@}}|<br>}} 这段代码的意思就是将关羽、张飞、马超、赵云、黄忠视为要展开的5个元素(由指定的分隔符“,”从要展开的字符串里获得),每个元素都被放到公式“{{CharacterInfo|@@@}}”中去并用换行符隔开,代码展开为html之前等价于:
  • {{CharacterInfo|关羽}}<br>{{CharacterInfo|张飞}}<br>{{CharacterInfo|马超}}<br>{{CharacterInfo|赵云}}<br>{{CharacterInfo|黄忠}} 利用arraymap我们可以模拟打印表格,公式生成每一行,在整段代码前放上th和table标签,整段代码之后用table标签闭合即可
  • 这一方法能够完美的替代SMW中的format=template功能,无需在其他页面书写逻辑,自定义程度也更高,更加自由。

default form

default form函数用来为当前页面制定默认使用的表单,指定后页面就可以使用表单编辑。

  • 写法:{{#default_form:表单名称}}, 替代了旧写法 [[Page has default form::表单名称]]

Array系列函数

arraydefine

语法: {{#arraydefine:数组名|赋值来源|分隔符|选项参数}} {| class="wikitable" |- !参数 !描述 |- |使用正则表达式作为分隔符,例如/\s*[;,]\s*/ |{{#arraydefine:f|apple, pear; orange|/\s*[;,]\s*/}} |- |选项参数:unique,将赋值自动去重 |{{#arraydefine:b|orange,red ,yellow, yellow|,|unique}}中,两个yellow只会被保留1个 |- |选项参数:sort=,可选值

  • none (default) - 不予排序
  • desc - 降序
  • asce/asc - 升序
  • random - 随机,乱序
  • reverse - 反序 |{{#arraydefine:b|orange,red ,yellow, yellow|,|unique,sort=desc}} |- |选项参数:print=,可选值,将内容打印出来
  • list - 不予排序 |{{#arraydefine:b|orange,red ,yellow, yellow|,|unique,sort=desc,print=list}} |}

arrayprint

语法: {{#arrayprint:数组名|分隔符|定义格式|表达式|选项参数}} 该函数功能类似arraymap,将制定数组按照所制定的分隔符分割,并将每一元素按照表达式展开。一般和arraydefine成对使用。

arrayindex

语法: {{#arrayindex:数组名|元素序号|返回默认值}} {| class="wikitable" |- !参数 !描述 |- |元素序号,基数为0(第一个元素),如果序号超出范围或者无效,arrayindex函数会返回空值。负数序号表示反序取值(从最后开始取),-1表示数组的最后一个元素。 |{{#arrayindex:a|0}}{{#arrayindex:b|-1}} |- |返回默认值,当查询不到对应元素时返回的默认结果 |{{#arrayindex:c |foo |数值不存在! }} |} 除此之外,arrays家族还包括arrayunique、arraymerge、arrayunion等函数,均为php数组函数的映射,可以对字符串进行查重、排序、合并等操作,这里不再一一介绍,可以在MediaWiki官网查阅相关语法。

subst

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

表格

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

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

参见

感谢

本文内容搬运自灰机Wiki的帮助文件,后期根据心百科自身情况做了调整和增删改动,无论如何对原文档的编辑致以诚挚的谢意。