Module:BooleanSwitch
跳到导航
跳到搜索
提供了一种用于模板中快速判断类 Switch Parameter 的机制。只能用于被引用且传参数的页面。
方法
boolMatch
用于精确匹配一个 Switch Parameter。返回一个 Boolean 来表示是否在被引用页找到这个 Switch Parameter。
参数
- 匿名参数: 需要匹配的 Switch Parameter 内容。
返回值
如果找到,返回 true,否则返回 false。
举例
{{崩坏系列|崩坏学园2|崩坏3}}
这个模板调用传入了两个 Switch Parameter,一个 “崩坏学园” 和一个 “崩坏学园2”。
{{#invoke:BooleanSwitch|boolMatch|崩坏学园}}
-- returns true
{{#invoke:BooleanSwitch|boolMatch|foo}}
-- returns false
{{#invoke:BooleanSwitch|boolMatch}}
-- returns false
-- This module determines if a certain switch parameter
-- exists in template invocation parameters.
local p = {}
function p.boolMatch(frame)
local matchArg = frame.args[1]
if matchArg ~= nil then
-- So we will iterate every parameters in the upper level.
local parentPage = frame:getParent()
for _, param in pairs(parentPage.args) do
if param == matchArg then
return true
end
end
end
-- Otherwise
return false
end
return p