Jump to content

Module:TemplateStructure: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
Line 4: Line 4:
     p.render(args, config)
     p.render(args, config)
      
      
     Renders a table using a modular, block-based approach via the HTML builder API.
     Renders a table using a modular, block-based approach.
    Instead of returning wiki table markup (which is later reinterpreted and escaped),
    this version builds and returns an mw.html object so that the output is treated as raw HTML.
      
      
     Parameters:
     Parameters:
Line 13: Line 11:
           tableClass: (string) CSS class for the table.
           tableClass: (string) CSS class for the table.
                       Default: "template-table"
                       Default: "template-table"
           tableAttrs: (table) A table of additional attributes for the table tag.
           tableAttrs: (string) 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 an HTML fragment.
                       Each block function should accept (args, config) and return a string.
      
      
     Returns:
     Returns:
       An mw.html object representing the complete table.
       A string containing the full wikitext markup for the table.
   
    NOTE:
      This approach expects that the block functions produce HTML (or HTML fragments) rather than
      wiki markup. For example, instead of returning "{| ..." rows and "|}" markers, block functions
      should generate <tr>, <td>, or <th> elements. This change moves layout control into Lua so that
      MediaWiki’s parser won’t rewrap or escape the output.
]]
]]
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" }
     local tableAttrs = config.tableAttrs or 'cellpadding="2"'
     local blocks = config.blocks or {}
     local blocks = config.blocks or {}


     -- Create a table element using the HTML builder API.
     -- Begin the table markup.
     local tableEl = mw.html.create("table", tableAttrs)
     local result = {}
    tableEl:attr("class", tableClass)
    table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs))
      
      
     -- Iterate over each block function, and insert its output as wikitext (which here is assumed to be safe HTML).
     -- Process each block function in the supplied order.
     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
                 tableEl:wikitext(blockOutput)
                 table.insert(result, blockOutput)
             end
             end
         else
         else
Line 48: Line 44:
         end
         end
     end
     end
 
   
     -- Return the mw.html object so that its output is not re-escaped.
     table.insert(result, "|}")
     return tableEl
     return table.concat(result, "\n")
end
end


return p
return p

Revision as of 05:02, 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.
    
    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