Module:TemplateStructure: Difference between revisions
Appearance
No edit summary Tag: Manual revert |
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 mw.html API. | ||
This version creates a complete HTML table element that is not re-wrapped by the parser. | |||
Parameters: | Parameters: | ||
args - Table of template parameters (passed to each block function). | args - Table of template parameters (passed to each block function). | ||
config - Table containing configuration options: | config - Table containing configuration options: | ||
tableClass: (string) CSS class for the table | tableClass: (string) CSS class for the table (default: "template-table") | ||
tableAttrs: (table) Attributes for the table element (default: {cellpadding = "2"}) | |||
tableAttrs: ( | blocks: (array) An ordered list of block functions that generate HTML fragments. | ||
Each block function should accept (args, config) and return a string | |||
containing HTML (e.g., complete <tr>...</tr> markup). | |||
blocks: (array) An ordered list of functions | |||
Each block function should accept (args, config) and return a string. | |||
Returns: | Returns: | ||
A string containing the | A string containing the 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 | local tableAttrs = config.tableAttrs or {cellpadding = "2"} | ||
local blocks = config.blocks or {} | local blocks = config.blocks or {} | ||
-- | -- Create the table element. | ||
local | local tableEl = mw.html.create("table", tableAttrs) | ||
tableEl:attr("class", tableClass) | |||
-- Process each block function | -- Process each block function and append its HTML fragment to the table. | ||
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:wikitext(blockOutput) | |||
end | end | ||
else | else | ||
| Line 44: | Line 40: | ||
end | end | ||
end | end | ||
-- Return the complete HTML as a string. | |||
return | return tostring(tableEl) | ||
end | end | ||
return p | return p | ||
Revision as of 12:12, 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 mw.html API.
This version creates a complete HTML table element that is not re-wrapped by the parser.
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) Attributes for the table element (default: {cellpadding = "2"})
blocks: (array) An ordered list of block functions that generate HTML fragments.
Each block function should accept (args, config) and return a string
containing HTML (e.g., complete <tr>...</tr> markup).
Returns:
A string containing the 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 the table element.
local tableEl = mw.html.create("table", tableAttrs)
tableEl:attr("class", tableClass)
-- Process each block function and append its HTML fragment to the table.
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 complete HTML as a string.
return tostring(tableEl)
end
return p