User:4madness/Templates/ParamSum
The ParamSum template accepts a generated list of values, adds them together and returns a table with the requested sums.
Usage
{{User:4madness/Templates/ParamSum |data = <!-- raw data (see below) --> |selected = <!-- comma-seperated list of keys to sum together from data --> |html = <!-- (Optional) "yes" if html output is desired instead of wikitext table syntax --> |<somekey> class = <!-- (Optional) class(es) to add to cell containing the sum of <somekey> --> }}
The parameters |data=
and |selected=
can also be passed as unnamed parameters.
{{User:4madness/Templates/ParamSum|<!-- data -->|<!-- selected -->|…}}
Parameter details
data
- Alias: First unnamed parameter
The value for |data=
is expected to be a list of rows delimited by !
symbols with each key=value
pair within a row separated by |
symbols. For example:
key1=row1value|key2=row1value!key1=row2value|key2=row2value!… key1=4|key2=5!key1=1|key2=10!key1=8|key2=6!key1=10|key2=4!key1=3|key2=10!…
selected
- Alias: Second unnamed parameter
A comma-separated list of keys to sum together. Keys not in this list are skipped and are returned in the same order as given here.
A special key, #
, is used to return the number of lines processed.
html (optional)
If set to yes
, the returned code will use HTML table cells (<td class="someclass">sum1</td><td class="someotherclass">sum2</td>
) instead of wikitext table syntax (class="someclass" | sum1|| class="someotherclass" | sum2
).
<somekey> class (optional)
This parameter is used to assign a class or classes to the cell containing the sum of <somekey>
values. For the cell containing the sum of all key1
values, use |key1 class=<classdef>
. This can be repeated for each key in |selected=
.
The value of <classdef>
can be an ordinary list of classes or it can be any number of conditional classes.
<classdef> |
Condition | Class is added… | Result when sum of key1 values is 10
|
---|---|---|---|
someclass |
none | always | |key1 class=someclass → class="someclass" | 10
|
gt<num>:someclass |
gt<num> |
when sum is greater than <num> |
|key1 class=gt0:someclass → class="someclass" | 10
|
lt<num>:someclass |
lt<num> |
when sum is less than <num> |
|key1 class=lt0:someclass → 10
|
eq<num>:someclass |
eq<num> |
when sum is equal to <num> |
|key1 class=eq10:someclass → class="someclass" | 10
|
neq<num>:someclass |
neq<num> |
when sum is not equal to <num> |
|key1 class=neq10:someclass → 10
|
lteq<num>:someclass |
lteq<num> |
when sum is less than or equal to <num> |
|key1 class=lteq0:someclass → 10
|
gteq<num>:someclass |
gteq<num> |
when sum is greater than or equal to <num> |
|key1 class=gteq0:someclass → class="someclass" | 10
|
For <classdef>
values, <num>
can be substituted with #
to compare against the number of lines processed.
|key1 class=<classdef> |
Result when sum of key1 values is 10
|
---|---|
|key1 class=someclass gt0:someotherclass |
class="someclass someotherclass" | 10
|
|key1 class=lt0:failclass gteq0:successclass |
class="successclass" | 10
|
|key1 class=gteq0:positiveclass gteq#:enoughclass
|
Count is 9 → class="positiveclass enoughclass" | 10 Count is 10 → |
|key1 class=lt#:toofewclass eq#:correctclass gt#:toomanyclass
|
Count is 9 → class="toomanyclass" | 10 Count is 10 → |
Example
This template is best used with DynamicPageList (DPL) queries to reformat the data for ingestion by this template. To count the number of template calls (e.g. DropsLine) on a given page that use a particular parameter (e.g. "nosmw"), a phantom template can be created (e.g. at "/hasnosmw") with the following content:
hasnosmw={{#if:{{{nosmw|}}}|1|0}}!
The phantom template can then be used in a DPL query to generate the data value for use by this template. To display the result correctly, the template must be wrapped in a table.
{| class="wikitable" style="text-align:center" |+ DropsLine templates on [[{{{title|}}}]] |- ! Calls !! Calls w/ <code>nosmw</code> |- |{{User:4madness/Templates/ParamSum |data = {{#dpl:title={{{title|}}}|uses=Template:DropsLine|include={DropsLine¦/hasnosmw}|noresultsheader=0|namespace=}} |selected = #,hasnosmw }} |}
|
|
|
This can be improved by adding cell highlighting as shown below:
{| class="wikitable" style="text-align:center" |+ DropsLine templates on [[{{{title|}}}]] |- ! Calls !! Calls w/ <code>nosmw</code> |- |{{User:4madness/Templates/ParamSum |data = {{#dpl:title={{{title|}}}|uses=Template:DropsLine|include={DropsLine¦/hasnosmw}|noresultsheader=0|namespace=}} |selected = #,hasnosmw |hasnosmw class=eq#:table-bg-green gt0:table-bg-orange lteq0:table-bg-red }} |}
Note regarding colors |
---|
The table-bg-* color classes overwrite each other depending on the order they appear in MediaWiki:Common.less/wikitables.less.
|
They do so in the following order:
|
|
|
|