用戶:Selfice/圖書館/幫助:擴展:解析器函數(下)
| 本頁面之全部或部分原來自MediaWiki 的Help:Extension:ParserFunctions/zh,依 CC0 1.0 授權引入;原貢獻者可以在這裏看到。 |
| 本頁面之全部或部分原來自tools Wiki 的首頁,依 CC0 1.0 授權引入;原貢獻者可以在這裏看到。 |
上接User:Selfice/圖書館/幫助:擴展:解析器函數(上)
#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 }} →
[搬運者註:以下用法在萌娘百科裏似乎會因為觸發過濾器而導致WAF,無法保存和預覽,請謹慎使用]
支持布爾值運算的等於/不等於的運算符。
{{#ifexpr: 0 = 0 or 1 = 0 | yes}} → yes
{{#ifexpr: 0 = 0 and 1 = 0 | | no}} → no
#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