Jump to content

Module:TemplateStructure: Difference between revisions

No edit summary
No edit summary
Tag: Reverted
Line 4: Line 4:
     p.render(args, config)
     p.render(args, config)
      
      
     Renders a table using a modular, block-based approach.
     Renders a table using a modular, block-based approach via the HTML builder API.
    This version generates an explicit <table> element with its rows inserted as raw HTML,
    so that MediaWiki’s parser won’t inject unwanted <p> or <br> elements.
      
      
     Parameters:
     Parameters:
Line 11: Line 13:
           tableClass: (string) CSS class for the table.
           tableClass: (string) CSS class for the table.
                       Default: "template-table"
                       Default: "template-table"
           tableAttrs: (string) Additional attributes for the table tag.
           tableAttrs: (table) A table of additional attributes for the table tag.
                       Default: 'cellpadding="2"'
                       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.
           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.
      
      
     Returns:
     Returns:
       A string containing the full wikitext markup for the table.
       A string containing the complete HTML markup for the table.
]]
]]
function p.render(args, config)
function p.render(args, config)
     config = config or {}
     config = config or {}
     local tableClass = config.tableClass or "template-table"
     local tableClass = config.tableClass or "template-table"
     local tableAttrs = config.tableAttrs or 'cellpadding="2"'
    -- Expect tableAttrs as a table; if not provided, default to cellpadding="2"
     local tableAttrs = config.tableAttrs or { cellpadding = "2" }
     local blocks = config.blocks or {}
     local blocks = config.blocks or {}


     -- Begin the table markup.
     -- Create a table element with the given attributes.
     local result = {}
     local tableEl = mw.html.create("table", tableAttrs)
    table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs))
    tableEl:attr("class", tableClass)
      
      
     -- Process each block function in the supplied order.
     -- Process each block function and insert its output as raw HTML.
     for i, block in ipairs(blocks) do
     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)
             if blockOutput and blockOutput ~= "" then
             if blockOutput and blockOutput ~= "" then
                 table.insert(result, blockOutput)
                 tableEl:raw(blockOutput)
             end
             end
         else
         else
Line 44: Line 43:
         end
         end
     end
     end
   
 
     table.insert(result, "|}")
     -- Return the full HTML string for the table.
     return table.concat(result, "\n")
     return tableEl:html()
end
end


return p
return p

Revision as of 04:38, 16 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 via the HTML builder API.
    This version generates an explicit <table> element with its rows inserted as raw HTML,
    so that MediaWiki’s parser won’t inject unwanted <p> or <br> elements.
    
    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) A table of 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 complete HTML markup for the table.
]]
function p.render(args, config)
    config = config or {}
    local tableClass = config.tableClass or "template-table"
    -- Expect tableAttrs as a table; if not provided, default to cellpadding="2"
    local tableAttrs = config.tableAttrs or { cellpadding = "2" }
    local blocks = config.blocks or {}

    -- Create a table element with the given attributes.
    local tableEl = mw.html.create("table", tableAttrs)
    tableEl:attr("class", tableClass)
    
    -- Process each block function and insert its output as raw HTML.
    for i, block in ipairs(blocks) do
        if type(block) == "function" then
            local blockOutput = block(args, config)
            if blockOutput and blockOutput ~= "" then
                tableEl:raw(blockOutput)
            end
        else
            mw.log("Warning: Block #" .. i .. " is not a function and will be skipped.")
        end
    end

    -- Return the full HTML string for the table.
    return tableEl:html()
end

return p