Am I not understanding isset?

This is driving me nuts. Sample code:

{{ if isset (index .data .tab) "requestBody" }} stuff...
{{ end }}

.data has a map pulled from a yaml in /data. .tab is a known key in the top level of .data. “requestBody” is a key I am testing for, it exists on some pages, and not on others. I would have thought that the above would test for the existence of this key and move on if it was not found. Yet when I try to build the site with pages I know don’t have the key, I get the following error:

executing "main" at <index .data .tab>: error calling index: index of untyped nil

If I only build with the pages that contain the requestBody key, it builds fine. I have verified that both .data and .tab have the expected values, neither is nil. The key identified by .tab exists in .data. Yet I keep getting this error.

Apparently I’m a moron. What am I doing wrong here?

Have a look at and debug what is inside of .data and .tab before you run this if code.

I would assume from the error message, that .tab is not set, so checking for a not set index on .data fails. How is .tab set?

The ones I tested all had valid values. There are over 500 files that are trying to use this template, however, and it’s difficult to test them all one by one. Since Hugo does not tell me which markdown generated the error, only where the error occurred in the template, eg:

Error: Error building site: failed to render pages: render of "page" failed: "/layouts/api/api-doc.html:51:27": execute of template failed: template: api/api-doc.html:51:27: executing "main" at <index $r "content" "application/json">: error calling index: index of nil pointer

This is a different error, but it demonstrates my problem. I know what code in the template is failing, but I do not know what data it’s failing on because Hugo doesn’t tell me the markdown file that attempted to parse this template. So right now I’m debugging one by one. Should be done later this week. :slight_smile:

I’ve tracked down a few of the issues already and it looks like there are definitely cases where some of that data is not found due to incorrect references.

Maybe I should update the question to, Is there any way for Hugo to tell me the markdown file that was being processed when the error was triggered? Knowing the template file is helpful, knowing the markdown file would be more helpful so I can check the data specific to that markdown file.

(index .data .tab) is still being evaluated even on the pages where requestBody does not exist, because it needs to evaluate that before it can check for requestBody. Hence index of untyped nil.

Best to nest your if statements:

{{ if isset .data "tab" }}
{{ if isset (index .data .tab) "requestBody)" }}

You may even need to nest that further to test if .data exists, first.

{{ if isset .data .tab }}

{{ else }}
{{ warnf ... }}


1 Like