Provide examples of content rendering before accessing Scratch or Store

Closes #2402
This commit is contained in:
Joe Mooring 2024-02-08 22:42:31 -08:00 committed by Bjørn Erik Pedersen
parent 0206b9699b
commit a3184764d7
5 changed files with 56 additions and 2 deletions

View File

@ -58,7 +58,7 @@ Hugo does not provide a built-in template for Mermaid diagrams. Create your own
And then include this snippet at the bottom of the content template:
```go-html-template
{{ if .Page.Store.Get "hasMermaid" }}
{{ if .Store.Get "hasMermaid" }}
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });

View File

@ -9,6 +9,7 @@ action:
- functions/collections/NewScratch
returnType: maps.Scratch
signatures: [PAGE.Scratch]
toc: true
aliases: [/extras/scratch/,/doc/scratch/,/functions/scratch]
---
@ -21,3 +22,21 @@ To create a locally scoped scratch pad that is not attached to a `Page` object,
[scratch pad]: /getting-started/glossary/#scratch-pad
{{% 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:
```go-html-template
{{ $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:
```go-html-template
{{ $noop := .WordCount }}
{{ .Store.Get "mykey" }}
```

View File

@ -9,6 +9,7 @@ action:
- functions/collections/NewScratch
returnType: maps.Scratch
signatures: [PAGE.Store]
toc: true
aliases: [/functions/store]
---
@ -102,3 +103,21 @@ Removes the given key.
{{ .Store.Set "greeting" "Hello" }}
{{ .Store.Delete "greeting" }}
```
## Determinate values
The `Store` 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:
```go-html-template
{{ $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:
```go-html-template
{{ $noop := .WordCount }}
{{ .Store.Get "mykey" }}
```

View File

@ -122,7 +122,7 @@ For example, to create a code block render hook to render [Mermaid] diagrams:
Then include this snippet at the bottom of the your base template:
{{< code file=layouts/_default/baseof.html copy=true >}}
{{ if .Page.Store.Get "hasMermaid" }}
{{ if .Store.Get "hasMermaid" }}
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });

View File

@ -104,6 +104,22 @@ In its default configuration, Hugo's file watcher may not be able detect file ch
In these cases, instead of monitoring native file system events, use the `--poll` command line flag. For example, to poll the project files every 700 milliseconds ms, use `--poll 700ms`.
###### Why is my page Scratch or Store missing a value?
The [`Scratch`] and [`Store`] methods on a `Page` object allow you to create a "scratch pad" on the given page to store and manipulate data. Values are often set 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:
```go-html-template
{{ $noop := .Content }}
{{ .Store.Get "mykey" }}
```
You can trigger content rendering with other methods as well. See next FAQ.
[`Scratch`]: /methods/page/scratch
[`Store`]: /methods/page/store
###### Which page methods trigger content rendering?
The following methods on a `Page` object trigger content rendering: `Content`, `FuzzyWordCount`, `Len`, `Plain`, `PlainWords`, `ReadingTime`, `Summary`, `Truncated`, and `WordCount`.