In my shortcode, I tried to use the following isset code to apply a conditional logic depending on whether an id parameter is passed to the shortcode. This works when the shortcode is called with an id parameter, but the isset condition causes a panic runtime error when the shortcode is called without parameters.
{{ if (isset .Params "id") }}
Param $id is set = "{{ .Get "id" }}".
{{ else }}
Param $id is not set.
{{ end }}
When I use similar code to check whether an unnamed parameter was passed as the first shortcode parameter (0), everything works as expected, including when the shortcode is called without parameters:
{{ if (isset .Params 0) }}
Param 0 is set = "{{ .Get 0 }}".
{{ else }}
Param 0 is not set.
{{ end }}
I also tested the following, based on answers I saw in the forum, and it causes a similar panic error when the shortcode is called without an id parameter. In addition, when the shortcode is called with an id parameter I get the error “can’t evaluate field Params in type string” for the Get call in the with clause (and the same if I use .Params.id instead of .Get "id"):
{{ with .Params.id }}
Param $id is set = "{{ .Get "id" }}".
{{ else }}
Param $id is not set.
{{ end }}
I am using Hugo version 0.24 on Windows (in a Unix Git shell).
[UPDATE] Based on the with example in the Hugo Shortocdes doc (https://gohugo.io/extras/shortcodes/#creating-your-own-shortcodes), I found that the following code works both when the shortcode receives an id parameter and when it does not:
{{ with (.Get "id") }}
$id = "{{ . }}".
{{ else }}
No shortcode $id parameter.
{{ end }
However, when the id parameter is explicitly set to an empty string in the shortcode call (id="") the else clause is executed (the same as when id is not passed at all), unlike what you would expect for an isset condition. While this can certainly be useful, there are situations where I would like the behavior to be different if the user explicitly passes an empty string or doesn’t pass it at all, which I should be able to achieve with isset except it doesn’t work for me for named shortcode parameters.