Module:Infobox: Difference between revisions
From GameBrew
More actions
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 4: | Line 4: | ||
--- Main function: generates an infobox table | --- Main function: generates an infobox table | ||
function p.infobox(frame) | function p.infobox(frame) | ||
-- | -- Determine args: if called from Scribunto frame, extract parent template args; otherwise frame is args table | ||
local args = (frame | local args = {} | ||
if type(frame) == 'table' and type(frame.getParent) == 'function' then | |||
local parent = frame:getParent() | |||
args = parent and parent.args or {} | |||
elseif type(frame) == 'table' then | |||
args = frame | |||
end | |||
-- Create the container table | -- Create the container table | ||
| Line 17: | Line 23: | ||
-- Title row (defaults to page title) | -- Title row (defaults to page title) | ||
local title = args.title or mw.title.getCurrentTitle().text | local title = args.title | ||
if not title or title == '' then | |||
title = mw.title.getCurrentTitle().text | |||
end | |||
infobox:tag('tr') | infobox:tag('tr') | ||
:tag('th') | :tag('th') | ||
| Line 55: | Line 64: | ||
-- Data rows: uses args.rows to limit iterations or defaults to 30 | -- Data rows: uses args.rows to limit iterations or defaults to 30 | ||
local maxRows = tonumber(args.rows or 30 | local maxRows = tonumber(args.rows) or 30 | ||
for i = 1, maxRows do | for i = 1, maxRows do | ||
local header = args['header' .. i] | local header = args['header' .. i] | ||
Revision as of 02:22, 18 April 2025
Documentation for this module may be created at Module:Infobox/doc
-- Simple infobox module with enhanced flexibility and cleaner structure
local p = {}
--- Main function: generates an infobox table
function p.infobox(frame)
-- Determine args: if called from Scribunto frame, extract parent template args; otherwise frame is args table
local args = {}
if type(frame) == 'table' and type(frame.getParent) == 'function' then
local parent = frame:getParent()
args = parent and parent.args or {}
elseif type(frame) == 'table' then
args = frame
end
-- Create the container table
local infobox = mw.html.create('table'):addClass('infobox')
-- Top ad slot
infobox:node(
mw.html.create('div')
:attr('id', 'Ads-InfoboxTop')
)
-- Title row (defaults to page title)
local title = args.title
if not title or title == '' then
title = mw.title.getCurrentTitle().text
end
infobox:tag('tr')
:tag('th')
:addClass('infobox-title')
:attr{scope = 'col', colspan = '2'}
:wikitext(title)
-- Optional description row
if args.description and args.description ~= '' then
infobox:tag('tr')
:tag('td')
:addClass('infobox-description')
:attr('colspan', 2)
:wikitext(args.description)
end
-- Image block (with optional width and caption)
if args.image and args.image ~= '' then
local img = args.image
-- Wrap in File: namespace if needed
if not img:match('%[%[') then
if not img:find(':') then img = 'File:' .. img end
local width = args.imagewidth or '300px'
img = string.format('[[%s|%s]]', img, width)
end
local imgCell = infobox:tag('tr')
:tag('td')
:addClass('infobox-image')
:attr('colspan', 2)
:wikitext(img)
if args.imagecaption and args.imagecaption ~= '' then
imgCell:tag('div')
:addClass('infobox-caption')
:wikitext(args.imagecaption)
end
end
-- Data rows: uses args.rows to limit iterations or defaults to 30
local maxRows = tonumber(args.rows) or 30
for i = 1, maxRows do
local header = args['header' .. i]
if header and header ~= '' then
infobox:tag('tr')
:tag('th')
:addClass('infobox-header')
:attr{scope = 'col', colspan = '2'}
:wikitext(header)
end
local label = args['label' .. i]
local data = args['data' .. i]
if data and data ~= '' then
if label and label ~= '' then
local row = infobox:tag('tr')
row:tag('th')
:addClass('infobox-label')
:attr('scope', 'row')
:wikitext(label)
row:tag('td')
:addClass('infobox-data')
:wikitext(data)
else
infobox:tag('tr')
:tag('td')
:addClass('infobox-data')
:attr{colspan = '2', style = 'text-align:center;'}
:wikitext(data)
end
end
end
-- Optional footer row
if args.below and args.below ~= '' then
infobox:tag('tr')
:tag('td')
:addClass('infobox-below')
:attr('colspan', 2)
:wikitext(args.below)
end
-- Return HTML string
return tostring(infobox)
end
return p