While building this website I needed to understand a bit more about the internals and its variables. Even though Hugo’s documentation about debugging is pretty “okay”, there are a lot of attributes that are really hard to figure out, specially if you can’t see them on the documentation.
So I started to look online for something to help me out debugging Hugo templates and the usual 3 options were:
- π¨ printing the dot on Hugo’s templates
- π
console.log
the whole dot variable - β using Hugo debug themes
Printing “the dot”
In any hugo template, if you do this:
|
|
You’ll get something like this:
Which to be honest is not the best dev friendly output to figure out what can you use. Also, it hides a lot of variables that you have access to, for example, printing the site
variable only shows this:
|
|
When you also have access to site.Params
, which is not showing on the above output:
|
|
Console.log everything
In your html files, put a snippet like the following:
|
|
This will log the same object that we had with the previous approach, but you can manipulate it on the browser’s console tab. You can always move that snippet into a shortcode and use it like this whenever you need to debug something. π€
|
|
Although this way is nicer (I can play around with the object) it still doesn’t show me all available variables.
There was also a similar solution posted on hugo’s discourse forums that involves in
console.log
every variable, which seems to be a bit to much. Although if we put it in a partial, its kinda cool.
Using Hugo debug themes
Basically, the two options combined π.
We just need to install it, add hugo-debugprint
to our themes
variable on our config.toml and use the shortcode that the template offers:
|
|
Resources
- Printing “the dot”: https://discourse.gohugo.io/t/howto-show-what-values-are-passed-to-a-template/41
- Console.log everything: https://discourse.gohugo.io/t/easier-debugging-hugo-variables-using-the-javascript-console/22873
- Using Hugo debug themes: https://github.com/kaushalmodi/hugo-debugprint
- Context variables: https://www.smashingmagazine.com/2021/02/context-variables-hugo-static-site-generator/
π