diff --git a/content/functions/scratch.md b/content/functions/scratch.md index 206b73975..1b7c574e6 100644 --- a/content/functions/scratch.md +++ b/content/functions/scratch.md @@ -10,11 +10,57 @@ tags: [iteration] categories: [functions] toc: false draft: false -aliases: [/extras/scratch/] +aliases: [/extras/scratch/,/doc/scratch/] notesforauthors: --- -## Example +`Scratch` -- a "scratchpad" for your page-scoped variables. 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. + +`Scratch` is added to both `Page` and `Shortcode` -- with following methods: + +* `Set` and `Add` takes a `key` and the `value` to add. +* `Get` returns the `value` for the `key` given. +* `SetInMap` takes a `key`, `mapKey` and `value` +* `GetSortedMapValues` returns array of values from `key` sorted by `mapKey` + +`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" }}`. -## Advanced Example diff --git a/content/tutorials/_index.md b/content/tutorials/_index.md index f3c80c719..b52fc0ba9 100644 --- a/content/tutorials/_index.md +++ b/content/tutorials/_index.md @@ -1,5 +1,5 @@ --- -title: Overview +title: Tutorials linktitle: Overview description: date: 2017-02-01 diff --git a/content/variables-and-params/_index.md b/content/variables-and-params/_index.md index 4cba0ce23..cfe487556 100644 --- a/content/variables-and-params/_index.md +++ b/content/variables-and-params/_index.md @@ -1,5 +1,5 @@ --- -title: Overview +title: Variables and Params linktitle: Overview description: Page-, file-, taxonomy-, and site-level variables and parameters available in templates. date: 2017-02-01