1
0
Fork 0
hugo/docs/content/en/methods/page/Scratch.md

1.7 KiB

title description categories keywords action toc aliases
Scratch Returns a "scratch pad" on the given page to store and manipulate data.
related returnType signatures
methods/page/Store
functions/collections/NewScratch
maps.Scratch
PAGE.Scratch
true
/extras/scratch/
/doc/scratch/
/functions/scratch

The Scratch method on a Page object creates a scratch pad to store and manipulate data. To create a scratch pad that is not reset on server rebuilds, use the Store method instead.

To create a locally scoped scratch pad that is not attached to a Page object, use the newScratch function.

{{% include "methods/page/_common/scratch-methods.md" %}}

Determinate values

The Scratch method is often used to set scratch pad values within a shortcode, a partial template called by a shortcode, or by a Markdown render hook. In all three cases, the scratch pad values are not determinate until Hugo renders the page content.

If you need to access a scratch pad value from a parent template, and the parent template has not yet rendered the page content, you can trigger content rendering by assigning the returned value to a noop variable:

{{ $noop := .Content }}
{{ .Store.Get "mykey" }}

You can also trigger content rendering with the FuzzyWordCount, Len, Plain, PlainWords, ReadingTime, Summary, Truncated, and WordCount methods. For example:

{{ $noop := .WordCount }}
{{ .Store.Get "mykey" }}