Module:Var
跳到导航
跳到搜索
简介
该模块为一个操作wiki页面变量的快捷工具。
方法
get
[nil, boolean, number, string] var.get($string 变量名 [, $string 获取类型])
该方法获取一个变量的值,且按值进行一系列对应的转换并返回。
- 空字符串 => nil
- 字符串true => 布尔值true
- 字符串false => 布尔值false
- 开头非0的字符串十进制数字 => 数字
- 与以上条件均不匹配的字符串 => 原样返回
第二个参数获取类型目前仅支持一个参数:
- final:获取变量的最终值,等同于解析器函数
{{#var_final}}
getPlain
[string] var.getPlain($string 变量名 [, $string 获取类型])
同上,但没有值的转换。
set
[任何类型] var.set(变量名, 值)
设置一个变量的值,并返回设置的值。
remove
[string] var.remove(变量名)
删除一个变量,相当于var.set('变量名', '')
,并返回被删除变量的值。
如果单纯要进行删除,不需要被删除的值,建议使用var.set('变量名', '')
,因为remove方法会执行两次解析器函数(查询值+删除值)。
- local frame = mw.getCurrentFrame()
- local module = {
- getPlain = function(key, type)
- if type then
- type = '_'..type
- else
- type = ''
- end
- return frame:callParserFunction("#var"..type, tostring(key))
- end,
- set = function(key, val)
- frame:callParserFunction("#vardefine", tostring(key), tostring(val))
- return val
- end
- }
- module.get = function(key, type)
- local val = module.getPlain(key, type)
- if val == '' then return nil end
- if val:find('^0') then return val end
- if tonumber(val) then return tonumber(val) end
- if var == 'true' then return true end
- if var == 'false' then return false end
- return val
- end
- module.remove = function(key)
- local removedVal = module.getPlain(key)
- module.set(key, "")
- return removedVal
- end
- return module