模块:Var

    来自中国珂学院
    MEmori(讨论 | 贡献)2020年3月3日 (二) 21:21的版本 (创建页面,内容为“local frame = mw.getCurrentFrame() local module = { getPlain = function(key, type) if type then type = '_'..type else type = '' end return frame:call…”)
    (差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

    可在模块:Var/doc创建此模块的帮助文档

    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