Using define blocks in shortcode templates

There’s certain shortcodes that I’d like to be able to use in a markdown template that affect templating/content that is essentially “not in order”. Here’s a simple bit of code to explain.

baseof.html

<html>
    <head>
        {{- partial "head.html" . -}}
    </head>
    <body>
        {{- partial "header.html" . -}}
        {{- block "hero" . }}{{- end }}
        <section id="mainContent">
            {{- block "main" . }}{{- end }}
        </section>
        {{- partial "footer.html" . -}}
    </body>
</html>

_index.md

---
title: "Title"
description: "This is meta description."
draft: false
---
{{< hero >}}
Hero content
{{< /hero >}}

The rest of the content.....

shortcodes/hero.html

{{ define "hero" }}
    {{ .Inner }}
{{ end }}

In my testing, the hero block in the baseof.html template is replaced the the shortcode defined hero block, however, I can’t seem to get the contents of the shortcode. I’ve tried as follows:

{{ .Page.Scratch.Set "heroContent" .Inner }}
{{ define "hero" }}
    {{ .Page.Scratch.Get "heroContent" }}
{{ end }}

That doesn’t seem to work.

I’m just wondering if I’m going around this in the right way, or if there’s a better way to do it? The reason that I want it to be structured like this is that there’s 5 or 6 blocks that can be used that need to go “out of order” and because they can be used in any combination, I don’t want to make separate layouts for each because there’d be 6!.


EDIT

Scrap that, it seems as if the hero block code in the baseof.html file was actually calling the shortcode? Because the shortcode content actually appeared on all the pages.

So I guess my question is how to achieve the desired outcome of being able to inject components in different blocks?

That is not possible.

Here’s an approach that seems to work consistently when building (hugo), but inconsistently when serving (hugo server). It relies on setting a page-level .Scratch variable in a shortcode, and conditionally displaying the .Scratch variable within a template if the .HasShortcode function detects the presence of the shortcode on the page.

Give it a run…

git clone --single-branch -b hugo-forum-topic-26542 https://github.com/jmooring/hugo-testing hugo-forum-topic-26542
cd hugo-forum-topic-26542
hugo server

Caveat Emptor: The fact that this works at all may be a bug.

To clarify, are you saying it’s not possible to use a shortcode to inject content in an arbitrary location on a page?

Please open a new topic describing your objective.