Module:TemplateStructure: Difference between revisions
Appearance
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 44: | Line 44: | ||
-- Return the full HTML string for the table. | -- Return the full HTML string for the table. | ||
return tableEl: | return tableEl:toString() | ||
end | end | ||
return p | return p | ||
Revision as of 04:41, 16 February 2025
Documentation for this module may be created at Module:TemplateStructure/doc
local p = {}
--[[
p.render(args, config)
Renders a table using a modular, block-based approach via the HTML builder API.
This version generates an explicit <table> element with its rows inserted as unescaped wikitext,
so that MediaWiki’s parser won’t inject unwanted <p> or <br> elements.
Parameters:
args - Table of template parameters (passed to each block function).
config - Table containing configuration options:
tableClass: (string) CSS class for the table.
Default: "template-table"
tableAttrs: (table) A table of additional attributes for the table tag.
Default: { cellpadding = "2" }
blocks: (array) An ordered list of functions (blocks) that generate table rows.
Each block function should accept (args, config) and return a string.
Returns:
A string containing the complete HTML markup for the table.
]]
function p.render(args, config)
config = config or {}
local tableClass = config.tableClass or "template-table"
local tableAttrs = config.tableAttrs or { cellpadding = "2" }
local blocks = config.blocks or {}
-- Create a table element with the given attributes.
local tableEl = mw.html.create("table", tableAttrs)
tableEl:attr("class", tableClass)
-- Process each block function and insert its output as unescaped wikitext.
for i, block in ipairs(blocks) do
if type(block) == "function" then
local blockOutput = block(args, config)
if blockOutput and blockOutput ~= "" then
tableEl:wikitext(blockOutput)
end
else
mw.log("Warning: Block #" .. i .. " is not a function and will be skipped.")
end
end
-- Return the full HTML string for the table.
return tableEl:toString()
end
return p