Store
Returns a “scratch pad” to store and manipulate data, scoped to the current page.
Syntax
PAGE.Store
Returns
maps.Scratch
Use the Store method on a Page object to create a scratch pad to store and manipulate data, scoped to the current page. To create a scratch pad with a different scope, refer to the scope section below.
方法
Set
設置給定鍵的值。
{{ .Store.Set "greeting" "Hello" }}Get
獲取給定鍵的值。
{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Get "greeting" }} → HelloAdd
將給定值添加到給定鍵的現有值中。
對於單個值,Add 接受支持 Go 的 + 運算符的值。如果鍵的第一個 Add 是數組或切片,則後續添加將附加到該列表中。
{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Add "greeting" "Welcome" }}
{{ .Store.Get "greeting" }} → HelloWelcome{{ .Store.Set "total" 3 }}
{{ .Store.Add "total" 7 }}
{{ .Store.Get "total" }} → 10{{ .Store.Set "greetings" (slice "Hello") }}
{{ .Store.Add "greetings" (slice "Welcome" "Cheers") }}
{{ .Store.Get "greetings" }} → [Hello Welcome Cheers]SetInMap
接受 key、mapKey 和 value,並將 mapKey 和 value 的映射添加到給定 key。
{{ .Store.SetInMap "greetings" "english" "Hello" }}
{{ .Store.SetInMap "greetings" "french" "Bonjour" }}
{{ .Store.Get "greetings" }} → map[english:Hello french:Bonjour]DeleteInMap
接受 key 和 mapKey,並從給定 key 中移除 mapKey 的映射。
{{ .Store.SetInMap "greetings" "english" "Hello" }}
{{ .Store.SetInMap "greetings" "french" "Bonjour" }}
{{ .Store.DeleteInMap "greetings" "english" }}
{{ .Store.Get "greetings" }} → map[french:Bonjour]GetSortedMapValues
返回按 mapKey 排序的 key 值數組。
{{ .Store.SetInMap "greetings" "english" "Hello" }}
{{ .Store.SetInMap "greetings" "french" "Bonjour" }}
{{ .Store.GetSortedMapValues "greetings" }} → [Hello Bonjour]Delete
移除給定鍵。
{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Delete "greeting" }}
{{__hugo_ctx/}}
{{__hugo_ctx pid=1467}}
## 作用域
用於創建暫存板的方法或函數決定了其作用域。例如,在 `Page` 對象上使用 `Store` 方法創建作用於頁面的暫存板。
作用域 | 方法或函數
:--|:--
page|[`PAGE.Store`]
site|[`SITE.Store`]
global|[`hugo.Store`]
local|[`collections.NewScratch`]
shortcode|[`SHORTCODE.Store`]
[`page.store`]: /zh/methods/page/store
[`site.store`]: /zh/methods/site/store
[`hugo.store`]: /zh/functions/hugo/store
[`collections.newscratch`]: /zh/functions/collections/newscratch
[`shortcode.store`]: /zh/methods/shortcode/store
{{__hugo_ctx/}}
## Determinate values
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.
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:
```go-html-template
{{ $noop := .Content }}
{{ .Store.Get "mykey" }}You can also trigger content rendering with the ContentWithoutSummary, FuzzyWordCount, Len, Plain, PlainWords, ReadingTime, Summary, Truncated, and WordCount methods. For example:
{{ $noop := .WordCount }}
{{ .Store.Get "mykey" }}