Scratch doc: Use the special $ variable to prevent confusion

This commit is contained in:
bep 2015-02-01 20:36:43 +01:00
parent 13d9009406
commit dd9344816a

View file

@ -25,18 +25,20 @@ The scope of the backing data is global for the given `Node` or `Page`, and span
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 "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 "a2" "AB" }}
{{ $.Scratch.Get "a2" }} {{/* => AB */}}
{{ $.Scratch.Add "a2" "CD" }}
{{ $.Scratch.Get "a2" }} {{/* => ABCD */}}
{{ .Scratch.Set "v1" 123 }}
{{ .Scratch.Get "v1" }} {{/* => 123 */}}
{{ $.Scratch.Set "v1" 123 }}
{{ $.Scratch.Get "v1" }} {{/* => 123 */}}
```
**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" }}`.