Module:Forward parameters to template call
Usage
{{subst:#invoke:Forward parameters to template call|main|_template=template name}}
This module is for a very particular set of circumstances: when a substituted template needs to dynamically generate the wikimarkup for a template call, forwarding all numbered parameters. It also allows for the option of designating a prefix for a series of named parameters which will be converted to numbered ones. Because it generates wikitext, this module only works when substituted, unless all you want is to show the template call's markup without actually executing it.
To specify a prefix to convert to numbered parameters, set {{{_convert_prefix}}}
(or {{{_convert_prefix1}}}
, {{{_convert_prefix2}}}
, etc.) The order of items within that family of parameters will be retained, with those items being placed after any true numbered parameters. For instance, {{subst:#invoke:Forward parameters to template call|main|_template=foo|_convert_prefix=bar|1|2|bar2=b|bar1=a}}
will become {{foo|1=1|2=2|3=a|4=b}}
. If multiple prefixes are converted, order of items within each set is retained, but the ordering of the sets is unpredictable.
local p = {}
local getArgs = require('Module:Arguments').getArgs
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
function convert(args)
local filter = {}
local convertPrefixes = {}
local numberedParams = {}
for arg, val in pairs(args) do
if arg:match("_convert_prefix") then
convertPrefixes[#convertPrefixes + 1] = val
end
end
for arg, val in pairs(args) do
if arg == tonumber(arg) then
numberedParams[#numberedParams + 1] = val
else
for prefix in convertPrefixes do
if arg == convertPrefix then
filter[prefix][0] = val
break
else
local prefixnum = arg:match(convertPrefix .. "(%d)")
if prefixnum then
filter[prefix][prefixnum] = val
end
break
end
end
end
end
for _, values in ipairs(filter) do
for _, val in ipairs(values) do
numberedParams[#numberedParams + 1] = val
end
end
end
function p._main(args)
local markup = "{{" .. args.template
local convertedArgs = convert(args)
for i, val in ipairs(convertedArgs) do
markup = markup .. "|" .. i .. "=" .. val
end
return markup .. "}}"
end
return p