Advice on how to inspect a page context

I am a hugo newbie and I am learning a great many things by inspecting the runtime environment with printf statements. However, I often encounter variables that are some form of arrays or structs and I can’t figure out how to print those in a meaningful way. (For example as a list of keys or key/value pairs.)

# I've added this to my layout to inspect my current context.
{{ printf "%#v" . }}

# This gets me this somewhat useless output 
&hugolib.pageState{pageOutputs:[]*hugolib.pageOutput{(*hugolib.pageOutput)(0xc000be2480), (*hugolib.pageOutput)(0xc000be26c0), (*hugolib.pageOutput)(0xc000be2900), (*hugolib.pageOutput)(0xc000be2480), (*hugolib.pageOutput)(0xc000be26c0), (*hugolib.pageOutput)(0xc000be2900)}, pageOutput:(*hugolib.pageOutput)(0xc000be2480), pageCommon:(*hugolib.pageCommon)(0xc00086a500)}

My question: what is the data structure in the above example and how can I get more meaningful information on the runtime context?

In this particular instance, my bundle content isn’t showing and I would like to figure out if and how it was passed to the layout page.

1 Like

In addition to printf I guess there are two options:

  1. {{ . | debug.Dump }}
  2. {{ . | jsonify (dict "indent" " ") }}
  • The first one doesn’t handle pointers very well, but works really well for many things.
  • The second one does not handle recursive object graphs very well (I think there is an open bug about that in Go, not sure what the status is).

That said, since the “.” in Hugo often is a Page and a Page references everything, I would recommend that you do these debug prints on a little more fine grained level, e.g. what you’re looking for {{ .Params | debug.Dump }}

Hmm, 1 returns output similar to my printf statement and 2 crashes on about everything I try it on.

But I guess my real question is rather, how can I debug what type my object is and what variables it provides? Once I know the . object has a variable called Params I know what to do but I am often stuck at the step before that.

Here is another snippet from my code, this one not related to Page context. Once again I tried to use inspection to figure out what type of object I was facing in $photo

 {{ $photo := resources.Get .Params.photo }}
 {{ printf "processing %#v" .Params.photo }}
# this is the output
&resources.resourceAdapter{commonResource:resources.commonResource{}, resourceTransformations:(*resources.resourceTransformations)(0xc00913ba40), resourceAdapterInner:(*resources.resourceAdapterInner)(0xc00807c180)}

I take your point that I should be more specific about what I want to print but if I don’t know what is available, I can’t break down my call to a more fine-grained level.

There is a thing called documentation, but I agree that this could be improved.

1 Like

Translation: Have a look at the following Golang (not Hugo) resources:

Those docs should help you to find a way to debug your variables. It works quite nicely with most of these variables, but some of those documented as variables in GoHugo’s documentation are actually functions, so results might differ. Just “debugging” the a .Page “object” might not show you all it has to give.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.