Bug: .Inner with no closing shortcode is not falsey

This seems to be acknowledged in If/else empty .Inner does not seem to work.

However, this contradicts the documentation in https://gohugo.io/templates/shortcode-templates:

If a closing shortcode is used, the .Inner variable will be populated with the content between the opening and closing shortcodes. If a closing shortcode is required, you can check the length of .Inner as an indicator of its existence.

I expected that if there’s no closing shortcode, .Inner would be an empty string, so checks for closing shortcodes wouldn’t have to use chomp, they could do {{ with .Inner }} directly.

For example, for this shortcode use:

---
date: "2023-01-01"
---

{{< foo >}}

.Inner is "\n".

Case 1: Inline

{{< foo >}}{{< /foo >}} -->  .Inner | len = 0

Case 2: Not inline

{{< foo >}}
{{< /foo >}}  -->  .Inner | len = 1

Case 3: Self-closing

{{< foo />}}  --> .Inner | len = 0

Case 4: Erroneous usage (using .Inner without closing)

{{< foo >}}   --> .Inner | len = 6 (\n+ \n+ 3 + \n)

Bar

Regarding #2, given that we don’t know if one or more leading or trailing newlines is intentional, I think we should leave this to shortcode authors as to whether or not to trim .Inner. However, documentation is misleading, implying that the length is zero:

you can check the length of .Inner as an indicator of its existence.

See https://github.com/gohugoio/hugoDocs/issues/1962

Regarding #4, it seems like we should fix this if possible, instead of relying on content authors to get it right. If the shortcode isn’t closed with either a closing tag or is self-closing, then .Inner should be nil.

See https://github.com/gohugoio/hugo/issues/10671

Additionally, when a shortcode is closed and .Inner is not referenced, we currently emit this:

failed to extract shortcode: shortcode “>}}” has no .Inner, yet a closing tag was provided

That message needs some love.

See https://github.com/gohugoio/hugo/issues/10672

N.B. This discussion applies to .InnerDeindent as well.

1 Like

I am having second thoughts about Case #2 due to comparison of behavior with code block render hooks. With code block render hooks, we trim all trailing newlines. With shortcodes, trailing new lines are not trimmed.

Test cases

Length = 0

```text
```

Length = 0

```text

```

Length = 0

```text


```

Length = 1

```text
a
```

Length = 2

```text

a
```

Length = 3

```text


a
```

Length = 3

```text


a

```

Length = 3

```text


a


```

See https://github.com/gohugoio/hugo/issues/10673

1 Like