Isset or similar function for index

Hello, I am not able to use isset on .Site.Data in the following partial:

{{$scratch := newScratch}}
{{$arguments := dict "dot" . "scratch" $scratch}}
{{block "fields" $arguments}}
	{{$parent := .dot.Parent}}
	{{with $parent}}
		{{if not .IsHome}}
			{{$arguments := dict "dot" . "scratch" $.scratch}}
			{{template "fields" $arguments}}
		{{end}}
	{{end}}
	{{$field := .dot.Render "_view/field" | string | slice}}
	{{.scratch.Add "fields" $field}}
{{end}}
{{$fields := $scratch.Get "fields"}}
{{return index .Site.Data $fields}}

The .Render element creates a slice of the path based on file name. This works if I use a slice that exists, for example this will look for a file in data/wiki/hugo.yml and works as long as a data file exists there:

{{$fields := slice "wiki" "hugo"}}
{{index .Site.Data $fields}}

However, because many of the pages don’t have data files it gives me an error:

hugo_1     | ERROR 2020/04/25 23:18:25 Failed to render pages: render of "page" failed: execute of template failed: template: wiki/single.html:2:3: executing "main" at <partial "data" .>: error calling partial: "/site/themes/test/layouts/partials/data.html:15:2": execute of template failed: template: partials/data.html:15:2: executing "partials/data.html" at <index .Site.Data $fields>: error calling index: index of nil pointer

isset says it works with an index, but I can’t figure out how to get it to work. Ideally Hugo would automatically have a page variable for a data file in a data folder and file with identical names. I’m not opposed to using front-matter params either, but the challenge is that if someone adds metadata to a page that I haven’t created a specific template for like so:

{{range $key, $value := .Params}}
{{if eq $key "test"}}
<span class="{{$key}}">{{$value}}</span>
{{end}}
<details>
<li>{{$key}}: {{$value}}</li>
</details>
{{end}}

The issue with this, is that there are page elements such as title, categories etc that are listed in the .Params and it seems like it can be hard to predict these.

Hi there,

It’s easier to help you if we had a repo we can test instead of trying to recreate your setup. Have a read about Requesting Help .