模块:模块骨架
来自星砂岛百科
更多操作
概述
模块骨架 是中文百科业务模块的最小起步模板,用于快速搭建一个读取数据页并暴露 getField 的新模块。
用法
local common = require('Module:Common')
local p = {}
local data_cache
local index_by_name
local function load_data()
if data_cache then
return
end
data_cache = common.loadJsonData('数据:Example/example_index.json') or { by_name = {} }
index_by_name = data_cache.by_name or {}
end
示例
{{#invoke:Example|getField|示例条目|name}}
函数
getField:按页面名、映射名或 ID 查找记录并返回指定字段。
local common = require('Module:Common')
local p = {}
local data_cache
local index_by_name
local function load_data()
if data_cache then
return
end
data_cache = common.loadJsonData('数据:Example/example_index.json') or { by_name = {} }
index_by_name = data_cache.by_name or {}
end
local function find_record(key)
load_data()
local resolved = common.trim(key)
if resolved == '' then
resolved = common.getCurrentTitleText()
end
return index_by_name[resolved] or index_by_name[common.normalizeKey(resolved)]
end
function p.getField(frame)
local key = common.getArg(frame, 1, '')
local field = common.getArg(frame, 2, '')
if field == '' then
return ''
end
local record = find_record(key)
if not record then
return ''
end
return common.toText(record[field])
end
return p