Possible Bug: Scratch Values between Shortcode and Page templates

Not sure if this is a bug or the desired behavior but thought I would point it out. A frequent design pattern I use in hugo is using a shortcode to wrap sections of content. To template their output and build an in page menu.

Between the release of 0.38.2 and 0.40.3 the scratch variables work slightly differently. The variables I had been storing in the page’s scratch from within shortcodes are not available at the start of the page template. Instead I have found that they are only available after you have called “{{ .Content }}”. So if you are trying to grab a variable assigned within a shortcode prior to printing your “.Content” it will be undefined/empty.

My quick work around was to print the content into a variable at the top of the template and everything works as expected.

Below is simple example to illustrate the issue:

<!-- Illustrating the problem -->

<div id="page">
  EMPTY: {{ .Scratch.Get "variableFromShortcode" }}
  <div id="my-content">
    {{ .Content }}
  </div>
  FILLED: {{ .Scratch.Get "variableFromShortcode" }}
</div>




<!-- Workaround -->

{{ $callContent := .Content }}

<div id="page">
  FILLED: {{ .Scratch.Get "variableFromShortcode" }}
  <div id="my-content">
    {{ .Content }}
  </div>
  FILLED: {{ .Scratch.Get "variableFromShortcode" }}
</div>
1 Like

Indeed, this is a change to the old behavior. Saved me time bro.