Nested shortcodes or partials with arguments inside content

Hi,

I’m just a few days into Hugo (0.36), so heads up for questions with obvious answers :grinning:

First a quick walkthrough

My use case is that I’m creating a documentation page that shows different layout variations with grouped articles in various numbers. I am trying to find a flexible way of building these blocks with shortcodes or partials inside my content files.

I made this shortcode for showing a single article, with a given image ratio:

<!-- /layouts/shortcodes/article.html -->
<article>
	<figure class="image{{ if .Get "ratio" }} ratio-{{ .Get "ratio" }}{{ end }}"></figure>
	<h3>Some dummy headline</h3>
</article>

So when I use {{< article ratio="portrait" >}} in my index.md I get a single article and all is well so far.

For groups of articles I want to use another shortcode, this time with a given template (aka master) name:

<!-- /layouts/shortcodes/master.html -->
<section>
{{ if .IsNamedParams }}
	{{ if .Get "template" | eq "master-a" }}

		<!-- Some layout -->

	{{ else if .Get "template" | eq "master-a" }}

		<!-- Some other layout -->

	{{ end }}
{{ end }}
</section>

I can now use {{< master template="master-a" >}} in my index.md to show a section.

The problem

What I want is to re-use my article shortcode inside the master templates, so that I have a single point of changing the article markup etc. for all my master templates.

I have tried adapting my master shortcode by adding article shortcodes like this:

<!-- /layouts/shortcodes/master.html -->
<section>
{{ if .IsNamedParams }}
	{{ if .Get "template" | eq "master-a" }}

		{{< article ratio="portrait" >}}
		{{< article ratio="square" >}}

	{{ else if .Get "template" | eq "master-a" }}

		{{< article ratio="square" >}}
		{{< article ratio="square" >}}
		{{< article ratio="wide" >}}

	{{ end }}
{{ end }}
</section>

This throws an ERROR Unable to locate template for shortcode "master" in page index.md". From what I understand by now it’s not possible to use a shortcode inside another shortcode, or even partial.

I can’t wrap my head around an easier solution at this point, can someone point me in the right direction for a solution to this little problem?

Thank you in advance!

Shortcodes are meant to be used in your content files you cannot use them in Hugo templates.

You should try to adapt your code in a partial and call that from your templates.