HUGO

Getting a list from within a shortcode

I’m trying to get a list from within a shortcode.

{{< shortcode 
      name="Peter Parker"
      aliases="* Spiderman * Petey">}}

And the shortcode

{{ .Get "Name" | markdownify }}
{{ .Get "Aliases" | markdownify }}

It displays well the first LI, but the second one comes raw (as an asterisk) and it doesn’t add any new line markup.

Aliases:
• Spiderman * Something

If within the shortcode I try to leave the new line, this way

{{< shortcode 
      name="Peter Parker"
      aliases=" * Spiderman 
                * Petey">}}

It ends up by giving me an error

unterminated quoted string in shortcode parameter-argument: '* Alias * Something

I have a suggestion. What about using split?

So your shortcode could be something like:

{{ .Get "name" | markdownify }}
{{ $aliases := split (.Get "aliases") "," }}

<ul>
  {{ range $aliases }}
    <li>{{ . }}</li>
  {{ end }}
</ul>

And be used like:

{{< shortcode 
      name="Peter Parker"
      aliases="Spiderman,Petey">}}
1 Like

Great suggestion from @sephore. Do remember that white-space at the end of an </li> is significant, so if you’re getting mystery white-space in between items that you don’t want. . .

<ul>
 {{ range $aliases }}<li>{{ . }}</li>{{ end }}
</ul>
1 Like

Works like charm!!!

Suggestion accepted! Lovely, thanks!

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