HUGO
Menu
GitHub 90479 stars Mastodon

Store

Returns a persistent data structure for storing and manipulating keyed values, scoped to the current page.
Syntax
PAGE.Store
Returns
maps.Scratch

Usage

The Store method on a Page object returns a persistent data structure for storing and manipulating keyed values, scoped to the current page. Use a different function or method to create a data structure with a different scope.

Methods

Use these methods on the data structure:

Set
Sets the value of the given key.
Get
(any) Returns the value of the given key.
Add
Adds the given value to the existing value of the given key. If the existing value is a slice, Add appends the given value to the slice. Otherwise, Add combines the values with Go’s + operator, such as adding two numbers or joining two strings. If the key does not exist, Add sets the value of the key.
SetInMap
Takes a key, a map key, and a value, and sets the value of the map key within the map stored under the given key.
DeleteInMap
Takes a key and a map key, and removes the map key from the map stored under the given key.
GetSortedMapValues
([]any) Returns the values of the map stored under the given key, sorted by map key.
Delete
Removes the given key.

Scope

The method or function used to create the data structure determines its scope. For example, use the Store method on a Page object to create a data structure scoped to the page.

ScopeMethod or function
pagePAGE.Store
siteSITE.Store
globalhugo.Store
localcollections.NewScratch
shortcodeSHORTCODE.Store

Determinate values

You often set stored values within a shortcode template, a partial template called by a shortcode template, or a render hook template. In all three cases, the stored values are indeterminate until Hugo renders the page content.

To access a stored value from a parent template before it renders the page content, first trigger content rendering by assigning the value returned by the Content method to a noop variable:

{{ $noop := .Content }}

You can also trigger content rendering with the ContentWithoutSummary, FuzzyWordCount, Len, Plain, PlainWords, ReadingTime, Summary, Truncated, and WordCount methods. The Fragments, RenderShortcodes, and TableOfContents methods do not trigger full content rendering, and stored values set by some templates may not be available.

Examples

{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Get "greeting" }} → Hello

Adding a string to a string:

{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Add "greeting" "Welcome" }}
{{ .Store.Get "greeting" }} → HelloWelcome

Adding a number to a number:

{{ .Store.Set "total" 3 }}
{{ .Store.Add "total" 7 }}
{{ .Store.Get "total" }} → 10

Appending to a slice:

{{ .Store.Set "greetings" (slice "Hello") }}
{{ .Store.Add "greetings" (slice "Welcome" "Cheers") }}
{{ .Store.Get "greetings" }} → [Hello Welcome Cheers]

Setting values in a map:

{{ .Store.SetInMap "greetings" "english" "Hello" }}
{{ .Store.SetInMap "greetings" "french" "Bonjour" }}
{{ .Store.Get "greetings" }} → map[english:Hello french:Bonjour]

Deleting a value from a map:

{{ .Store.SetInMap "greetings" "english" "Hello" }}
{{ .Store.SetInMap "greetings" "french" "Bonjour" }}
{{ .Store.DeleteInMap "greetings" "english" }}
{{ .Store.Get "greetings" }} → map[french:Bonjour]

Getting the map values sorted by map key:

{{ .Store.SetInMap "greetings" "english" "Hello" }}
{{ .Store.SetInMap "greetings" "french" "Bonjour" }}
{{ .Store.GetSortedMapValues "greetings" }} → [Hello Bonjour]

Deleting a key:

{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Delete "greeting" }}