How to inspect variables in the current scope in Hugo?

I’m trying to see what variables are available under “the dot”.

According to the docs, {{ printf "%#v" . }} is what I’m looking for.

But it only seems to print a bunch of memory adresses.

For instance, in a place where the dot contains .Params and a bunch of other variables, this is what {{ printf "%#v" . }} yields:

&hugolib.pageState{pageOutputs:[]*hugolib.pageOutput{(*hugolib.pageOutput)(0xc0008cad80),
(*hugolib.pageOutput)(0xc0008cafc0)},
pageOutput:(*hugolib.pageOutput)(0xc0008cad80),
pageCommon:(*hugolib.pageCommon)(0xc00052b400)}

I’m not sure what that is, but it doesn’t mention what is available in the current context.


(PS: posted the exact same question on Stack Overflow but didn’t get answer there even with a bounty…)

Note that I’m not convinced it will improve your example too much, but you can try

 {{ debug.Dump . }}

Thanks! {{ debug.Dump . }} works well in some places, in some others it only gives &hugolib.pageState{}

Not the perfect solution, but helpful in some situations.

The library that backs debug.Dump isn’t great, so we should look for an alternative. I noticed @moorereason introduced another “pretty printer” in a PR, maybe we could consider that – but Hugo’s Page is composed of lots of small interfaces, and that is something the reflection based tools doesn’t do well with.

Have you tried

{{ . | jsonify (dict “indent” " ") }}

1 Like

{{ . | jsonify (dict "indent" " ") }}
Works better!

I haven’t tested it thoroughly, but it did dump everything everywhere I tested it, including places where both {{ printf "%#v" . }} and {{ debug.Dump . }} gave unhelpful outputs.

1 Like