Module:For each named parameter/doc
| This is a documentation subpage for Module:For each named parameter. It may contain usage information, categories and other content that is not part of the original module page. |
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
[edit]Simple example
[edit]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
[edit]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
[edit]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
[edit]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.