Skip to content

Commit

Permalink
Provide examples of content rendering before accessing Scratch or Store
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring committed Feb 15, 2024
1 parent a24367d commit e43593d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion content/en/content-management/diagrams.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
19 changes: 19 additions & 0 deletions content/en/methods/page/Scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ action:
- functions/collections/NewScratch
returnType: maps.Scratch
signatures: [PAGE.Scratch]
toc: true
aliases: [/extras/scratch/,/doc/scratch/,/functions/scratch]
---

Expand All @@ -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" }}
```
19 changes: 19 additions & 0 deletions content/en/methods/page/Store.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ action:
- functions/collections/NewScratch
returnType: maps.Scratch
signatures: [PAGE.Store]
toc: true
aliases: [/functions/store]
---

Expand Down Expand Up @@ -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" }}
```
2 changes: 1 addition & 1 deletion content/en/render-hooks/code-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
16 changes: 16 additions & 0 deletions content/en/troubleshooting/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit e43593d

Please sign in to comment.