Modul:HasValue

Aus Stadtsprachen
Wechseln zu:Navigation, Suche

Die Dokumentation für dieses Modul kann unter Modul:HasValue/Doku erstellt werden

local p = {}

-- Function to check if at least one parameter has a value
function p.hasAny(frame)
    local args = frame.args
    
    -- Check each argument
    for key, value in pairs(args) do
        -- Check if the value is not nil, not empty string, and not just whitespace
        if value and value ~= '' and mw.text.trim(value) ~= '' then
            return 1
        end
    end
    
    -- If no non-empty values found, return empty string (falsy in MediaWiki)
    return 0
end

-- Alternative function that accepts template parameters directly
function p.hasAnyTemplate(frame)
    local parentFrame = frame:getParent()
    local args = parentFrame.args
    
    -- Check each argument
    for key, value in pairs(args) do
        -- Check if the value is not nil, not empty string, and not just whitespace
        if value and value ~= '' and mw.text.trim(value) ~= '' then
            return 1
        end
    end
    
    -- If no non-empty values found, return empty string (falsy in MediaWiki)
    return 0
end

-- Function to check specific named parameters
function p.hasAnyNamed(frame)
    local args = frame.args
    local paramNames = {}
    
    -- Get the parameter names from the arguments
    for i = 1, 20 do  -- Support up to 20 parameters
        if args[i] then
            table.insert(paramNames, args[i])
        else
            break
        end
    end
    
    -- Get parent frame to access template parameters
    local parentFrame = frame:getParent()
    local templateArgs = parentFrame.args
    
    -- Check each named parameter
    for _, paramName in ipairs(paramNames) do
        local value = templateArgs[paramName]
        if value and value ~= '' and mw.text.trim(value) ~= '' then
            return 1
        end
    end
    
    return 0
end

return p