How can I render shortcodes within a snippet?

I’m trying to create a way to reuse content in our knowledge base. For example, I have a section that applies to 5 platforms and want to maintain it in one place rather than copy/pasting it across all 5 of the platform articles. So my idea was to create a shortcode called shared-content.html, which has the following:

{{ $path := printf "/content/snippets/%s.md" (.Get 0) }}
{{ readFile $path | .Page.RenderString }}

Then I created content/snippets and added sso-groups.md to it, which contains content like this:

## Step 3

{{< callout important >}}
important details
{{< /callout >}}

etc etc

To use that content, I added it to one of our articles in content/article with this line:

{{< shared-content "sso-groups" >}}

The issue is, the callout shortcode, and others in the sso-groups.md file, are not rendering in the final article. All other Markdown renders fine, but the shortcodes don’t. Is this not supported, or am I not nesting things properly? Thanks!

have a look at that one. seems to be a perfect fit.

and have alook at the topics below

Thanks! Sadly I’m currently stuck on Hugo 0.88.1 due to some CI/CD stuff at my company, so this solution won’t quite work. Is there another way?

Capture/render the snippet as a page resource. There really isn’t a good reason to use the os.ReadFile function anymore.

layouts/shortcodes/shared-content.html
{{ with .Get 0 }}
  {{ $path := printf "/snippets/%s" . }}
  {{ with site.GetPage $path }}
  {{ .Content }}{{/* Do not indent. */}}
  {{ else }}
    {{ errorf "The %s shortcode was unable to get %s: see %s" $.Name $path $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requries a single positional paramter, the name of a snippet file: see %s" .Name .Position }}
{{ end }}

Call the outer shortcode using the {{% %}} notation.

{{% shared-content snippet-1 %}}

White the above isn’t strictly necessary in your case, you should get into the habit of doing this with “include” shortcodes in preparation for being to use the RenderShortcodes method when you’re able to upgrade to v0.117.0 or later.

Thank you so much for the help again! This is exactly what I needed and it solved the rendering issues. Very excited to go implement it and test it out fully.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.