Debian Codegraph @

docs/content/en/methods/site/Store.md

raw
1 ---
2 title: Store
3 description: Returns a persistent data structure for storing and manipulating keyed values, scoped to the current site.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: maps.Scratch
9 signatures: [site.Store]
10 ---
11
12 {{< new-in 0.139.0 />}}
13
14 Use the `Store` method on a `Site` object to create a persistent data structure for storing and manipulating keyed values, scoped to the current site. To create a data structure with a different [scope](g), refer to the [scope](#scope) section below.
15
16 ## Methods
17
18 Use these methods on the data structure.
19
20 `Set`
21 : Sets the value of a given key.
22
23 ```go-html-template
24 {{ site.Store.Set "greeting" "Hello" }}
25 ```
26
27 `Get`
28 : (`any`) Gets the value of a given key.
29
30 ```go-html-template
31 {{ site.Store.Set "greeting" "Hello" }}
32 {{ site.Store.Get "greeting" }} → Hello
33 ```
34
35 `Add`
36 : Adds a given value to existing value(s) of the given key.
37
38 For single values, `Add` accepts values that support Go's `+` operator. If the first `Add` for a key is an array or slice, the following adds will be appended to that list.
39
40 ```go-html-template
41 {{ site.Store.Set "greeting" "Hello" }}
42 {{ site.Store.Add "greeting" "Welcome" }}
43 {{ site.Store.Get "greeting" }} → HelloWelcome
44 ```
45
46 ```go-html-template
47 {{ site.Store.Set "total" 3 }}
48 {{ site.Store.Add "total" 7 }}
49 {{ site.Store.Get "total" }} → 10
50 ```
51
52 ```go-html-template
53 {{ site.Store.Set "greetings" (slice "Hello") }}
54 {{ site.Store.Add "greetings" (slice "Welcome" "Cheers") }}
55 {{ site.Store.Get "greetings" }} → [Hello Welcome Cheers]
56 ```
57
58 `SetInMap`
59 : Takes a `key`, `mapKey` and `value` and adds a map of `mapKey` and `value` to the given `key`.
60
61 ```go-html-template
62 {{ site.Store.SetInMap "greetings" "english" "Hello" }}
63 {{ site.Store.SetInMap "greetings" "french" "Bonjour" }}
64 {{ site.Store.Get "greetings" }} → map[english:Hello french:Bonjour]
65 ```
66
67 `DeleteInMap`
68 : Takes a `key` and `mapKey` and removes the map of `mapKey` from the given `key`.
69
70 ```go-html-template
71 {{ site.Store.SetInMap "greetings" "english" "Hello" }}
72 {{ site.Store.SetInMap "greetings" "french" "Bonjour" }}
73 {{ site.Store.DeleteInMap "greetings" "english" }}
74 {{ site.Store.Get "greetings" }} → map[french:Bonjour]
75 ```
76
77 `GetSortedMapValues`
78 : (`[]any`) Returns an array of values from `key` sorted by `mapKey`.
79
80 ```go-html-template
81 {{ site.Store.SetInMap "greetings" "english" "Hello" }}
82 {{ site.Store.SetInMap "greetings" "french" "Bonjour" }}
83 {{ site.Store.GetSortedMapValues "greetings" }} → [Hello Bonjour]
84 ```
85
86 `Delete`
87 : Removes the given key.
88
89 ```go-html-template
90 {{ site.Store.Set "greeting" "Hello" }}
91 {{ site.Store.Delete "greeting" }}
92 ```
93
94 {{% include "_common/store-scope.md" %}}
95
96 ## Determinate values
97
98 The `Store` method is often used to set values within a _shortcode_ template, a _partial_ template called by a _shortcode_ template, or by a _render hook_ template. In all three cases, the stored values are indeterminate until Hugo renders the page content.
99
100 If you need to access a stored value from a parent template, and the parent template has not yet rendered the page content, you can trigger content rendering by assigning the returned value to a [noop](g) variable:
101
102 ```go-html-template
103 {{ $noop := .Content }}
104 {{ site.Store.Get "mykey" }}
105 ```
106
107 You can also trigger content rendering with the `ContentWithoutSummary`, `FuzzyWordCount`, `Len`, `Plain`, `PlainWords`, `ReadingTime`, `Summary`, `Truncated`, and `WordCount` methods. For example:
108
109 ```go-html-template
110 {{ $noop := .WordCount }}
111 {{ site.Store.Get "mykey" }}
112 ```