Exisistence of Shortcode Parameters

these work fine in that they are site or page parameters
{{ if .Site.Params.myparameter }} {{ end }}
{{ with .Site.Params.myparameter }} {{ end }}

but…can’t get similar syntax to work for shortcode parameters.

example of things that DON’T work

{{ if .Get "size" }} only do if exists  {{ end }}
{{ if isset (.Get "size") }} only do if exists  {{ end }}
this one gives error "wrong number of args for isset: want 2 got 1"
{{ if isset .Get "size" }} only do if exists  {{ end }}
wrong number of args for Get: want 1 got 0
{{ with .Get "size"  }} only do if exists  {{ end }}
nor
with  (.Get "size")  in the above.

Am I having a syntax issue or a scoping issue. I don’t know the error messages I get from Hugo are not helping.
What is the proper way to test for existence of shortcode parameters?
Must I use scratch like this?? https://discuss.gohugo.io/t/solved-setting-a-variable-to-one-of-two-values-in-a-shortcode/4444/6

ok this works

{{ $.Scratch.Set "size" (.Get "size") }}
{{ $size := $.Scratch.Get "size" }}
{{ if $size }}
<div class="">
  <p>What was in "size" if of course it exists {{ $size }}</p>
</div>
{{ end }}

so is this my only option?? Some explanation would be helpful @bep or @moorereason thx

Both with and if should work fine in your case. The built-in figure shortcode is battle-tested in this area:

Indeed it’s as I I thought! thx @bep for confirmation

Ok so It’s working now…! {{ with .Get “icon” }}

<a
{{ if eq (.Get "display") "tab" }}target="_blank"{{ end }}
{{ if eq (.Get "display") "modal" }}rel="modal:open"{{ end }}
{{ if eq (.Get "type") "btn" }}class="box box--btn btn btn--{{ .Get "size" | default "regular" }}"{{ end }}
href="{{ .Get "url" }}">
{{ with .Get "icon" }}<i class="btn__icon fa fa-{{ . }}"></i>{{ end }}
{{ if eq (.Get "type") "btn" }}<div class="btn__text">{{ .Get "text" }}</div>
{{ else }}
{{ .Get "text" }}
{{ end }}
</a>

shrug…I have had nothing but trouble today getting what should work to work. I have to keep stopping the server and restarting and refreshing the browser to see my changes to the shortcode which hasn’t been the case. I’m sure it’s related to why this is not being compiled consistently. Time to reboot my machine maybe.

1 Like

How would you test the presence of a positional parameter?

In my case, I have a shortcode with 2 positional parameters. The first one is required, and the second one is optional. I can’t find a way to test whether the second parameter is defined or not.

None of this works:

{{ if isset .Get 1 }}<h4>{{ .Get 1 | markdownify }}</h4>{{ end }}
{{ with (.Get 1) }}<h4>{{ .Get 1 | markdownify }}</h4>{{ end }}

Remember that with binds the context, so replace your with statement with {{ . | markdownify }} and LMK if that works…

Right. I forgot about that, thanks.

Unfortunately it still doesn’t work. The following message appears in the HTML at the location of the .Get 1 param :

error: index out of range for positional param at position 1

Can you point me to a bit more of your templating? I need some more context.

Sure.

Here’s the template for my shortcode (in MYTHEME/layouts/shortcodes/alert.html) :

<!-- NB. This version produces the "index out of range" error -->
<div class="alert alert-{{ .Get 0 }}" role="alert">
  {{ with (.Get 1) }}<h4 class="alert-heading">{{ . | markdownify }}</h4>{{ end }}
  {{ .Inner }}
</div>

And this is how I invoke the shortcode :

<!-- NB. Removing "Some title" triggers the index out of range error -->
{{% alert warning "Some title" %}}
Some content
{{% /alert %}}

Can you do me a favor and see what happens if you put the “warning” in quotes…

I appreciates this isn’t the best debugging approach, btw, but I’m not really at my computer and just sporadically trying to throw out some ideas here…

No problem at all. And thanks for your help btw.

The “warning” in quotes makes no difference. The error is still here.

Hmmm. Odd. How about the following?

<div class="alert alert-{{ .Get 0 }}" role="alert">
  {{ if ne (.Get 1) "" }}<h4 class="alert-heading">{{ .Get 1 | markdownify }}</h4>{{ end }}
  {{ .Inner }}
</div>

Again, not tested and just spitballing…

Also, if you are adding and removing shortcodes with the live server, please try and kill and then restart the server. Oh, and what version of Hugo are you using when you run hugo version

Same error with {{ if ne (.Get 1) "" }}. From the error message it looks like I can’t access index 1 of .Get so it kind of makes sense that the code above triggers the error.

What I don’t get is why this fails: {{ if isset .Get 1 }}

I’m using Hugo v0.20.

So let me ask some more basic questions:

  1. You are definitely calling these inside your content files, correct?
  2. Are you sure that every time you call the shortcode, you’re doing so in a uniform manner?
  3. What version of Hugo are you using?
  4. Did you try my recommendation to kill the server and start it again in the event that you are creating these in development?

Something is going on at the templating level. As bep mentions above, the with statements are well battle tested for the embedded templates:

Can you point me to a repo? I get the feeling maybe you are calling one of these shortcodes differently in one of your content files…

1. You are definitely calling these inside your content files, correct?

Yes.

2. Are you sure that every time you call the shortcode, you’re doing so in a uniform manner?

Yes. I’m calling it exactly the way the way I showed you in here.

3. What version of Hugo are you using?

v0.20

4. Did you try my recommendation to kill the server and start it again in the event that you are creating these in development?

Yes, after each variation I tried.


The code sample you provided used named parameters, not positional ones. I’m thinking I need to use named parameters too to get it to work. Anyways, thanks a lot for your time and your help.