Content deleted Content added
Rating alpha is better |
Update doc |
||
Line 10:
<syntaxhighlight lang="wikitext">{{#invoke:params|[modifier]|[...]|[modifier]|[...]|function|[...]}}</syntaxhighlight>
For instance, as the name suggests, the [[#list|<code>list</code>]] function lists the parameters wherewith a template was called
<syntaxhighlight lang="wikitext">{{#invoke:params|
and such template were called with the following arguments,
{{tj|Example template|Beast of Bodmin: A large feline inhabiting Bodmin Moor
|Morgawr: A sea serpent
the following result would be produced:
: Owlman: A giant owl-like creature<br />Beast of Bodmin: A large feline inhabiting Bodmin Moor<br />Morgawr: A sea serpent
We can also do more sophisticated things, and by exploiting the possibility to set a header (<code>h</code>) and a footer (<code>f</code>) we an transform the previous code into a generator of [https://html.spec.whatwg.org/multipage/grouping-content.html#the-dl-element definition lists],
<syntaxhighlight lang="wikitext">{{#invoke:params|setting|h/p/i/f|<dl><dt>|</dt><dd>|</dd><dt>|</dd></dl>|list}}</syntaxhighlight>
thus yielding:
:; Beast of Bodmin
:: A large feline inhabiting Bodmin Moor
:; Morgawr
:: A sea serpent
:; Owlman
:: A giant owl-like creature
<syntaxhighlight lang="wikitext">{{#invoke:params|with_name_matching|n$|
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:
<syntaxhighlight lang="wikitext">{{#invoke:params|non-sequential|with_name_matching|^B|with_name_matching|n$|
The two modifiers [[#sequential|<code>sequential</code>]] and [[#non-sequential|<code>non-sequential</code>]] 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 {{para|1}} – this is known as the parameters' “sequence”. A parameter list that does not have a first parameter specified does not possess a sequence.
Line 61 ⟶ 69:
{{vpad|1.5em}}
=== <code>
; Brief
: ''Prepend numerical arguments to the current parameters, or impose non-numerical arguments; then propagate everything to a custom template''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|
}}</syntaxhighlight>
For example, if our <code><nowiki>{{Example template}}</nowiki></code> had the following code,
<syntaxhighlight lang="wikitext">{{#invoke:params|
and were called with,
{{tj|Example template|one|two|three|hello: world|wind: spicy}}
the following call to the <code><nowiki>{{Foo bar}}</nowiki></code> template would be performed:
{{tj|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.
{{A note}} All arguments passed to this function except the template name are not trimmed of their leading and trailing spaces. The <code>concat_and_call</code> function name itself, however, can be surrounded by spaces.
{{vpad|1.5em}}
=== <code>
; Brief
: ''Prepend numerical arguments to the current parameters, or impose non-numerical arguments; then propagate everything to a custom module''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|
}}</syntaxhighlight>
Exactly like [[#concat_and_call|<code>concat_and_call</code>]], but invokes a module instead of calling a template.
{{A note}} All arguments passed to this function except the module name and the function name are not trimmed of their leading and trailing spaces. The <code>
{{vpad|1.5em}}
=== <code>
; Brief
: ''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|
Without modifiers this function is similar to writing <syntaxhighlight lang="wikitext" inline>{{{parameter name|}}}</syntaxhighlight>. With modifiers, however, it allows to reach parameters that would be unreachable without knowing their number in advance. For instance, writing
<syntaxhighlight lang="wikitext">{{#invoke:params|cutting|-2|0|value_of|1}}</syntaxhighlight>
will write the value of the second-last sequential parameter, independently of how many parameters the template was called with. If no matching parameter is found this function expands to nothing. A header (<code>h</code>), a footer (<code>f</code>), and a fallback text (<code>n</code>) are available via the [[#setting|<code>setting</code>]] modifier (the strings assigned to the key-value pair delimiter (<code>p</code>) and the iteration delimiter (<code>i</code>) will be ignored).
{{vpad|1.5em}}
=== <code>
; Brief
: ''List the template parameters (both their names and their values)''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|
This function does not take arguments.
If the [[#setting|<code>setting</code>]] modifier was not placed before, this function will not add delimiters, but will return an indistinct blob of text in which keys and values are sticked to each other. A header (<code>h</code>), a key-value pair delimiter (<code>p</code>), an iteration delimiter (<code>i</code>), a footer (<code>f</code>), and a fallback text (<code>n</code>) are available via [[#setting|<code>setting</code>]].
For example, the following code
<syntaxhighlight lang="wikitext">{{#invoke:params|setting|h/i/p/f/n|'''Parameters passed:''' |); | (|)|'''No parameters were passed'''|list}}</syntaxhighlight>
will generate an output similar to the following.
: '''Parameters passed:''' Owlman (A giant owl-like creature); Beast of Bodmin (A large feline inhabiting Bodmin Moor); Morgawr (A sea serpent)
{{vpad|1.5em}}
=== <code>
; Brief
: ''List the values of the incoming parameters''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|
This function does not take arguments.
The [[#sequential|<code>sequential</code>]] modifier normally accompanies this function. If the [[#setting|<code>setting</code>]] modifier was not placed before, this function will not add delimiters, but will return an indistinct blob of text in which values are sticked to each other. A header (<code>h</code>), an iteration delimiter (<code>i</code>), a footer (<code>f</code>), and a fallback text (<code>n</code>) are available via [[#setting|<code>setting</code>]] (the string assigned to the key-value pair delimiter (<code>p</code>) will be ignored).
For example, the following code
<syntaxhighlight lang="wikitext">{{#invoke:params|setting|h/i/p/f/n|'''Parameters passed:''' |); | (|)|'''No parameters were passed'''|list}}</syntaxhighlight>
will generate an output similar to the following.
: '''Values of parameters passed:''' A giant owl-like creature; A large feline inhabiting Bodmin Moor; A sea serpent.
{{vpad|1.5em}}
Line 147 ⟶ 169:
}}</syntaxhighlight>
All
Calling a template for each key-value pair with
Line 182 ⟶ 204:
{{A note}} All arguments passed to this function except the module name and the function name are not trimmed of their leading and trailing spaces. The <code>invoke_for_each</code> function name itself, however, can be surrounded by spaces.
{{vpad|1.5em}}
=== <code>magic_for_each</code> ===
; Brief
: ''For each parameter passed to the caller template, call a magic word with at least two arguments (key and value)''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|magic_for_each|parser function|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...]}}</syntaxhighlight>
Exactly like [[#call_for_each|<code>call_for_each</code>]], but calls a [[:mw:Help:Magic words#Parser functions|parser function]] instead of a template.
{{A note}} All arguments passed to this function except the magic word are not trimmed of their leading and trailing spaces. The <code>magic_for_each</code> function name itself, however, can be surrounded by spaces.
{{vpad|1.5em}}
Line 191 ⟶ 225:
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|call_for_each_value|template name|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...]}}</syntaxhighlight>
For example, calling {{tl|tl}} with each parameter can be done by writing
<syntaxhighlight lang="wikitext">{{#invoke:params|sequential|setting|i|, |call_for_each_value|
This will be more efficient than writing
<syntaxhighlight lang="wikitext">{{#invoke:params|sequential|setting|i|, |for_each|{{
Furthermore, in the second example the <code>$@</code> token will be expanded ''after'' the {{tl|tl}} template has been called. In most cases this will make no difference, however there are several cases where it will lead to nonsensical results.
{{A note}} All arguments passed to this function except the template name are not trimmed of their leading and trailing spaces. The <code>call_for_each_value</code> function name itself, however, can be surrounded by spaces.
Line 226 ⟶ 260:
{{A note}} All arguments passed to this function except the module name and the function name are not trimmed of their leading and trailing spaces. The <code>invoke_for_each_value</code> function name itself, however, can be surrounded by spaces.
{{vpad|1.5em}}
=== <code>magic_for_each_value</code> ===
; Brief
: ''For each parameter passed to the caller template, call a magic word with at least one argument (i.e. the parameter's value)''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|magic_for_each_value|parser function|[append 1]|[append 2]|[...]|[append n]|[named param 1=value 1]|[...]|[named param n=value n]|[...]}}</syntaxhighlight>
Exactly like [[#call_for_each_value|<code>call_for_each_value</code>]], but calls a [[:mw:Help:Magic words#Parser functions|parser function]] instead of a template.
For example, if a template had the following code,
<syntaxhighlight lang="wikitext">{{#invoke:params|sequential|setting|ih|&preloadparams%5b%5d{{=}}|magic_for_each_value|urlencode|QUERY}}</syntaxhighlight>
and were transcluded as <syntaxhighlight lang="wikitext" inline>{{example template|hello world|àèìòù|foo bar}}</syntaxhighlight>, the <code><nowiki>{{</nowiki>[[:mw:Help:Magic words#URL data|urlencode]]:...|QUERY<nowiki>}}</nowiki></code> parser function would be called for each incoming parameter as first argument and with <code>QUERY</code> as second argument, and finally the returned text would be prefixed with <code>&preloadparams%5b%5d=</code>. This would generate,
<pre>&preloadparams%5b%5d=hello+world&preloadparams%5b%5d=%C3%A0%C3%A8%C3%AC%C3%B2%C3%B9&preloadparams%5b%5d=foo+bar</pre>
which can be used to allow [[:mw:Manual:Creating pages with preloaded text#Using parameters|the creation of pages with preloaded text and parameters]].
{{A note}} All arguments passed to this function except the magic word are not trimmed of their leading and trailing spaces. The <code>magic_for_each_value</code> function name itself, however, can be surrounded by spaces.
{{vpad|1.5em}}
Line 231 ⟶ 287:
=== <code>for_each</code> ===
; Brief
: ''For each parameter passed to the caller template, expand all occurrences of <code>$#</code> and <code>$@</code> within a given text, as key and value respectively''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|for_each|wikitext
Example:
Line 239 ⟶ 295:
<syntaxhighlight lang="wikitext">{{#invoke:params|for_each|Arg name: $#, Arg value: $@}}</syntaxhighlight>
{{A note}}
{{vpad}}
Line 277 ⟶ 333:
Using <code>non-sequential</code> together with [[#sequential|<code>sequential</code>]] will generate an error.
{{vpad|1.5em}}
=== <code>setting</code> ===
; Brief
: ''Set internal variables''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|setting|directives|...|pipe function name}}</syntaxhighlight>
This modifier allows to set some internal variables that will be used by other functions. The function takes a variable number of arguments, relying on the first argument to understand how many other arguments to read.
The first argument is a slash-separated list of lists of slots to assign; each slash-separated list maps exactly one argument; one slot is referred by exactly one character. A slot indicates which internal variable to set. If more than one slots are aggregated with the same slash-separated list the same text will be assigned to more than one variable.
The slots available are the following:
{| class="wikitable" style="margin-left: auto; margin-right: auto;"
|-
! Slots
! Variable
! Description
|-
| <code>i</code>
| ''Iteration delimiter''
| The string of text that will be placed between each iteration; it is never inserted unless there are two or more parameters to show.
|-
| <code>p</code>
| ''Key-value pair delimiter''
| The string of text that will be placed between each parameter name and its value; it is never inserted by functions that only iterate between values, or by functions that pass the key-value pairs to external calls.
|-
| <code>h</code>
| ''Header text''
| The string of text that will be placed before the iteration begins; it is inserted printed if there are no parameters to show.
|-
| <code>f</code>
| ''Footer text''
| The string of text that will be placed after the iteration is over; it is never inserted if there are no parameters to show.
|-
| <code>n</code>
| ''Fallback text''
| The string of text that will be placed if there are no parameters to show.
|}
Examples:
* <syntaxhighlight lang="wikitext" inline>{{#invoke:params|setting|i|{{!}}|list_values}}</syntaxhighlight>
*: ↳ Sets the value of ''iteration delimiter'' to <code>|</code>, then list all values
* <syntaxhighlight lang="wikitext" inline>{{#invoke:params|setting|ih|{{!}}|list_values}}</syntaxhighlight>
*: ↳ Sets the value of both ''header text'' and ''iteration delimiter'' to <code>|</code>, then list all values
* <syntaxhighlight lang="wikitext" inline>{{#invoke:params|setting|ih/p|{{!}}|{{=}}|list}}</syntaxhighlight>
*: ↳ Sets the value of both ''header text'' and ''iteration delimiter'' to <code>|</code>, set ''key-value pair delimiter'' to <code>=</code>, then list all parameters
* <syntaxhighlight lang="wikitext" inline>{{#invoke:params|setting|ih/p/n|{{!}}|{{=}}|No parameters were passed|list}}</syntaxhighlight>
*: ↳ Sets the value of both ''header text'' and ''iteration delimiter'' to <code>|</code>, set ''key-value pair delimiter'' to <code>=</code>, set ''fallback text'' to <code>No parameters were passed</code>, then list all parameters
In theory, instead of assigning different slots at once (i.e. <syntaxhighlight lang="wikitext" inline>{{...|setting|ih/p|{{!}}|{{=}}|...}}</syntaxhighlight>), it is possible to write separate invocations of <code>setting</code> for each variable, as in <syntaxhighlight lang="wikitext" inline>{{...|setting|ih|{{!}}|setting|p|{{=}}...}}</syntaxhighlight>. This method will be less efficient.
{{vpad|1.5em}}
Line 295 ⟶ 405:
<syntaxhighlight lang="wikitext">{{Hatnote|{{{altphrase|Foobar see also}}}: {{#if:{{{1|}}}
|[[{{{1}}}]]{{#invoke:params|sequential|cutting|1|1|for_each|, [[$@]]}}{{#invoke:params|sequential|cutting|1|0|cutting|-1|0|for_each
|{{Error|{{tl|Foobar see also}} requires at least one page name}}
}}
Line 314 ⟶ 424:
Example:
<syntaxhighlight lang="wikitext">{{#invoke:params|with_name_matching|n$|
{{A note}} The argument passed to this function is not trimmed of its leading and trailing spaces. The <code>with_name_matching</code> function name itself, however, can be surrounded by spaces.
Line 361 ⟶ 471:
Example:
<syntaxhighlight lang="wikitext">{{#invoke:params|with_value_matching|
{{A note}} The argument passed to this function is not trimmed of its leading and trailing spaces. The <code>with_value_matching</code> function name itself, however, can be surrounded by spaces.
Line 377 ⟶ 487:
Example:
<syntaxhighlight lang="wikitext">{{#invoke:params|with_value_not_matching|
{{A note}} The argument passed to this function is not trimmed of its leading and trailing spaces. The <code>with_value_not_matching</code> function name itself, however, can be surrounded by spaces.
|