Module:Data: Difference between revisions
Jump to navigation
Jump to search
imported>Jackmcbarn (create helper module to get values from a data module) |
m (Protected "Module:Data": Important page ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local | local mt = {} | ||
function | function mt.__index(t, k) | ||
return function(frame) | |||
local data = mw.loadData(k) | |||
local i = 1 | |||
for _,v in ipairs(frame.args) do | |||
local ty = type(data) | |||
if ty ~= 'table' then | |||
local args = {} | |||
for j = 1, i - 1 do | |||
args[j] = frame.args[j] | |||
end | |||
if frame.args.softfail then | |||
return '<span class="error">[[Category:Pages with failed Module:Data lookups]]Error: Tried to read index "' .. mw.text.nowiki(v) .. '" of mw.loadData("' .. mw.text.nowiki(k) .. '").' .. mw.text.nowiki(table.concat(args, '.')) .. ', which is a ' .. ty .. '</span>' | |||
else | |||
error('Tried to read index "' .. v .. '" of mw.loadData("' .. k .. '").' .. table.concat(args, '.') .. ', which is a ' .. ty) | |||
end | |||
end | |||
data = data[v] | data = data[v] | ||
i = i + 1 | |||
end | end | ||
return data | |||
end | end | ||
end | end | ||
return | return setmetatable({}, mt) |
Latest revision as of 03:25, 15 September 2023
Documentation for this module may be created at Module:Data/doc
local mt = {} function mt.__index(t, k) return function(frame) local data = mw.loadData(k) local i = 1 for _,v in ipairs(frame.args) do local ty = type(data) if ty ~= 'table' then local args = {} for j = 1, i - 1 do args[j] = frame.args[j] end if frame.args.softfail then return '<span class="error">[[Category:Pages with failed Module:Data lookups]]Error: Tried to read index "' .. mw.text.nowiki(v) .. '" of mw.loadData("' .. mw.text.nowiki(k) .. '").' .. mw.text.nowiki(table.concat(args, '.')) .. ', which is a ' .. ty .. '</span>' else error('Tried to read index "' .. v .. '" of mw.loadData("' .. k .. '").' .. table.concat(args, '.') .. ', which is a ' .. ty) end end data = data[v] i = i + 1 end return data end end return setmetatable({}, mt)