Isset output not as expected

I’m beginning to suspect I might have found a bug, but hoping someone can either verify the same results or explain why the following is not working as expected.

Given the following test code:

{{- warnf ".Params.missingParam: %s" .Params.missingParam -}}
{{- warnf ".Params.existingParam: %s" .Params.existingParam -}}

{{- isset .Params "missingParam" | warnf "isset .Params \"missingParam\": %s" -}}
{{- isset .Params "existingParam" | warnf "isset .Params \"existingParam\": %s" -}}

The following output is generated:

WARN 2020/04/10 23:36:52 .Params.missingParam: %!s(<nil>)
WARN 2020/04/10 23:36:52 .Params.existingParam: I Exist
WARN 2020/04/10 23:36:52 isset .Params "missingParam": %!s(bool=false)
WARN 2020/04/10 23:36:52 isset .Params "existingParam": %!s(bool=false)

Please let me know the results if you try those test snippets.

tested using:
Hugo Static Site Generator v0.68.3/extended darwin/amd64 BuildDate: unknown

What is your expected output?

I expected it to return true when called on “existingParam”, unless I don’t understand what the isset function is supposed to do.

It does. It’s probably warnf that you are misunderstanding.

{{ if isset .Params "missingparam" }}
  do stuff
{{ else }}
  {{ warnf "missingParam" }}
{{ end }}

Ok, but this all started when I tried the following:

{{ if (not (isset .Params "missingparam")) }}
  do something...
{{ end }}

and it didn’t work as expected, so I thought the problem was the not keyword (since there’s no docs on it) and I tried the following:

{{ if isset .Params "missingParam" }}
  <p>Inside missingParam if</p>
{{ end }}

which gives the same results (no output) as:

{{ if isset .Params "existingParam" }}
  <p>Inside existingParam if</p>
{{ end }}

which lead to the tests with warnf (just a way to see the vals without changing the generated files)

So what am I missing about isset?

{{ if (not (isset .Params "missingparam")) }}
  absent
{{ else }}
  present
{{ end }}

works perfectly fine for me. Do you have a repo I can reproduce?

I was testing this in a basic site I use for testing syntax etc. I can upload it though, one sec.

[{{ .Params.missingparam }}]
<br>
{{ (isset .Params "missingparam") }}
<br>
{{ not (isset .Params "missingparam") }}
<br>

should result in

[]
false
true

and

existingParam: "here"

---

[{{ .Params.existingparam }}]
<br>
{{ (isset .Params "existingparam") }}
<br>
{{ not (isset .Params "existingparam") }}
<br>

should result in

[here]
true
false
1 Like

I just got those same results which is really baffling me now. What do you get from these?

  {{ if isset .Params "missingParam" }}
    <p>Inside missingParam if</p>
  {{ end }}

  {{ if isset .Params "existingParam" }}
    <p>Inside existingParam if</p>
  {{ end }}

results in no output:

Also tried with () around the "isset .Params “existingParam”, didn’t change anything. I don’t understand why the if statement isn’t working.

Finally figured it out. “existingParam” vs “existingparam”. Not sure why, but that’s what it was.

Ah. https://gohugo.io/variables/page/#page-level-params

Page-level .Params are only accessible in lowercase.

I never knew that, had never run into that before.

It’s also inconsistent, since it was working in the warnf statements, ie the below prints out the correct value from page front-matter using a camelCase name:

  {{- warnf ".Params.existingParam: %s" .Params.existingParam -}}

So I was seeing it was set and its value output, and was lead in a totally different direction than the actual cause.

Update, just looked over my real site, and I was accessing the many front-matter params with camelCase and it’s working fine. The ONLY place I see it NOT work is in isset.

As an example of the inconsistencies, the following shows all the ways “existingParam” works, and the only case I’ve found where it doesn’t (isset):

existingParam: "I Exist"
---
  {{- warnf ".Params.existingParam: %s" .Params.existingParam -}}

  {{ if (isset .Params "existingParam") }}
    <p>isset .Params "existingParam": true</p>
  {{ else }}
    <p>isset .Params "existingParam": false</p>
  {{ end }}
  
  {{ if (isset .Params "existingparam") }}
    <p>isset .Params "existingparam": true</p>
  {{ else }}
    <p>isset .Params "existingparam": false</p>
  {{ end }}

  {{ $myVar := .Params.existingParam }}
  <p>.Params.existingParam via $myVar: {{ $myVar }}
  
  <p>.Params.existingParam direct: {{ .Params.existingParam }}

outputs:

isset .Params "existingParam": false

isset .Params "existingparam": true

.Params.existingParam via $myVar: I Exist

.Params.existingParam: I Exist