Mutliple Shortcodes on the Same Page?

I’ve run into a weird one here.

I’ve got a page with two embedded blocks of HTML which I’ve extracted to Shortcodes to keep the page Markdown tidy. There are no template functions or anything fancy in either shortcode, just plain old blocks of HTML which are too complex to render directly in Markdown.

On the page in question, I have the following:

{{< shortcode1 >}}{{< /shortcode1 >}}

{{< shortcode2 >}}{{< /shortcode2 >}}

When I generate the page, only the HTML in ‘shortcode1’ is output. If I swap the order so ‘shortcode2’ comes first, only the HTML for 'shortcode2’is output. So both shortcodes are working fine individually, but when I embed them both on a page together, only the first one is included.

Any idea why that is? There’s not an unwritten rule that a page can only contain one type of shortcode, is there?

This works fine for me. Do you see some error in the log?

Ah. I never even spotted that, as it was only a one-line error:

ERROR: 2015/11/11 Shortcode 'shortcode1' in page '' has no .Inner, yet a closing tag was provided

Similar error if I switch the order so the other shortcode comes first:

ERROR: 2015/11/11 Shortcode 'shortcode2' in page '' has no .Inner, yet a closing tag was provided

Weird ???

Almost looks like Hugo’s complaining that I’ve not added any template tags inside the shortcode [as I said above, it’s just plain HTML]. But, if that’s the case, why does it work when I only use one of the shortcodes on the page?

I wrote the shortcode part of Hugo. And now you get what I would call the perfect error message, and still you complain :slight_smile:

I think you will find that the first shortcode isn’t really working, either.

Just remove the closing shortcode and it should work:

{{< shortcode1 >}}

I can explain the motivation behind this:

In Hugo there are 3 variants (x 2):

{{< shortcode1 >}}
{{< shortcode2 >}}{{< /shortcode2 >}}
{{< shortcode3 />}}

To distinguish between the first and second, we could either:

  1. Look for a end tag that may or may not be there (until EOF)
  2. Look in the template for an .Inner usage.

The second one is much cheaper, so that is how I implemented it. And hence you get an error because you have a closing tag but no .Inner in your shortcode.


Yep. that fixed it. thanks for your help.

That makes sense, now you’ve explained the mechanics behind it.

I had been expecting the shortcode tags syntax to be functionally similar the the HTML tags they resemble. So had tried both {{< shortcode1 >}}{{< /shortcode1 >}} and the [undocumented?] self-closing version {{< shortcode1 />}}

It’s in the shortcode page in the doc. The selfclosing variant is for shortcodes that may or may not be used with inner content.