Module:TemplateStructure: Difference between revisions
Appearance
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 12: | Line 12: | ||
--[[ | --[[ | ||
p.render(args, config, errorContext) | p.render(args, config, errorContext) | ||
Parameters: | Parameters: | ||
args - Template parameters (passed to each block function) | args - Template parameters (passed to each block function) | ||
| Line 37: | Line 35: | ||
if continueOnError == nil then continueOnError = true end -- Default to true | if continueOnError == nil then continueOnError = true end -- Default to true | ||
-- Begin the table markup | -- 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)) | ||
Revision as of 05:39, 12 April 2025
Documentation for this module may be created at Module:TemplateStructure/doc
-- Module:TemplateStructure
-- A module for rendering templates block-based approach. Integrates with Module:ErrorHandling for centralized error reporting.
local ErrorHandling = require('Module:ErrorHandling')
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, errorContext)
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
continueOnError: Continue rendering if a block fails (default: true)
errorContext - Optional error context from ErrorHandling.createContext()
If provided, errors will be reported to this context
Returns:
Wikitext markup for the complete table
]]
function p.render(args, config, errorContext)
config = config or {}
local tableClass = config.tableClass or "template-table"
local tableAttrs = config.tableAttrs or 'cellpadding="2"'
local blocks = config.blocks or {}
local continueOnError = config.continueOnError
if continueOnError == nil then continueOnError = true end -- Default to true
-- 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
-- Protect block execution to avoid template failure
local success, blockOutput = pcall(function()
return block(args, config)
end)
if success then
blockOutput = trim(blockOutput)
if blockOutput ~= "" then
table.insert(result, blockOutput)
end
else
-- Get error message
local errorMsg = tostring(blockOutput)
-- Report to error context if available
if errorContext then
ErrorHandling.addError(
errorContext,
"TemplateStructure",
"Block #" .. i .. " execution failed",
errorMsg,
false
)
end
-- If we shouldn't continue on error, break the loop
if not continueOnError then
break
end
end
else
-- Report non-function blocks to error context if available
if errorContext then
ErrorHandling.addError(
errorContext,
"TemplateStructure",
"Block #" .. i .. " is not a function",
"Expected function, got " .. type(block),
false
)
end
end
end
-- Close the table
table.insert(result, "|}")
local finalOutput = table.concat(result, "\n")
return trim(finalOutput)
end
return p