Module:Params/doc: Difference between revisions

Content deleted Content added
Amend incorrect statement
Function rename: `trimmed` -> `cut` – Update doc
Line 69:
Without modifiers this function is similar to writing <syntaxhighlight lang="wikitext" inline>{{{parameter name|}}}</syntaxhighlight> – with optional header and footer to be displayed only when the parameter is found. 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|trimmedcut|-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.
Line 315:
{{vpad|1.5em}}
 
=== <code>trimmedcut</code> ===
; Brief
: ''Remove zero or more parameters from the beginning and the end of the parameter list''
; Syntax
: <syntaxhighlight lang="wikitext" inline>{{#invoke:params|trimmedcut|left trim|right trim|pipe function name}}</syntaxhighlight>
 
The first argument indicates how many sequential parameters must be removed from the beginning of the parameter list, the second argument indicates how many sequential parameters must be removed from the end of the parameter list. If any of the two arguments is a negative number its absolute value indicates what must be left ''on the other side'' – i.e. <syntaxhighlight lang="wikitext" inline>{{#invoke:params|trimmedcut|-3|0|list}}</syntaxhighlight> indicates that the last three arguments must be listed.
 
Example:
 
<syntaxhighlight lang="wikitext">{{#invoke:params|trimmedcut|0|2|sequential|call_for_each_value|example template}}</syntaxhighlight>
 
In some cases it might be necessary to concatenate more than one invocation of the <code>trimmedcut</code> function. For instance, notice the <code><nowiki>|trimmedcut|1|0|trimmedcut|-1|0</nowiki></code> code in the following imaginary <code><nowiki>{{Foobar see also}}</nowiki></code> template in order to properly show the “and” conjunction and possibly an [[Serial comma|Oxford comma]] when more than two page names are provided:
 
<syntaxhighlight lang="wikitext">{{Hatnote|{{{altphrase|Foobar see also}}}: {{#if:{{{1|}}}
|[[{{{1}}}]]{{#invoke:params|sequential|trimmedcut|1|1|for_each|, [[$@]]}}{{#invoke:params|sequential|trimmedcut|1|0|trimmedcut|-1|0|for_each| and [[$@]]|{{#if:{{{3|}}}|,}}}}
|{{Error|{{tl|Foobar see also}} requires at least one page name}}
}}
}}</syntaxhighlight>
 
{{A note|Suggestion:}} Although <syntaxhighlight lang="wikitext" inline>{{#invoke:params|trimmedcut|-1|1|...}}</syntaxhighlight> de facto gets rid of all sequential parameters, it is clearer and more idiomatic to write <syntaxhighlight lang="wikitext" inline>{{#invoke:params|non-sequential|...}}</syntaxhighlight> to obtain the same effect. Writing <syntaxhighlight lang="wikitext" inline>{{#invoke:params|sequential|trimmedcut|-1|1|...}}</syntaxhighlight> will leave zero arguments to show.
 
{{vpad}}