Module:Delimited template list
More actions
This module expands each non-empty item in a comma-separated list through a template. Whitespace surrounding each item is removed.
The separator between expanded templates is taken from MediaWiki:Comma-separator in the wiki's content language. Local message overrides are respected.
Usage
edit{{#invoke:Delimited template list|main
|ex=a,b
|template=Code
|prefix=font=
|suffix=
|leading=,
}}
This produces the equivalent of:
,{{code|font{{=}}a}}{{int:comma-separator}}{{code|font{{=}}b}}
Invocation from another template
editWhen ex is not passed directly to the module, it is read from the arguments of the template containing the invocation. This allows the module to be placed inline between other template output:
{{Previous template}}{{#invoke:Delimited template list|main
|template=Code
|prefix=font=
|leading=
}}{{Next template}}
If the containing template is called as {{Example|ex=a,b}}, the module uses that value automatically.
Parameters
editex- Comma-separated input. If omitted, the value is read from the parent template. Empty items are ignored.
template- Template expanded once for each item. The default is Template:Code.
prefix- Text prepended to every item before it is passed as the template's first unnamed argument. The default is empty.
suffix- Text appended to every item before it is passed as the template's first unnamed argument. The default is empty.
leading- Text placed before the first expanded template. The default is a comma without a following space. Pass an empty value to suppress it.
Limitations
editThe comma is always treated as a separator and cannot be escaped. Consequently, individual values cannot contain commas.
local p = {}
local function getArgument( frame, name )
local value = frame.args[name]
if value == nil then
value = frame:getParent().args[name]
end
return value
end
function p.main( frame )
local source = getArgument( frame, 'ex' ) or ''
local template = frame.args.template or 'Code'
local prefix = frame.args.prefix or ''
local suffix = frame.args.suffix or ''
local leading = frame.args.leading
if leading == nil then
leading = ','
end
local separator = mw.message.new( 'comma-separator' )
:inLanguage( mw.getContentLanguage() )
:useDatabase( true )
:plain()
local output = {}
for value in mw.text.gsplit( source, ',', true ) do
value = mw.text.trim( value )
if value ~= '' then
output[#output + 1] = frame:expandTemplate {
title = template,
args = { prefix .. value .. suffix }
}
end
end
if #output == 0 then
return ''
end
return leading .. table.concat( output, separator )
end
return p