Howto: show what values are passed to a template

If you ever need to drill into what is passed to a template, just put this at the top of your template somewhere:

{{ printf "%#v" . }}

Then look at a page generated by that template - that’ll print out what the top level object is, and what it’s fields are. When I do it with a shortcode template, I get something like this:

&hugolib.ShortcodeWithPage{Params:[]string{"."}, Inner:"", Page:(*hugolib.Page)(0xc2082e4840)}

That tells me there’s a .Params value which is a list of strings (that contains a single string which is just the dot character), there’s a .Inner value which is a string (which is empty), and a Page value, which is a hugolib.Page (the object you get back with various templates from .Page). You can then modify the template to drill down, so now make the template

{{ printf "%#v" .Page }}

That’ll print out what the Page value contains… which is a huge block of stuff, but you should be able understand it, more or less. Repeat as necessary.

13 Likes