Hugo shortcode questions with named parameters and =

I am trying to understand behavior of a shortcode I created.

It seems like whitespace around the “=” and named parameter cause different errors.

(1) Doesn’t work

{{ <myshortcode inputOne = "Holler" inputTwo="World">}}

  • (above) Using space with named parameter, but the inputs have different white spacing around them*

(2) Works

{{ <myshortcode inputOne = "Holler" inputTwo = "World">}}

  • Consistent 1 whitespace around equals sign (=) *

(3) Works
{ <myshortcode inputOne="Holler" inputTwo="World">}}

Consistently no white space around equals signs


Two questions:

Q1: Is this expected?

Q2: In the documentation for shortcodes in Hugo there is mention:

a) of space delimited named parameters for shortcodes
b) position parameters
and examples:
c) some named parameters shortcodes examples with the “=” for named parameters to values

Are all these valid?

Guidance

Use any of these, and nothing else:

<!-- Positional parameters -->
{{< myshortcode postionalParam1 postitionalParam2 >}}
{{< myshortcode "postionalParam1" "postitionalParam2" >}}
{{< myshortcode `postionalParam1` `postitionalParam2` >}}

<!-- Named parameters -->
{{< myshortcode namedParam1=foo namedParam2=bar >}}
{{< myshortcode namedParam1="foo" namedParam2="bar" >}}
{{< myshortcode namedParam1=`foo` namedParam2=`bar` >}}

If you quote a parameter, either positional or named, it is a string.

If you do not quote a parameter, Hugo will determine if it is a string, int, float64, or bool.

If you use single quotes, instead of double quotes or backticks, the resulting string will include the single quotes. Don’t do this, unless for some reason it is intentional.

Explanation

I’m not sure that spaces before the = sign were ever intended to work. Spaces after the = sign are not a problem. Perhaps this is an oversight in the shortcode parser, but since the documentation shows no spaces on either side of the = sign, I would not classify this as a bug.

Please let us know if you find an example in the documentation which doesn’t match the guidance above.

3 Likes

Thank you @jmooring!

Appreciate the response and guidance.

I spent several hour troubleshooting this behavior. I had never dug into the details so this will a key reference for me.

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