Module:TemplateStructure: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 1: Line 1:
-- Module:TemplateStructure
-- Module:TemplateStructure
-- A module for rendering templates block-based approach. Integrates with Module:ErrorHandling for centralized error reporting.
-- A module for rendering templates block-based approach. Integrates with Module:ErrorHandling for centralized error reporting.
-- Provides block rendering functions for templates, including title blocks, divider blocks, and field tables.


local ErrorHandling = require('Module:ErrorHandling')
local ErrorHandling = require('Module:ErrorHandling')
Line 93: Line 94:
     local finalOutput = table.concat(result, "\n")
     local finalOutput = table.concat(result, "\n")
     return trim(finalOutput)
     return trim(finalOutput)
end
-- Renders a standard title block with configurable class and text (basic version)
function p.renderTitleBlock(args, titleClass, titleText)
    titleClass = titleClass or "template-title"
    return string.format('|-\n! colspan="2" class="%s" | %s', titleClass, titleText)
end
-- Renders a standard divider block with optional label
function p.renderDividerBlock(label)
    if label and label ~= "" then
        return string.format('|-\n| colspan="2" class="template-divider" |\n|-\n| colspan="2" class="icannwiki-centered" | <span class="icannwiki-bold">%s</span>', label)
    else
        return '|-\n| colspan="2" class="template-divider" |'
    end
end
--[[
    Renders a table of fields with labels and values using TemplateStructure.
   
    Parameters:
      fields      - Array of field objects, each with:
                    - label: The field label to display
                    - value: The field value to display
                    - class: Optional CSS class for the row
      options      - Optional configuration:
                    - tableClass: CSS class for the table (default: "template-field-table")
                    - tableAttrs: Additional table attributes
                    - fieldFormat: Format string for field rows (default: uses FIELD_FORMAT)
                    - errorContext: Optional error context for error handling
   
    Returns:
      Wikitext markup for the field table
]]
function p.renderFieldTable(fields, options)
    -- Early return for empty fields
    if not fields or #fields == 0 then
        return ""
    end
   
    options = options or {}
   
    -- Define the field table rendering operation
    local function renderFieldTableOperation(fields, options)
        -- Create a config for the render function with optimized defaults
        local config = {
            tableClass = options.tableClass or "template-field-table",
            tableAttrs = options.tableAttrs or 'cellpadding="2"',
            blocks = {}
        }
       
        -- Pre-allocate blocks array based on field count
        local blocks = {}
       
        -- Get the field format from options or use a default
        local fieldFormat = options.fieldFormat or '|- class="template-data-row"\n| class="template-label-cell" | <span class="template-label-style">%s</span>\n| class="template-value-cell" | %s'
       
        -- Create a block function for each field with direct index assignment
        for i = 1, #fields do
            local field = fields[i]
            blocks[i] = function()
                -- Combine the field's class with the template-data-row class
                local fieldClass = field.class and field.class or ""
               
                -- Create the row with proper classes
                local row = '|- class="template-data-row' .. (fieldClass ~= "" and ' ' .. fieldClass or '') .. '"'
               
                -- Format the cells using the field format but replace the row start
                local cellsFormat = fieldFormat:gsub("^[^|]+", "")
               
                -- Return the complete row
                return row .. string.format(cellsFormat, field.label, field.value)
            end
        end
       
        -- Assign blocks to config
        config.blocks = blocks
       
        -- Use TemplateStructure's render function
        return p.render({}, config, options.errorContext)
    end
   
    -- Use error handling if provided
    if options.errorContext then
        return ErrorHandling.protect(
            options.errorContext,
            "renderFieldTable",
            renderFieldTableOperation,
            "", -- Empty string fallback
            fields, options
        )
    else
        -- Direct execution without error handling
        return renderFieldTableOperation(fields, options)
    end
end
end


return p
return p