Module:TemplateStructure: Difference between revisions
Appearance
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Local trim function: removes leading and trailing whitespace. | |||
local function trim(s) | |||
return (s:gsub("^%s+", ""):gsub("%s+$", "")) | |||
end | |||
--[[ | --[[ | ||
| Line 13: | Line 18: | ||
tableAttrs: (string) Additional attributes for the table tag. | tableAttrs: (string) Additional attributes for the table tag. | ||
Default: 'cellpadding="2"' | 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. | ||
| Line 38: | Line 39: | ||
local blockOutput = block(args, config) | local blockOutput = block(args, config) | ||
if blockOutput and blockOutput ~= "" then | if blockOutput and blockOutput ~= "" then | ||
blockOutput = trim(blockOutput) | |||
table.insert(result, blockOutput) | table.insert(result, blockOutput) | ||
end | end | ||
| Line 46: | Line 48: | ||
table.insert(result, "|}") | table.insert(result, "|}") | ||
local finalOutput = table.concat(result, "\n") | |||
return trim(finalOutput) | |||
end | end | ||
return p | return p | ||
Revision as of 20:09, 16 February 2025
Documentation for this module may be created at Module:TemplateStructure/doc
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 - 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: (string) 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 full wikitext 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 {}
-- 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)
if blockOutput and blockOutput ~= "" then
blockOutput = trim(blockOutput)
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