Jump to content

Module:TemplateStructure

Revision as of 13:21, 8 February 2025 by MarkWD (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:TemplateStructure/doc

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.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 {}
    
    local result = {}
    table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs))
    
    for _, 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
        end
    end

    table.insert(result, "|}")
    return table.concat(result, "\n")
end

return p