Module:TemplateStructure: Difference between revisions
Appearance
minor rev // via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 14: | Line 14: | ||
Parameters: | Parameters: | ||
args - | args - Template parameters (passed to each block function) | ||
config - | config - Configuration options: | ||
tableClass: | tableClass: CSS class for the table (default: "template-table") | ||
tableAttrs: Additional table attributes (default: 'cellpadding="2"') | |||
tableAttrs: | blocks: Functions that generate table rows | ||
Each accepts (args, config) and returns a string | |||
blocks: | |||
Each | |||
Returns: | Returns: | ||
Wikitext markup for the complete table | |||
]] | ]] | ||
function p.render(args, config) | function p.render(args, config) | ||
Revision as of 22:11, 7 April 2025
Documentation for this module may be created at Module:TemplateStructure/doc
-- Module:TemplateStructure
local p = {}
-- Local trim function: removes leading and trailing whitespace.
local function trim(s)
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
end
--[[
p.render(args, config)
Renders a table using a modular, block-based approach.
Parameters:
args - Template parameters (passed to each block function)
config - Configuration options:
tableClass: CSS class for the table (default: "template-table")
tableAttrs: Additional table attributes (default: 'cellpadding="2"')
blocks: Functions that generate table rows
Each accepts (args, config) and returns a string
Returns:
Wikitext markup for the complete 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 {}
-- Begin the table markup.
local result = {}
table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs))
-- Process each block function in the supplied order.
for i, block in ipairs(blocks) do
if type(block) == "function" then
local blockOutput = block(args, config)
blockOutput = trim(blockOutput)
if blockOutput ~= "" then
table.insert(result, blockOutput)
end
else
mw.log("Warning: Block #" .. i .. " is not a function and will be skipped.")
end
end
table.insert(result, "|}")
local finalOutput = table.concat(result, "\n")
return trim(finalOutput)
end
return p