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.
{{ $.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
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.
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 }}
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…
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 }}
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.