BBSers.org

Help:模板扩展语法

出自BBSers' Wiki

Template:H:hns Template: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 舍入 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> | <相等时返回的字符串> | <不相等时返回的字符串> }}

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

ifexist

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

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

示例:

  • {{#ifexist:test|有test頁面|無test頁面}} 得到 {{#ifexist:test|有test頁面|無test頁面}}
  • {{#ifexist:user:sex|該用戶存在|該用戶不存在}} 得到 {{#ifexist:user:sex|該用戶存在|該用戶不存在}}
  • {{#ifexist:Calculation|Yes|Oops}} 得到 {{#ifexist:Calculation|Yes|Oops}}

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”的结果。

time

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

{{ #time: format }}

或者

{{ #time: format | time }}

Template:TransH If the time is not specified, the time at which the article is converted to HTML is used. Note that due to caching, this may be up to a week different to the time at which the article is viewed. Manual updates may be required, this can be achieved by saving the page without making any changes (a "null edit").

The format parameter is a format string similar to the one used by PHP's date.

The following format codes have the same meaning as in PHP. Significant differences from PHP's behaviour, apart from internationalisation (i.e. language and locale differences), should be considered an error of ParserFunctions and should be reported. All numeric format codes return numbers formatted according to the local language, use the xn code described below to override this.

Code Description Example output
d The day of the month, zero-padded. 04
D An abbreviation for the day of the week. Rarely internationalised. Mon
j The day of the month, not zero-padded. 3
l The full weekday name. Rarely internationalised. Monday
F The full month name. Often internationalised. January
m The month number, zero-padded. 01 to 12
M An abbreviation of the month name. Often internationalised. Jan
n The month number, not zero-padded. 1 to 12
Y The 4-digit year. 2006
y The 2-digit year. 06
H The hour of the day, zero-padded. 00 to 23
i The minute, zero-padded. 00 to 59
s The second, zero-padded 00 to 59

The following format codes are extensions to the PHP syntax:

Code Description
xn Format the next numeric code as a raw ASCII number. For example, in the Hindi language, {{ #time:H, xnH}} produces ०६, 06.
xr Format the next numeric code as a roman numeral.
xg Output the genitive form of the month name, for languages which make such a distinction between genitive and nominative.
xx A literal "x"

Any unrecognised character will be passed through to the output unmodified. There are also two quoting conventions which can be used to output literal characters.

  • Characters enclosed in double quotes will be considered literal (with the quotes themselves removed). Unmatched quotes will be considered literal quotes. Example:
    • {{ #time: "The month is" F}} → The month is January
    • {{ #time:i's"}} → 20'11"
  • Backslash escaping as in PHP's date() is supported. \H produces a literal H, \" produces a literal ".

More format codes may be added in the future, as demanded by the users of this extension. This may come in the form of either a more complete implementation of PHP's format codes, or additional "x" codes.

The format of the time parameter is identical to the one used by PHP's strtotime(). It supports relative dates, such as "+10 hours", which can be used for timezone translation. Please see the GNU tar manual for more information.

Template:TransF

Examples

  • {{#time:Y年Fj日 |-14 days}} 得到14天前的日期: {{#time:Y年Fj日|-14 days}}
  • {{#time:H:i|+8 hours}} 得到当前的UTC+8时间: {{#time:H:i|+8 hours}}

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. 通过嵌套模板来达到隐藏“|”的目的。比如:{{!{{#if:||{{{2}}}}}}}
  2. 使用HTML语法。

参见

Template:H:f Help

en:m:ParserFunctions