Help iterating/accessing .Params / .Site.Params with a variable

In the following code, I am failing to come up with the right syntax to access some page/site frontmatter with variables correctly.

I know I can say “give me” but I’d like to say “$key := “author”, give me .Params.$key” in go/Hugo. Can anyone help with a generalized solution to this kind of lookup? Ideally it shouldn’t be .Params specific because I also need to use it with .Site.Params and just want to know in general how this should be done. I didn’t recognize any obvious accessor functions in the docs and I know this is blindly stupid… sorry.

[In this full snippet, I’m trying to output some json for structured data and don’t want to type the exact same code for author, creator, and accountablePersons.]

{{- range $pubfield := (slice "author" "creator" "accountablePersons") }}
    {{- $credit := index .Site.Data.authors (or (.Params.$pubfield) (lower .Site.Params.$pubfield)) }}
    {{- if $credit }}
    "{{ $pubfield }}": {
        {{- range $key, $val := $credit }}
            {{ $key}} : {{ $val }}
        {{- end }}
    {{- end }}
{{- end }}

When you use range or with then inside of the loop you are WITHIN the variable. So .Site.Data.authors inside of range $pubfield is actually $pubfield.Site.Data.authors which probably does not exist in your system.

Easy fix for anything .Site related: use site instead. site is a global variable that is available anywhere. .Site is a scoped variable that is available within the context of the loop you are in.

Small s and no dot in front.