Geen bewerkingssamenvatting |
slotData can be nil (if page does not exist) |
||
| (Een tussenliggende versie door dezelfde gebruiker niet weergegeven) | |||
| Regel 21: | Regel 21: | ||
--[[ helper ]]-- | --[[ helper ]]-- | ||
p.getValueFromTemplateData = function( slot, page, template, param ) | p.getValueFromTemplateData = function( slot, page, template, param, default ) | ||
local slotData = {} | local slotData = {} | ||
slotData = mw.slots.slotData(slot,page) | slotData = mw.slots.slotData(slot,page) | ||
if slotData == {} then | if slotData == {} or slotData == nil then | ||
mw.log( "No slot data found" ) | mw.log( "No slot data found" ) | ||
return "" | return "" | ||
end | end | ||
local | -- default value if no value was found (unless there are no slotdata at all) | ||
local default = default or "" | |||
local res = default | |||
if template ~= nil then | if template ~= nil then | ||
local tree = slotData[template] | local tree = slotData[template] | ||
Huidige versie van 28 okt 2025 18:12
Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Slot/doc
local p = {}
--[[
{{#invoke:Slot|getParamValue
|slot= // defaults to main
|page= // defaults to FULLPAGENAME
|template=
|param=Category
}}
]]--
p.getParamValue = function ( frame )
local slot = frame.args.slot or "main"
local page = frame.args.page or ""
-- else present page
if page == "" then page = mw.title.getCurrentTitle().fullText end
local template = frame.args.template or nil
if template == nil then return "" end
local param = frame.args.param or ""
return p.getValueFromTemplateData( slot, page, template, param )
end
--[[ helper ]]--
p.getValueFromTemplateData = function( slot, page, template, param, default )
local slotData = {}
slotData = mw.slots.slotData(slot,page)
if slotData == {} or slotData == nil then
mw.log( "No slot data found" )
return ""
end
-- default value if no value was found (unless there are no slotdata at all)
local default = default or ""
local res = default
if template ~= nil then
local tree = slotData[template]
if type(tree) == "table" then
--for k,v in pairs( tree ) do
--mw.log( "tree key: " .. k )
--end
if tree[1] ~= nil and tree[1][param] ~= nil then
res = tree[1][param]["_text"]
end
end
else
-- do JSON, too?
end
return res
end
--[[
Moved from Class/Title
]]--
p.getValueFromTemplateParam = function( pagename, templateName, templateParam )
if pagename == "" then
mw.log( "pagename is empty" )
return ""
end
-- pre-check, just in case
local title = mw.title.new( pagename )
if title == nil or not title.exists then
mw.log( "title does not exist" )
return ""
end
local slotContent = p.getSlotContent( pagename, "ws-page-props", templateName )
if slotContent == nil or slotContent[1] == nil or slotContent[1][templateParam] == nil then
mw.log( "No value retrieved from page. Template param: " .. templateParam )
return ""
end
if type( slotContent[1][templateParam]["_text"] ) == "string" then
return slotContent[1][templateParam]["_text"]
else
return "?"
end
end
--[[
Moved from Class/Title
]]--
p.getSlotContent = function( page, slot, template )
local template = template or ""
if ( template == "" ) then
-- Assuming JSON
if slot == "main" then
--loadJsonData does not alway work. Use WSSlots instead
--return mw.loadJsonData( page )
end
local str = mw.slots.slotContent( slot, page )
if type(str) == "table" then
-- @todo VERY unlikely
return str
end
if str ~= nil then
return mw.text.jsonDecode( str )
else
return {}
end
end
-- template
local slotData = mw.slots.slotData( slot, page )
if ( slotData == nil ) then
return type(slotData)
end
return slotData[template]
end
return p
