File: Counter.md

package info (click to toggle)
hugo 0.131.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 45,580 kB
  • sloc: javascript: 31,172; xml: 248; makefile: 73; sh: 42
file content (35 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
---
title: math.Counter
description: Increments and returns a global counter.
categories: []
keywords: []
action:
  aliases: []
  related: []
  returnType: uint64
  signatures: [math.Counter]
---

The counter is global for both monolingual and multilingual sites, and its initial value for each build is 1.

```go-html-template
{{ warnf "single.html called %d times" math.Counter }}
```

```sh
WARN  single.html called 1 times
WARN  single.html called 2 times
WARN  single.html called 3 times
```

Use this function to:

- Create unique warnings as shown above; the [`warnf`] function suppresses duplicate messages
- Create unique target paths for the `resources.FromString` function where the target path is also the cache key

[`warnf`]: /functions/fmt/warnf/
[`resources.FromString`]: /functions/resources/fromstring/

{{% note %}}
Due to concurrency, the value returned in a given template for a given page will vary from one build to the next. You cannot use this function to assign a static id to each page.
{{% /note %}}