Module:TemplateStructure: Difference between revisions
Appearance
Created page with "local p = {} --[[ p.render(args, config) args: Table of template parameters. config: Table that can contain: - tableClass: CSS class for the table (default: "template-table"). - tableAttrs: Additional attributes for the table tag (default: 'cellpadding="2"'). - blocks: An ordered array of functions. Each function receives (args, config) and returns a string (table rows or sections). Blocks that return an empty string are skipped. ]] function p.r..." |
No edit summary |
||
| Line 2: | Line 2: | ||
--[[ | --[[ | ||
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"' | |||
Note: Using cellpadding here is a legacy technique that ensures | |||
a minimum amount of spacing in table cells, which helps maintain | |||
readability and consistent rendering across different skins and | |||
browsers. It can be overridden via config if desired. | |||
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) | function p.render(args, config) | ||
| Line 16: | Line 28: | ||
local tableAttrs = config.tableAttrs or 'cellpadding="2"' | local tableAttrs = config.tableAttrs or 'cellpadding="2"' | ||
local blocks = config.blocks or {} | local blocks = config.blocks or {} | ||
-- Begin the table markup. | |||
local result = {} | local result = {} | ||
table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs)) | table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs)) | ||
for | -- Process each block function in the supplied order. | ||
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) | ||
| Line 26: | Line 40: | ||
table.insert(result, blockOutput) | table.insert(result, blockOutput) | ||
end | end | ||
else | |||
mw.log("Warning: Block #" .. i .. " is not a function and will be skipped.") | |||
end | end | ||
end | end | ||
table.insert(result, "|}") | table.insert(result, "|}") | ||
return table.concat(result, "\n") | return table.concat(result, "\n") | ||
Revision as of 15:12, 9 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.
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"'
Note: Using cellpadding here is a legacy technique that ensures
a minimum amount of spacing in table cells, which helps maintain
readability and consistent rendering across different skins and
browsers. It can be overridden via config if desired.
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
table.insert(result, blockOutput)
end
else
mw.log("Warning: Block #" .. i .. " is not a function and will be skipped.")
end
end
table.insert(result, "|}")
return table.concat(result, "\n")
end
return p