Jump to content

User:BrandonXLF/Help:Infinite parameters

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 23:59, 1 September 2024 (Added content about custom modules and a lead). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Wikitext does not have support for infinite parameters, so templates that use infinite parameters make use of Lua modules.

Custom modules

One way to support infinite parameters is to write a custom Lua modules that implements the template. This is done by modules like Module:Jct, Module:Infobox, Module:List, and Module:Template link general. There are many different strategies for supporting infinite parameters in Lua, some of which are demonstrated below. Also check out modules like Module:TableTools for working with Lua tables.

Using ipairs

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local out = ''

    for i, v in ipairs(args) do
        if out then out = out .. ', ' end
        out = out .. i .. ' is ' .. v
    end

    return out
end

return p

When a template with {{#invoke:__MODULE__|main}} is called with {{__TEMPLATE__|A|B|c}}, the output 1 is A, 2 is B, 3 is C would be produced.

Using while

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local out = ''
    local i = 1

    while args['prefix' .. i] do
        if out then out = out .. ', ' end
        out = out .. 'prefix' .. i .. ' is ' .. v
        i = i + 1
    end

    return out
end

return p

When a template with {{#invoke:__MODULE__|main}} is called with {{__TEMPLATE__|prefix1=A|prefix2=B|prefix3=c}}, the output 1 is A, 2 is B, 3 is C would be produced.

Using metamodules

If you'd like to avoid writing an entire Lua module, there are metamodules that exist to help aid in adding infinite parameter support for functions.

Arbitrary wikitext

These modules work by taking wikitext in <nowiki>...</nowiki> and expanding it while passing it the appropriate parameters.

Unnamed parameters

__TEMPLATE__:

{{#invoke:For nowiki|template|<br>|<nowiki><code>{{{i}}}</code> is {{2x|{{{1}}}|, }}</nowiki>}}
{{__TEMPLATE__|A|B|C}}
produces 1 is A, A
2 is B, B
3 is C, C

__TEMPLATE__:

{{#if:{{{1|}}}|<code>1</code> is {{2x|{{{1}}}|, }}}}{{#invoke:ArgRest|main|<nowiki><br><code>?</code> is {{2x|{{{2}}}|, }}</nowiki>|2}}
{{__TEMPLATE__|A|B|C}}
produces

1 is A, A
? is B, B
? is C, C

__TEMPLATE__:

{{#invoke:Params|setting|i|<br>|sequential|for_each|<code>$#</code> is {{2x|$@|, }}}}
{{__TEMPLATE__|A|B|C}}
produces 1 is A, A
2 is B, B
3 is C, C

Named parameters

__TEMPLATE__:

{{#invoke:For nowiki/sandbox|template|prefix=item|<br>|
<nowiki><code>item{{{i}}}</code> is {{{1}}}{{#if:{{{extra{{{i}}}|}}}|{{sp}}and <code>extra{{{i}}}</code> is {{{extra{{{i}}}}}}}}</nowiki>}}
{{__TEMPLATE__
| item1 = A
| extra1 = Apple
| item2 = B
| extra2 = Orange
| item3 = C
}}
produces

__TEMPLATE__:

{{#if:{{{item1|}}}|<code>item1</code> is {{{item1}}}{{#if:{{{extra1|}}}|{{sp}}and <code>extra1</code> is {{{extra1}}}}}}}{{#invoke:ArgRest|main|<nowiki><br><code>item?</code> is {{{item2}}}{{#if:{{{extra2|}}}|{{sp}}and <code>extra?</code> is {{{extra2}}}}}</nowiki>|item2}}
{{__TEMPLATE__
| item1 = A
| extra1 = Apple
| item2 = B
| extra2 = Orange
| item3 = C
}}
produces item1 is A and extra1 is Apple
item? is B and extra? is Orange
item? is C

__TEMPLATE__:

{{#invoke:params|setting|i|<br>|all_sorted|call_for_each_group|__CHILD_TEMPLATE__}}

__CHILD_TEMPLATE__:

<code>item?</code> is {{{item}}}{{#if:{{{extra|}}}|{{sp}} and <code>extra?</code> is {{{extra}}}}}
{{__TEMPLATE__
| item1 = A
| extra1 = Apple
| item2 = B
| extra2 = Orange
| item3 = C
}}
produces item? is A and extra? is Apple
item? is B and extra? is Orange
item? is C

Fixed output format

Unnamed parameters

__TEMPLATE__:

{{#invoke:Separated entries|main|separator=&#32;also&#32;|conjunction=&#32;and finally&#32;}}
{{__TEMPLATE__|A|B|C}}
produces A also B and finally C

__TEMPLATE__:

{{#invoke:Enumerate|main}}
{{__TEMPLATE__|A|B|C}}
produces
  • A
  • B
  • C

Named parameters

__TEMPLATE__:

{{#invoke:Enumerate|main|item}}
{{__TEMPLATE__|item1=A|item2=B|item3=C}}
produces
  • A
  • B
  • C

See also