| 1 |
--- |
| 2 |
title: Store |
| 3 |
description: Returns a "scratch pad" to store and manipulate data, 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 [scratch pad](g) to store and manipulate data, scoped to the current site. To create a scratch pad with a different [scope](g), refer to the [scope](#scope) section below. |
| 15 |
|
| 16 |
## Methods |
| 17 |
|
| 18 |
### Set |
| 19 |
|
| 20 |
Sets the value of a given key. |
| 21 |
|
| 22 |
```go-html-template |
| 23 |
{{ site.Store.Set "greeting" "Hello" }} |
| 24 |
``` |
| 25 |
|
| 26 |
### Get |
| 27 |
|
| 28 |
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 |
|
| 37 |
Adds a given value to existing value(s) of the given key. |
| 38 |
|
| 39 |
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. |
| 40 |
|
| 41 |
```go-html-template |
| 42 |
{{ site.Store.Set "greeting" "Hello" }} |
| 43 |
{{ site.Store.Add "greeting" "Welcome" }} |
| 44 |
{{ site.Store.Get "greeting" }} → HelloWelcome |
| 45 |
``` |
| 46 |
|
| 47 |
```go-html-template |
| 48 |
{{ site.Store.Set "total" 3 }} |
| 49 |
{{ site.Store.Add "total" 7 }} |
| 50 |
{{ site.Store.Get "total" }} → 10 |
| 51 |
``` |
| 52 |
|
| 53 |
```go-html-template |
| 54 |
{{ site.Store.Set "greetings" (slice "Hello") }} |
| 55 |
{{ site.Store.Add "greetings" (slice "Welcome" "Cheers") }} |
| 56 |
{{ site.Store.Get "greetings" }} → [Hello Welcome Cheers] |
| 57 |
``` |
| 58 |
|
| 59 |
### SetInMap |
| 60 |
|
| 61 |
Takes a `key`, `mapKey` and `value` and adds a map of `mapKey` and `value` to the given `key`. |
| 62 |
|
| 63 |
```go-html-template |
| 64 |
{{ site.Store.SetInMap "greetings" "english" "Hello" }} |
| 65 |
{{ site.Store.SetInMap "greetings" "french" "Bonjour" }} |
| 66 |
{{ site.Store.Get "greetings" }} → map[english:Hello french:Bonjour] |
| 67 |
``` |
| 68 |
|
| 69 |
### DeleteInMap |
| 70 |
|
| 71 |
Takes a `key` and `mapKey` and removes the map of `mapKey` from the given `key`. |
| 72 |
|
| 73 |
```go-html-template |
| 74 |
{{ site.Store.SetInMap "greetings" "english" "Hello" }} |
| 75 |
{{ site.Store.SetInMap "greetings" "french" "Bonjour" }} |
| 76 |
{{ site.Store.DeleteInMap "greetings" "english" }} |
| 77 |
{{ site.Store.Get "greetings" }} → map[french:Bonjour] |
| 78 |
``` |
| 79 |
|
| 80 |
### GetSortedMapValues |
| 81 |
|
| 82 |
Returns an array of values from `key` sorted by `mapKey`. |
| 83 |
|
| 84 |
```go-html-template |
| 85 |
{{ site.Store.SetInMap "greetings" "english" "Hello" }} |
| 86 |
{{ site.Store.SetInMap "greetings" "french" "Bonjour" }} |
| 87 |
{{ site.Store.GetSortedMapValues "greetings" }} → [Hello Bonjour] |
| 88 |
``` |
| 89 |
|
| 90 |
### Delete |
| 91 |
|
| 92 |
Removes the given key. |
| 93 |
|
| 94 |
```go-html-template |
| 95 |
{{ site.Store.Set "greeting" "Hello" }} |
| 96 |
{{ site.Store.Delete "greeting" }} |
| 97 |
``` |
| 98 |
|
| 99 |
{{% include "_common/scratch-pad-scope.md" %}} |
| 100 |
|
| 101 |
## Determinate values |
| 102 |
|
| 103 |
The `Store` method is often used to set scratch pad values within a _shortcode_ template, a _partial_ template called by a _shortcode_ template, or by a _render hook_ template. In all three cases, the scratch pad values are indeterminate until Hugo renders the page content. |
| 104 |
|
| 105 |
If you need to access a scratch pad 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: |
| 106 |
|
| 107 |
```go-html-template |
| 108 |
{{ $noop := .Content }} |
| 109 |
{{ site.Store.Get "mykey" }} |
| 110 |
``` |
| 111 |
|
| 112 |
You can also trigger content rendering with the `ContentWithoutSummary`, `FuzzyWordCount`, `Len`, `Plain`, `PlainWords`, `ReadingTime`, `Summary`, `Truncated`, and `WordCount` methods. For example: |
| 113 |
|
| 114 |
```go-html-template |
| 115 |
{{ $noop := .WordCount }} |
| 116 |
{{ site.Store.Get "mykey" }} |
| 117 |
``` |