hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

scratch.md (4470B)

    1 ---
    2 title: .Scratch
    3 description: Acts as a "scratchpad" to store and manipulate data.
    4 date: 2017-02-01
    5 publishdate: 2017-02-01
    6 lastmod: 2017-02-01
    7 keywords: [iteration]
    8 categories: [functions]
    9 menu:
   10   docs:
   11     parent: "functions"
   12 toc:
   13 signature: []
   14 workson: []
   15 hugoversion:
   16 relatedfuncs: []
   17 deprecated: false
   18 draft: false
   19 aliases: [/extras/scratch/,/doc/scratch/]
   20 ---
   21 
   22 Scratch is a Hugo feature designed to conveniently manipulate data in a Go Template world. It is either a Page or Shortcode method for which the resulting data will be attached to the given context, or it can live as a unique instance stored in a variable.
   23 
   24 {{% note %}}
   25 Note that Scratch was initially created as a workaround for a [Go template scoping limitation](https://github.com/golang/go/issues/10608) that affected Hugo versions prior to 0.48. For a detailed analysis of `.Scratch` and contextual use cases, see [this blog post](https://regisphilibert.com/blog/2017/04/hugo-scratch-explained-variable/).
   26 {{% /note %}}
   27 
   28 ### Contexted `.Scratch` vs. local `newScratch`
   29 
   30 Since Hugo 0.43, there are two different ways of using Scratch:
   31 
   32 #### The Page's `.Scratch`
   33 
   34 `.Scratch` is available as a Page method or a Shortcode method and attaches the "scratched" data to the given page. Either a Page or a Shortcode context is required to use `.Scratch`.
   35 
   36 ```go-html-template
   37 {{ .Scratch.Set "greeting" "bonjour" }}
   38 {{ range .Pages }}
   39   {{ .Scratch.Set "greeting" (print "bonjour" .Title) }}
   40 {{ end }}
   41 ```
   42 
   43 #### The local `newScratch`
   44 
   45 {{< new-in "0.43" >}} A Scratch instance can also be assigned to any variable using the `newScratch` function. In this case, no Page or Shortcode context is required and the scope of the scratch is only local. The methods detailed below are available from the variable the Scratch instance was assigned to.
   46 
   47 ```go-html-template
   48 {{ $data := newScratch }}
   49 {{ $data.Set "greeting" "hola" }}
   50 ```
   51 
   52 ### Methods
   53 
   54 A Scratch has the following methods:
   55 
   56 {{% note %}}
   57 Note that the following examples assume a [local Scratch instance](#the-local-newscratch) has been stored in `$scratch`.
   58 {{% /note %}}
   59 
   60 #### .Set
   61 
   62 Set the value of a given key.
   63 
   64 ```go-html-template
   65 {{ $scratch.Set "greeting" "Hello" }}
   66 ```
   67 
   68 #### .Get
   69 
   70 Get the value of a given key.
   71 
   72 ```go-html-template
   73 {{ $scratch.Set "greeting" "Hello" }}
   74 ----
   75 {{ $scratch.Get "greeting" }} > Hello
   76 ```
   77 
   78 #### .Add
   79 
   80 Add a given value to existing value(s) of the given key. 
   81 
   82 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.
   83 
   84 ```go-html-template
   85 {{ $scratch.Add "greetings" "Hello" }}
   86 {{ $scratch.Add "greetings" "Welcome" }}
   87 ----
   88 {{ $scratch.Get "greetings" }} > HelloWelcome
   89 ```
   90 
   91 ```go-html-template
   92 {{ $scratch.Add "total" 3 }}
   93 {{ $scratch.Add "total" 7 }}
   94 ----
   95 {{ $scratch.Get "total" }} > 10
   96 ```
   97 
   98 ```go-html-template
   99 {{ $scratch.Add "greetings" (slice "Hello") }}
  100 {{ $scratch.Add "greetings" (slice "Welcome" "Cheers") }}
  101 ----
  102 {{ $scratch.Get "greetings" }} > []interface {}{"Hello", "Welcome", "Cheers"}
  103 ```
  104 
  105 #### .SetInMap
  106 
  107 Takes a `key`, `mapKey` and `value` and adds a map of `mapKey` and `value` to the given `key`.
  108 
  109 ```go-html-template
  110 {{ $scratch.SetInMap "greetings" "english" "Hello" }}
  111 {{ $scratch.SetInMap "greetings" "french" "Bonjour" }}
  112 ----
  113 {{ $scratch.Get "greetings" }} > map[french:Bonjour english:Hello]
  114 ```
  115 
  116 #### .DeleteInMap
  117 Takes a `key` and `mapKey` and removes the map of `mapKey` from the given `key`.
  118 
  119 ```go-html-template
  120 {{ .Scratch.SetInMap "greetings" "english" "Hello" }}
  121 {{ .Scratch.SetInMap "greetings" "french" "Bonjour" }}
  122 ----
  123 {{ .Scratch.DeleteInMap "greetings" "english" }}
  124 ----
  125 {{ .Scratch.Get "greetings" }} > map[french:Bonjour]
  126 ```
  127 
  128 #### .GetSortedMapValues
  129 
  130 Return an array of values from `key` sorted by `mapKey`.
  131 
  132 ```go-html-template
  133 {{ $scratch.SetInMap "greetings" "english" "Hello" }}
  134 {{ $scratch.SetInMap "greetings" "french" "Bonjour" }}
  135 ----
  136 {{ $scratch.GetSortedMapValues "greetings" }} > [Hello Bonjour]
  137 ```
  138 
  139 #### .Delete
  140 
  141 {{< new-in "0.38" >}} Remove the given key.
  142 
  143 ```go-html-template
  144 {{ $scratch.Set "greeting" "Hello" }}
  145 ----
  146 {{ $scratch.Delete "greeting" }}
  147 ```
  148 
  149 #### .Values
  150 
  151 Return the raw backing map. Note that you should only use this method on the locally scoped Scratch instances you obtain via [`newScratch`](#the-local-newscratch), not `.Page.Scratch` etc., as that will lead to concurrency issues.
  152 
  153 
  154 [pagevars]: /variables/page/