Jump to content

Module:For each named parameter

From Elite Dangerous Wiki
Revision as of 18:00, 3 July 2026 by Wikipedia>MusikBot II (Protected "Module:For each named parameter": High-risk template or module: 333 transclusions (more info) ([Edit=Require autoconfirmed or confirmed access] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module allows templates to take arbitrary named parameters and execute wiki code for each name-value pair.

Syntax:

{{#invoke:For each named parameter|main|separator|code=code}}

code can also be given without a name:

{{#invoke:For each named parameter|main|separator|code}}

Runs code for each named parameter of the parent template, with the name available as {{{name}}} and the value as {{{value}}}. The results are concatenated and separated by separator.

The code should be wrapped in <nowiki>...</nowiki> tags. Otherwise, it is evaluated before the module is invoked, which may lead to unexpected results.

Examples

Simple example

Module:For each named parameter/example0 has the wikitext:

{{#invoke:For each named parameter|main|; |{{{name}}}: {{{value}}}}}

Hence, calling it with some params:

{{Module:For each named parameter/example0|a=b|c=d|e=f}}

produces:

a: b; c: d; e: f

Module:For each named parameter/example0 has the wikitext:

{{#invoke:For each named parameter|main|; |{{{name}}}: {{{value}}}}}

Note that the code is not wrapped in <nowiki>...</nowiki> tags. Hence, calling it with params name and value:

{{Module:For each named parameter/example0|name=name was replaced|value=value was replaced|a=b|c=d}}

produces:

name was replaced: value was replaced; name was replaced: value was replaced; name was replaced: value was replaced; name was replaced: value was replaced

Example with line breaks

Module:For each named parameter/example1 has the wikitext:

{{#invoke:For each named parameter|main|

|<nowiki>* {{{name}}} -> {{{value}}}</nowiki>}}

(Note the line breaks in the separator.)

Hence, calling it with some params:

{{Module:For each named parameter/example1|a=b|c=d|e=f}}

produces:

  • a -> b
  • c -> d
  • e -> f

Example with special handling of a parameter

Module:For each named parameter/example2 has the wikitext:

{| class="wikitable"
|+ {{{title|Title}}}
|-
! Name
! Value
|-
{{#invoke:For each named parameter|main|
{{!}}-
|code=<nowiki>{{#ifeq: {{{name}}} | title ||
{{!}} {{{name}}}
{{!}} {{{value}}}
}}</nowiki>}}
|}

It generates a table row for each parameter, but not for the title parameter (since it should only appear as the table title). Instead, it produces an empty string (note the ||). Generally, if the code produces an empty string for a parameter, the module omits it and also does not emit a separator.

Hence, calling it with these params:

{{Module:For each named parameter/example2|title=My Title|a=b|c=d|e=f}}

produces:

My Title
Name Value
a b
e f
c d

Note that due to technical limitations (of Lua tables), the order in which the parameters are processed cannot be guaranteed.

See also


local p = {}

function p.main(frame) 
	local code = mw.text.unstripNoWiki(frame.args.code or frame.args[2])
	local results = {}
	local sep = frame.args[1] or ''
	for key, value in pairs(frame:getParent().args) do
		if type(key) ~= 'number' then
			local actualCode = code:gsub("{{{([^{}|]*)|?[^{}]*}}}", {name = key, value = value})
			local result = frame:getParent():preprocess(actualCode)
			if #result > 0 then
				table.insert(results, result)
			end
		end
	end
	return table.concat(results, sep)
end
return p