Module:Loop

From PathfinderWiki

This loop implementation loops over arguments in the form of somename1, somename2. It only expands wikitext afterwards which makes it safe to use with categories and other templates.

Usage: {{ #invoke:Loop | arguments | myArgumentName | [[Category:{{{myArgumentName$n$}}}]] }}

resulting in effectively {{#if: {{{myArgumentName1|}}}|[[Category:{{{myArgumentName1}}}]]| }} {{#if: {{{myArgumentName2|}}}|[[Category:{{{myArgumentName2}}}]]| }} {{#if: {{{myArgumentName3|}}}|[[Category:{{{myArgumentName3}}}]]| }} {{#if: {{{myArgumentName4|}}}|[[Category:{{{myArgumentName4}}}]]| }} {{#if: {{{myArgumentName5|}}}|[[Category:{{{myArgumentName5}}}]]| }}


local p = {} --p stands for package

function p.arguments( frame )
    local argumentName = frame.args[1]
    argumentName = mw.ustring.match(argumentName, "^%s*(.-)%s*$") -- trim spaces
    local text = frame.args[2]

    local result = "";
    for i=1,30 do
        local name = argumentName..i
        local parsed = mw.ustring.gsub( text, "{{{ *"..argumentName.." *}}}", "{{{"..name .."}}}" )
        parsed = mw.ustring.gsub( parsed, "{{{ *"..argumentName.."%$n%$ *}}}", "{{{"..name.."}}}" )
        result = result .. "{{#if: {{{"..name.."|}}}|"..parsed.."| }}"
    end
    return frame:getParent():preprocess(result)
end

return p