Module:TemplateStructure: Difference between revisions
Appearance
No edit summary |
No edit summary Tag: Reverted |
||
| Line 4: | Line 4: | ||
p.render(args, config) | p.render(args, config) | ||
Renders a table using a modular, block-based approach. | 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 raw HTML, | |||
so that MediaWiki’s parser won’t inject unwanted <p> or <br> elements. | |||
Parameters: | Parameters: | ||
| Line 11: | Line 13: | ||
tableClass: (string) CSS class for the table. | tableClass: (string) CSS class for the table. | ||
Default: "template-table" | Default: "template-table" | ||
tableAttrs: ( | tableAttrs: (table) A table of additional attributes for the table tag. | ||
Default: | Default: { cellpadding = "2" } | ||
blocks: (array) An ordered list of functions (blocks) that generate table rows. | blocks: (array) An ordered list of functions (blocks) that generate table rows. | ||
Each block function should accept (args, config) and return a string. | Each block function should accept (args, config) and return a string. | ||
Returns: | Returns: | ||
A string containing the | A string containing the complete HTML markup for the table. | ||
]] | ]] | ||
function p.render(args, config) | function p.render(args, config) | ||
config = config or {} | config = config or {} | ||
local tableClass = config.tableClass or "template-table" | local tableClass = config.tableClass or "template-table" | ||
local tableAttrs = config.tableAttrs or | -- Expect tableAttrs as a table; if not provided, default to cellpadding="2" | ||
local tableAttrs = config.tableAttrs or { cellpadding = "2" } | |||
local blocks = config.blocks or {} | local blocks = config.blocks or {} | ||
-- | -- Create a table element with the given attributes. | ||
local | local tableEl = mw.html.create("table", tableAttrs) | ||
tableEl:attr("class", tableClass) | |||
-- Process each block function | -- Process each block function and insert its output as raw HTML. | ||
for i, block in ipairs(blocks) do | for i, block in ipairs(blocks) do | ||
if type(block) == "function" then | if type(block) == "function" then | ||
local blockOutput = block(args, config) | local blockOutput = block(args, config) | ||
if blockOutput and blockOutput ~= "" then | if blockOutput and blockOutput ~= "" then | ||
tableEl:raw(blockOutput) | |||
end | end | ||
else | else | ||
| Line 44: | Line 43: | ||
end | end | ||
end | end | ||
table. | -- Return the full HTML string for the table. | ||
return | return tableEl:html() | ||
end | end | ||
return p | return p | ||
Revision as of 04:38, 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 raw HTML,
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"
-- Expect tableAttrs as a table; if not provided, default to cellpadding="2"
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 raw HTML.
for i, block in ipairs(blocks) do
if type(block) == "function" then
local blockOutput = block(args, config)
if blockOutput and blockOutput ~= "" then
tableEl:raw(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:html()
end
return p