3,739 bytes added
, 05:16, 16 July 2021
This module counts the number of parameters that are passed to a template. For example, you might put the code <code><nowiki>{{#invoke:ParameterCount|all}}</nowiki></code> inside the <code><nowiki>{{my template}}</nowiki></code> template. If you then use <code><nowiki>{{my template}}</nowiki></code> like this:
<pre>
{{my template
| one
| two
| three
}}
</pre>
Then the <code><nowiki>{{#invoke:ParameterCount|all}}</nowiki></code> code inside the template will give the result <code>3</code>.
This module can be configured to count all parameters, or specific parameters defined by a template author. This module is only useful in templates. It should not be used on non-template pages.
The module has two functions available, <code>all</code> and <code>main</code>.
== all ==
The <code>all</code> function is used to count all parameters specified when using a template, regardless of whether or not they are used in the template itself.
'''Usage'''
<pre>{{#invoke:ParameterCount|all}}</pre>
'''Examples'''
If the code <code><nowiki>{{#invoke:ParameterCount|all}}</nowiki></code> was added to the <code><nowiki>{{my example template}}</nowiki></code> template, the code <code><nowiki>{{my example template|1|2|foo=bar}}</nowiki></code> would make ParameterCount produce <code>3</code>.
== main ==
The <code>main</code> function is used to count parameters with specific names. There are two ways of specifying parameters: by name, and using Lua patterns.
'''By name'''
<pre>{{#invoke:ParameterCount|main|1|2|3|abc|def}}</pre>
To specify a parameter by name, add the parameter name as a positional parameter.
'''By pattern'''
<pre>{{#invoke:ParameterCount|main|pattern1=^param%d+$|pattern2=^abc}}</pre>
To specify parameters by Lua Ustring pattern, add the pattern to {{para|pattern1}}, {{para|pattern2}} etc. For information on constructing Lua patterns, see the [http://lua-users.org/wiki/PatternsTutorial Lua Users' Wiki pattern tutorial], as well as the Lua reference manual sections on [[mw:Extension:Scribunto/Lua_reference_manual#Patterns|Lua patterns]] and [[mw:Extension:Scribunto/Lua_reference_manual#Ustring_patterns|Ustring patterns]].
'''Examples'''
If the code <code><nowiki>{{#invoke:ParameterCount|main|1|2|abc}}</nowiki></code> was added to the <code><nowiki>{{my example template}}</nowiki></code> template, the code <code><nowiki>{{my example template|1|2|abc=some value|other=some other value}}</nowiki></code> would make ParameterCount produce <code>3</code>.
If the code <code><nowiki>{{#invoke:ParameterCount|main|pattern1=^param%d+$}}</nowiki></code> was added to the <code><nowiki>{{my example template}}</nowiki></code> template, the code <code><nowiki>{{my example template|param1=a value|param2=another value|param5=yet another value}}</nowiki></code> would make ParameterCount produce <code>3</code>.
== Checking for blanks ==
<pre>{{#invoke:ParameterCount|all|checkblanks=no}}</pre>
<pre>{{#invoke:ParameterCount|main|checkblanks=no}}</pre>
By default, the module doesn't count parameters whose values contain only whitespace, e.g. {{para|abc| }}. If you wish to count all parameters, regardless of their value, use {{para|checkblanks|no}}. This works with both the <code>all</code> and <code>main</code> functions.
'''Examples'''
If the code <code><nowiki>{{#invoke:ParameterCount|all|checkblanks=no}}</nowiki></code> was added to the <code><nowiki>{{my example template}}</nowiki></code> template, the code <code><nowiki>{{my example template|1|2|foo=bar|baz=}}</nowiki></code> would make ParameterCount produce <code>4</code>.
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
<!-- Categories below this line, please; interwikis at Wikidata -->
}}</includeonly>