Module:For each named parameter
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
Example demonstrating why <nowiki> tags are recommended
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:
| 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
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.