HRWiki:Counter
From Homestar Runner Wiki
(→Usage: add template information) |
(→Examples: better to start with an internal link first) |
||
Line 13: | Line 13: | ||
==Examples== | ==Examples== | ||
- | '''count''' is a [[ | + | '''count''' is a [[Help:ParserFunctions|parser function]] that returns an integer that increments each time the function is called: |
{| border=1 style="border-collapse: collapse" | {| border=1 style="border-collapse: collapse" | ||
|- style="background: #EEE" | |- style="background: #EEE" | ||
Line 156: | Line 156: | ||
|} | |} | ||
- | You can use the counter in other [[ | + | You can use the counter in other [[Help:ParserFunctions|parser functions]]: |
{| border=1 style="border-collapse: collapse" | {| border=1 style="border-collapse: collapse" | ||
|- style="background: #EEE" | |- style="background: #EEE" |
Current revision as of 12:27, 19 April 2025
Contents |
[edit] Usage
{{#count: id| increment}}
| Increments the counter id by increment and displays the result; increment can be omitted, and defaults to 1 .
|
{{#resetcount: id| value| increment}}
| Silently sets the counter id to value minus increment (so that the next count by increment will display value); value and increment can be omitted, and both default to 1 .
|
{{hcount}} {{vcount}}
| Used in sortable tables. See below. |
[edit] Examples
count is a parser function that returns an integer that increments each time the function is called:
Code | Gives |
---|---|
{{#count:basic}} | 1 |
{{#count:basic}} | 2 |
{{#count:basic}} | 3 |
{{#count:basic}} | 4 |
{{#count:basic}} | 5 |
The counters can be given different names, so you can have more than one of them on the same page:
(The actual name doesn't matter, so long as they're different. If you use a counter in a template, pick a name that'll be unique.)
Code | Gives |
---|---|
{{#count:first}} | 1 |
{{#count:second}} | 1 |
{{#count:second}} | 2 |
{{#count:second}} | 3 |
{{#count:first}} | 2 |
{{#count:second}} | 4 |
You can also make the counter count by twos, or by any other number:
Code | Gives |
---|---|
{{#count:evens|2}} | 2 |
{{#count:evens|2}} | 4 |
{{#count:evens|2}} | 6 |
{{#count:evens|2}} | 8 |
{{#count:evens|2}} | 10 |
Different increments can be used at different times, depending on what is needed:
Code | Gives |
---|---|
{{#count:strange|1}} | 1 |
{{#count:strange|1}} | 2 |
{{#count:strange|3}} | 5 |
{{#count:strange|0}} | 5 |
{{#count:strange|-1}} | 4 |
{{#count:strange|2}} | 6 |
The counter can be reset partway through a page:
Code | Gives |
---|---|
{{#count:basic}} | 1 |
{{#count:basic}} | 2 |
{{#count:basic}} | 3 |
{{#resetcount:basic}} | |
{{#count:basic}} | 1 |
{{#count:basic}} | 2 |
{{#count:basic}} | 3 |
You can also set what number you want to start on:
Code | Gives |
---|---|
{{#resetcount:starting|11}} | |
{{#count:starting}} | 11 |
{{#count:starting}} | 12 |
{{#count:starting}} | 13 |
{{#count:starting}} | 14 |
{{#count:starting}} | 15 |
If you're using an increment, you need to give it to #resetcount
too:
Code | Gives |
---|---|
{{#resetcount:starting evens|14|2}} | |
{{#count:starting evens|2}} | 14 |
{{#count:starting evens|2}} | 16 |
{{#count:starting evens|2}} | 18 |
{{#count:starting evens|2}} | 20 |
{{#count:starting evens|2}} | 22 |
Use a negative increment to count down. As noted above, be sure to pass the increment to #resetcount
too:
Code | Gives |
---|---|
{{#resetcount:countdown|3|-1}} | |
{{#count:countdown|-1}} | 3 |
{{#count:countdown|-1}} | 2 |
{{#count:countdown|-1}} | 1 |
{{#count:countdown|-1}} | 0 |
{{#count:countdown|-1}} | -1 |
You can use the counter in other parser functions:
Code | Gives |
---|---|
{{#ifexpr:{{#count:alternate}} mod 2 = 0|even|odd}} | odd |
{{#ifexpr:{{#count:alternate}} mod 2 = 0|even|odd}} | even |
{{#ifexpr:{{#count:alternate}} mod 2 = 0|even|odd}} | odd |
{{#ifexpr:{{#count:alternate}} mod 2 = 0|even|odd}} | even |
{{#ifexpr:{{#count:alternate}} mod 2 = 0|even|odd}} | odd |
Code | Gives |
---|---|
{{#expr:{{#count:squares}} * {{#count:squares|0}}}} | 1 |
{{#expr:{{#count:squares}} * {{#count:squares|0}}}} | 4 |
{{#expr:{{#count:squares}} * {{#count:squares|0}}}} | 9 |
{{#expr:{{#count:squares}} * {{#count:squares|0}}}} | 16 |
{{#expr:{{#count:squares}} * {{#count:squares|0}}}} | 25 |
[edit] Sortable Tables
When using count in sortable tables, please use the {{hcount}} and {{vcount}} templates. Be sure to carefully read their documentation in order to sort table entries correctly.