github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/docs/content/en/functions/scratch.md (about)

     1  ---
     2  title: .Scratch
     3  description: Acts as a "scratchpad" to allow for writable page- or shortcode-scoped variables.
     4  godocref:
     5  date: 2017-02-01
     6  publishdate: 2017-02-01
     7  lastmod: 2017-02-01
     8  keywords: [iteration]
     9  categories: [functions]
    10  menu:
    11    docs:
    12      parent: "functions"
    13  toc:
    14  signature: []
    15  workson: []
    16  hugoversion:
    17  relatedfuncs: []
    18  deprecated: false
    19  draft: false
    20  aliases: [/extras/scratch/,/doc/scratch/]
    21  ---
    22  
    23  In most cases you can do okay without `Scratch`, but due to scoping issues, there are many use cases that aren't solvable in Go Templates without `Scratch`'s help.
    24  
    25  {{% note %}}
    26  See [this Go issue](https://github.com/golang/go/issues/10608) for the main motivation behind Scratch.
    27  {{% /note %}}
    28  
    29  {{% note %}}
    30  For a detailed analysis of `.Scratch` and in context use cases, see this [post](https://regisphilibert.com/blog/2017/04/hugo-scratch-explained-variable/).
    31  {{% /note %}}
    32  
    33  ## Methods
    34  
    35  `Scratch` is added to both `Page` and `Shortcode` -- with following methods:
    36  
    37  #### .Set
    38  Set the given value to a given key
    39  
    40  ```go-html-template
    41  {{ .Scratch.Set "greeting" "Hello" }}
    42  ```
    43  #### .Get
    44  Get the value of a given key
    45  
    46  ```go-html-template
    47  {{ .Scratch.Set "greeting" "Hello" }}
    48  ----
    49  {{ .Scratch.Get "greeting" }} > Hello
    50  ```
    51  
    52  #### .Add
    53  Will add a given value to existing value of the given key. 
    54  
    55  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.
    56  
    57  ```go-html-template
    58  {{ .Scratch.Add "greetings" "Hello" }}
    59  {{ .Scratch.Add "greetings" "Welcome" }}
    60  ----
    61  {{ .Scratch.Get "greetings" }} > HelloWelcome
    62  ```
    63  
    64  ```go-html-template
    65  {{ .Scratch.Add "total" 3 }}
    66  {{ .Scratch.Add "total" 7 }}
    67  ----
    68  {{ .Scratch.Get "total" }} > 10
    69  ```
    70  
    71  
    72  ```go-html-template
    73  {{ .Scratch.Add "greetings" (slice "Hello") }}
    74  {{ .Scratch.Add "greetings" (slice "Welcome" "Cheers") }}
    75  ----
    76  {{ .Scratch.Get "greetings" }} > []interface {}{"Hello", "Welcome", "Cheers"}
    77  ```
    78  
    79  #### .SetInMap
    80  Takes a `key`, `mapKey` and `value` and add a map of `mapKey` and `value` to the given `key`.
    81  
    82  ```go-html-template
    83  {{ .Scratch.SetInMap "greetings" "english" "Hello" }}
    84  {{ .Scratch.SetInMap "greetings" "french" "Bonjour" }}
    85  ----
    86  {{ .Scratch.Get "greetings" }} > map[french:Bonjour english:Hello]
    87  ```
    88  
    89  #### .GetSortedMapValues
    90  Returns array of values from `key` sorted by `mapKey`
    91  
    92  ```go-html-template
    93  {{ .Scratch.SetInMap "greetings" "english" "Hello" }}
    94  {{ .Scratch.SetInMap "greetings" "french" "Bonjour" }}
    95  ----
    96  {{ .Scratch.GetSortedMapValues "greetings" }} > [Hello Bonjour]
    97  ```
    98  #### .Delete
    99  Removes the given key
   100  
   101  ```go-html-template
   102  {{ .Scratch.Delete "greetings" }}
   103  ```
   104  
   105  ## Scope
   106  The scope of the backing data is global for the given `Page` or `Shortcode`, and spans partial and shortcode includes.
   107  
   108  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 scoped Scratch, then use `.Page.Scratch`.
   109  
   110  
   111  
   112  
   113  [pagevars]: /variables/page/