This module counts the number of parameters that are passed to a template. For example, you might put the code <nowiki>{{#invoke:ParameterCount|all}}</nowiki>
inside the <nowiki>{{my template}}</nowiki>
template. If you then use <nowiki>{{my template}}</nowiki>
like this:
Then the <nowiki>{{#invoke:ParameterCount|all}}</nowiki>
code inside the template will give the result 3
.
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, all
and main
.
The all
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
Examples
If the code <nowiki>{{#invoke:ParameterCount|all}}</nowiki>
was added to the <nowiki>{{my example template}}</nowiki>
template, the code <nowiki>{{my example template|1|2|foo=bar}}</nowiki>
would make ParameterCount produce 3
.
The main
function is used to count parameters with specific names. There are two ways of specifying parameters: by name, and using Lua patterns.
By name
To specify a parameter by name, add the parameter name as a positional parameter.
By pattern
To specify parameters by Lua Ustring pattern, add the pattern to, etc. For information on constructing Lua patterns, see the Lua Users' Wiki pattern tutorial, as well as the Lua reference manual sections on Lua patterns and Ustring patterns.
Examples
If the code <nowiki>{{#invoke:ParameterCount|main|1|2|abc}}</nowiki>
was added to the <nowiki>{{my example template}}</nowiki>
template, the code <nowiki>{{my example template|1|2|abc=some value|other=some other value}}</nowiki>
would make ParameterCount produce 3
.
If the code <nowiki>{{#invoke:ParameterCount|main|pattern1=^param%d+$}}</nowiki>
was added to the <nowiki>{{my example template}}</nowiki>
template, the code <nowiki>{{my example template|param1=a value|param2=another value|param5=yet another value}}</nowiki>
would make ParameterCount produce 3
.
By default, the module doesn't count parameters whose values contain only whitespace, e.g. . If you wish to count all parameters, regardless of their value, use . This works with both the all
and main
functions.
Examples
If the code <nowiki>{{#invoke:ParameterCount|all|checkblanks=no}}</nowiki>
was added to the <nowiki>{{my example template}}</nowiki>
template, the code <nowiki>{{my example template|1|2|foo=bar|baz=}}</nowiki>
would make ParameterCount produce 4
.