Jump to content

Module:TemplateStructure

Revision as of 12:12, 16 February 2025 by MarkWD (talk | contribs)

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