Custom Shortcode contains itself

Hey all, if have some issues with a custom shortcode:

This is the shortcode in question:

<div class="d-grid gap-3">
    {{- range $img := split .Inner "\n"}}
        <div>{{ $img }}</div>
    {{- end }}
</div>

This is the input:

{{< imglist >}}
img/laser-burns-the-word-laser.png
{{< imglist />}}

And here is the very strange result:

<div class="d-grid gap-3">
        <div></div>
        <div>img/laser-burns-the-word-laser.png</div>
        <div>&lt;div class=&#34;d-grid gap-3&#34;&gt;</div>
        <div>        &lt;div&gt;&lt;/div&gt;</div>
        <div>&lt;/div&gt;</div>
</div>

As you can see, the .Inner contains the markup from the shortcode itself.

Did i miss something here?

What were you expecting?

No recursion :wink:

<div class="d-grid gap-3">
        <div>img/laser-burns-the-word-laser.png</div>
</div>

Should be the result.

Two issues.

First, you are incorrectly closing the shortcode.

Change this:

{{< imglist />}}

To this:

{{< /imglist >}}

Second, you need to trim leading and trailing newlines:

<div class="d-grid gap-3">
  {{- range split (trim .Inner "\n") "\n" }}
    <div>{{ . }}</div>
  {{- end }}
</div>

Thanks, that worked. Strange behavior.

Not really. By not closing the first shortcode call, you were nesting the second shortcode call (self-closing) within the first.

1 Like

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