Module:TemplateStarter: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 170: Line 170:
     end
     end
      
      
    -- No complex error handling here
     local templates = {}
     local templates = {}
      
      
    -- Check if ConfigRepository.templates exists
     if ConfigRepository.templates then
     if ConfigRepository.templates then
         local index = 1
         local index = 1
         for templateName, _ in pairs(ConfigRepository.templates) do
         for templateName, config in pairs(ConfigRepository.templates) do
             if templateName and templateName ~= "" then
            -- Include template if page_creator is not explicitly false
             if templateName and templateName ~= "" and (not config.meta or config.meta.page_creator ~= false) then
                 templates[index] = templateName
                 templates[index] = templateName
                 index = index + 1
                 index = index + 1
Line 184: Line 183:
         table.sort(templates)
         table.sort(templates)
     end
     end
     -- No fallback! If ConfigRepository fails, return empty list (single source of truth)
      
 
    -- Update cache
     templateListCache = templates
     templateListCache = templates
     templateListCacheTime = currentTime
     templateListCacheTime = currentTime
Line 195: Line 192:
-- Get list of available templates including variants
-- Get list of available templates including variants
function p.getAvailableTemplatesWithVariants()
function p.getAvailableTemplatesWithVariants()
     local templates = p.getAvailableTemplates()
    -- Get the filtered list of base templates that are allowed for page creation
     local baseTemplates = p.getAvailableTemplates()
     local templatesWithVariants = {}
     local templatesWithVariants = {}
    local index = 1
      
      
     -- Add base templates first
    -- Use a map to ensure uniqueness before adding to the final list
     for _, templateName in ipairs(templates) do
    local templateMap = {}
         templatesWithVariants[index] = templateName
   
        index = index + 1
     -- Add base templates to the map
     for _, templateName in ipairs(baseTemplates) do
         templateMap[templateName] = true
     end
     end
      
      
     -- Add variants
     -- Iterate through the allowed base templates to find their variants
     if ConfigRepository.templates then
     for _, templateName in ipairs(baseTemplates) do
        for templateName, config in pairs(ConfigRepository.templates) do
        local config = ConfigRepository.getConfig(templateName)
            if config.variants then
        if config and config.variants then
                for variantKey, variant in pairs(config.variants) do
            for _, variant in pairs(config.variants) do
                    -- Only include active variants
                -- A variant is included if it's active and not explicitly excluded
                    if variant.state and variant.name then
                if variant.state and variant.name and (not variant.meta or variant.meta.page_creator ~= false) then
                        templatesWithVariants[index] = variant.name
                    templateMap[variant.name] = true
                        index = index + 1
                    end
                 end
                 end
             end
             end
Line 220: Line 217:
     end
     end
      
      
     -- Sort the combined list
    -- Convert map keys to a list
    local index = 1
    for templateName, _ in pairs(templateMap) do
        templatesWithVariants[index] = templateName
        index = index + 1
    end
   
     -- Sort the final combined list
     table.sort(templatesWithVariants)
     table.sort(templatesWithVariants)