f887bd7bAdd script to pull theme changese89bbb2fUpdate README78155decMerge commit '9301947b25099dd402065104b340b1a480906a74' as 'themes/gohugoioTheme'9301947bSquashed 'themes/gohugoioTheme/' content from commit 7dd8a302e7557a34Remove gohugoioTheme submodulea78bbe22Add link to Go issue31d1ef40Add example for taxonomy terms with metadatada978cc7Fix links in Blackfriday extension documentation950ad115Fix reference to Blackfriday Extensions section12d1d026Add documentation for Blackfriday Extensions8c2b8fb5Fix explaination for uglyURLs config option378aded7Use community repository for Arch Linux installation guide56455e7eImprove 0.26 release notesbe7db03anetlify: Build with the new 0.26d430b2d3Bump version to 0.2661adaca0releaser: Prepare repository for 0.27-DEVabef29a6releaser: Add release notes to /docs for release of 0.265937fe41releaser: Bump versions for release of 0.26033752f1Merge commit 'e81208265bb3cdb7606d051a23d83aeebcb7d34d'bec2bd12Make the title case style guide configurablef1739a44Merge commit '50ec65fbe1a48475d3320775dab2c47389c02114'a1aac0e5helpers: Add support for French Guillemetsb8dc1592doc: Merge commit '2c0d1ccdcd95de0bddeb39dca2e4d08f0d8056d7'7b3e31b8Merge commit '6dbde8d731f221b027c0c60b772ba82dad759943'a37e7201Bump versions to 0.26-DEV git-subtree-dir: docs git-subtree-split:f887bd7b4e
2.7 KiB
title, description, godocref, date, publishdate, lastmod, categories, menu, toc, signature, workson, hugoversion, relatedfuncs, deprecated, draft, aliases
| title | description | godocref | date | publishdate | lastmod | categories | menu | toc | signature | workson | hugoversion | relatedfuncs | deprecated | draft | aliases | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| .Scratch | Acts as a "scratchpad" to allow for writable page- or shortcode-scoped variables. | 2017-02-01 | 2017-02-01 | 2017-02-01 |
|
|
false | false |
|
In most cases you can do well without Scratch, but there are some use cases that aren't solvable with Go's templates without Scratch's help, due to scoping issues.
{{% note %}} See this Go issue for the main motivation behind Scratch. {{% /note %}}
Scratch is added to both Page and Shortcode -- with following methods:
SetandAddtakes akeyand thevalueto add.Getreturns thevaluefor thekeygiven.SetInMaptakes akey,mapKeyandvalueGetSortedMapValuesreturns array of values fromkeysorted bymapKey
Set and SetInMap can store values of any type.
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.
The scope of the backing data is global for the given Page or Shortcode, and spans partial and shortcode includes.
Note that .Scratch from a shortcode will return the shortcode's Scratch, which in most cases is what you want. If you want to store it in the page scroped Scratch, then use .Page.Scratch.
Sample usage
The usage is best illustrated with some samples:
{{ $.Scratch.Add "a1" 12 }}
{{ $.Scratch.Get "a1" }} {{/* => 12 */}}
{{ $.Scratch.Add "a1" 1 }}
{{ $.Scratch.Get "a1" }} // {{/* => 13 */}}
{{ $.Scratch.Add "a2" "AB" }}
{{ $.Scratch.Get "a2" }} {{/* => AB */}}
{{ $.Scratch.Add "a2" "CD" }}
{{ $.Scratch.Get "a2" }} {{/* => ABCD */}}
{{ $.Scratch.Add "l1" (slice "A" "B") }}
{{ $.Scratch.Get "l1" }} {{/* => [A B] */}}
{{ $.Scratch.Add "l1" (slice "C" "D") }}
{{ $.Scratch.Get "l1" }} {{/* => [A B C D] */}}
{{ $.Scratch.Set "v1" 123 }}
{{ $.Scratch.Get "v1" }} {{/* => 123 */}}
{{ $.Scratch.SetInMap "a3" "b" "XX" }}
{{ $.Scratch.SetInMap "a3" "a" "AA" }}
{{ $.Scratch.SetInMap "a3" "c" "CC" }}
{{ $.Scratch.SetInMap "a3" "b" "BB" }}
{{ $.Scratch.GetSortedMapValues "a3" }} {{/* => []interface {}{"AA", "BB", "CC"} */}}
{{% note %}}
The examples above uses the special $ variable, which refers to the top-level node. This is the behavior you most likely want, and will help remove some confusion when using Scratch inside page range loops -- and you start inadvertently calling the wrong Scratch. But there may be use cases for {{ .Scratch.Add "key" "some value" }}.
{{% /note %}}