Jump to content

Module:Params/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Grufo (talk | contribs) at 20:31, 9 July 2023 (First draft). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)


The {{#invoke:params}} module is designed to be adopted by those templates that want to have a deep control of their parameters. It is particularly useful to variadic templates, to which it offers the possibility to count, list, map and propagate the parameters received without knowing their number in advance.

The module offers elegant shortcuts to non variadic templates as well. Outside templates the module has virtually no applications; hence, if you plan to make experiments, make sure to do them from within a template, or you will not be able to see much.

Among the possibilities that the module offers there is that of performing a series of actions after novel arguments have been concatenated to the template's incoming parameters. As this sometimes makes it necessary to keep the parameter slots clean from intereferences, instead of named arguments in order to specify options this module uses piping functions (i.e. functions that expect to be piped instead of returning to the caller), or modifiers. This creates a syntax similar to the following example:

{{#invoke:params|[modifier]|[...]|[modifier]|[...]|function|[...]}}

For instance, as the name suggests, the list function lists the parameters wherewith a template was called; if we imagined a template named {{Example template}} containing the following wikitext,

{{#invoke:params|list|</dd><dt>|</dt><dd>|<dl><dt>|</dd></dl>}}

and such template were called with the following arguments,

{{Example template|Beast of Bodmin=A large feline inhabiting Bodmin Moor.|Morgawr=A sea serpent.|Owlman=A giant owl-like creature.}}

the following result would be produced:

Beast of Bodmin
A large feline inhabiting Bodmin Moor.
Morgawr
A sea serpent.
Owlman
A giant owl-like creature.

However, by placing the with_name_matching modifier before the list function we would be able to filter some parameters out – such as, for instance, all parameter names that do not end with an “n”:

{{#invoke:params|with_name_matching|n$|list|</dd><dt>|</dt><dd>|<dl><dt>|</dd></dl>}}

Thus, the previous code would produce:

Beast of Bodmin
A large feline inhabiting Bodmin Moor.
Owlman
A giant owl-like creature.}}

This mechanism has the intrinsic advantage that it allows to concatenate infinite modifiers. And so, in order to get the accurate result that we want to obtain we could write:

{{#invoke:params|with_name_matching|n$|with_name_matching|^A|non-sequential|with_name_matching|^E|list}}

The two modifiers sequential and non-sequential refer to a technical jargon used in wikitext: given a parameter list, the subgroup of sequential parameters is constituted by the largest group of consecutive numerical parameters starting from |1=; this is known as the parameters' “sequence”. A parameter list that does not have a first parameter specified does not possess a sequence.

Functions

Here follows the list of functions. All modifiers are appliable to each function (see § Modifiers).

count

Brief
Counts the number of parameters whereby a template was called
Syntax
{{#invoke:params|count}}

This function does not take arguments.

list

Brief
Lists the template parameters (both their names and their values)
Syntax
{{#invoke:params|list|[argument separator]|[key-value separator]|[header]|[footer]}}

If no arguments are passed the vertical bar (|) will be used as parameter separator and the equals sign (=) will be used as key-value separator, with no header and no footer.

list_values

Brief
Lists the template parameter values
Syntax
{{#invoke:params|list_values|[argument separator]|[header]|[footer]}}

If no arguments are passed the vertical bar (|) will be used as parameter separator, with no header and no footer.

merge_and_call

Brief
Prepends user-given unnamed arguments to the current parameters, or impose user-given named arguments; then propagate everything to a custom template
Syntax
{{#invoke:params|merge_and_call|template name|[prepend 1]|[prepend 2]|[...]|[item n]|[named item 1=value 1]|[...]|[named item n=value n]|[...] }}

For example, if our {{Example template}} had the following code,

{{#invoke:params|merge_and_call|foo bar|elbow|earth|room|7=classy|hello=not today}}

and it were called with,

{{example template|one|two|three|hello=world|wind=spicy}}

the following call to the {{Foo bar}} template would be performed:

{{foo bar|elbow|earth|room|7=classy|8=one|9=two|10=three|wind=spicy|hello=not today}}

If no other argument besides the template name are provided this function simply echoes the current parameters to another template.

merge_and_invoke

Brief
Prepends user-given unnamed arguments to the current parameters, or impose user-given named arguments; then propagate everything to a custom module
Syntax
{{#invoke:params|merge_and_invoke|template name|[prepend 1]|[prepend 2]|[...]|[item n]|[named item 1=value 1]|[...]|[named item n=value n]|[...] }}

Exactly like merge_and_call, but invokes a module instead of calling a template.

call_for_each

Brief
For each parameter passed to the caller template, calls a custom template with at least two parameters (key and value)
Syntax
{{#invoke:params|call_for_each|template name|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...] }}

All other unnamed parameters following the template name will be appended to the key-value pair. Named parameters will be passed verbatim.

invoke_for_each

Brief
For each parameter passed to the caller template, invokes a custom module function with at least two arguments (key and value)
Syntax
{{#invoke:params|invoke_for_each|module name|module function|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...]}}

Exactly like call_for_each, but invokes a module instead of calling a template.

call_for_each_value

Brief
For each parameter passed to the caller template, calls a custom template with at least one parameter (i.e. the parameter's value)
Syntax
{{#invoke:params|call_for_each_value|template name|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...]}}

All other unnamed parameters following the template name will be appended after the value parameter. Named parameters will be passed verbatim.

invoke_for_each_value

Brief
For each parameter passed to the caller template, invokes a custom module function with at least one argument (i.e. the parameter's value)
Syntax
{{#invoke:params|invoke_for_each_value|module name|module function|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...]}}

Exactly like call_for_each_value, but invokes a module instead of calling a template.

for_each

Brief
For each parameter passed to the caller template, expands all occurrences of $# and $@ within a given text, as key and value respectively
Syntax
{{#invoke:params|for_each|wikitext|[header]|[footer]}}

Example:

{{#invoke:params|for_each|Arg name: $#, Arg value: $@}}

Modifiers (piping functions)

sequential

Brief
Reduces the parameter list to the subgroup of consecutive parameters that follow |1=
Syntax
{{#invoke:params|sequential|pipe function name}}

Example:

{{#invoke:params|sequential|count}}

This function does not take arguments besides the name of the function that must follow.

non-sequential

Brief
Reduces the parameter list by subtracting the subgroup of consecutive parameters that follow |1=
Syntax
{{#invoke:params|non-sequential|pipe function name}}

Example:

{{#invoke:params|non-sequential|list|{{!}}|{{=}}|<nowiki>{{</nowiki>foobar{{!}}|<nowiki>}}</nowiki>}}

This function does not take arguments besides the name of the function that must follow.

with_name_matching

Brief
Removes from the parameter list all the parameters whose name does not match the given pattern
Syntax
{{#invoke:params|with_name_matching|pattern|pipe function name}}

Example:

{{#invoke:params|with_name_matching|n$|list|</dd><dt>|</dt><dd>|<dl><dt>|</dd></dl>}}

with_name_not_matching

Brief
Removes from the parameter list all the parameters whose name matches the given pattern
Syntax
{{#invoke:params|with_name_not_matching|pattern|pipe function name}}

Example:

{{#invoke:params|with_name_not_matching|n$|count}}

with_value_matching

Brief
Removes from the parameter list all the parameters whose value does not match the given pattern
Syntax
{{#invoke:params|with_value_matching|pattern|pipe function name}}

Example:

{{#invoke:params|with_value_matching|n$|count}}

with_value_not_matching

Brief
Removes from the parameter list all the parameters whose value matches the given pattern
Syntax
{{#invoke:params|with_value_not_matching|pattern|pipe function name}}

Example:

{{#invoke:params|with_value_not_matching|n$|count}}

trimmed

Brief
Removes zero or more parameters from the beginning and/or the end of the parameter list
Syntax
{{#invoke:params|trimmed|left trim|right trim|pipe function name}}

Example:

{{#invoke:params|trimmed|0|2|sequential|call_for_each_value|example template}}

See also