HUGO
Menu
GitHub 90265 stars Mastodon

math.Counter

Returns a global counter value, incrementing it each time the function is called.
Syntax
math.Counter
Returns
uint64

Usage

The math.Counter function returns a global counter value, incrementing it each time the function is called. The counter is global for both monolingual and multilingual projects, and its initial value for each build is 1.

Use this function to create unique values, such as:

  • Messages passed to the fmt.Warnf function, which otherwise suppresses duplicate messages.
  • Target paths passed to the resources.FromString function, which uses the target path as the cache key.

Because Hugo renders pages concurrently, the value returned in a given template for a given page varies from one build to the next. You cannot use this function to assign a static identifier to each page.

Examples

layouts/page.html
{{ warnf "page.html called %d times" math.Counter }}

The resulting console output:

WARN  page.html called 1 times
WARN  page.html called 2 times
WARN  page.html called 3 times