To debug which layout is being used, make a config param to check

I just thought of a way to make a debug tool for which layout gets used in a render, if it’s not clear.

Correct me if I’m wrong, but you could make a parameter in the config file called debug = "true" and in your layouts you put a bit wherever you want the debug print code to go and you wrap it with a {{ when .Site.Params.debug }} debug text here {{ end }}

1 Like

That’s a great idea. I started using it. Then, in my baseof.html I added a {{ block "debug" . }} and started putting all my debug data there.

I wonder if you can add your own flags to the CLI commands. That way you could do something like hugo server -debug instead of using the variable in the config file.

you can’t add any commands to cli hugo, unless you contribute to the source. the config was the best way I could think.

Would this be something other’s will use? I’ve been wanting it for a little while so I can make sure I’m not loading Google Analytics when debugging and running the site locally.

I was thinking of when you run the server as:

hugo serve --debug

then it will set a variable such as {{ .Hugo.IsDebug }} to true and so you could use that to conditionally load scripts, such as GA, or for the debug tool that you were suggesting @gaetawoo or anything else.

If this is something that’ll be useful then I’d be happy to submit a PR :slight_smile:

You can use an environment variable for that.
But yeah, a flag for the CLI command it’s the best way to go

i think there is already either in the works or just released, a debug parameter for the server, but it doesn’t do anything like what i’ve described. but yea it would be useful as a cli command parameter

Based on this idea, I created a debugging system that I’m using on all my projects now.
Instead of printing directly to the page, I’m printing on my console log. That way I can always have the debug info at hand, without having to ‘turn it off’ when I’m working on the looks of my site.
And I’m using an environment variable that is set on my .bash_profile. That way I can use it on any project I’m working on in my computer very easily. I only need to add the partial and the declaration to my baseof template.
Other advantage is that I can deploy safely at any moment, knowing that the debugging info will only be available on my computer.

  1. I added an environment variable on my .bash_profile. This could be anything, really. I used: LOCAL_DEV=true
  2. I added a partial with a script that logs debugging info on my console:
<script type="text/javascript">
  console.log("Kind: {{ .Kind }}");
  console.log("{{ printf "%#v" . }}")
  1. I added a conditional that displays that displays the partial on my baseof.html
{{ if eq (getenv "LOCAL_DEV") "true" }}
    {{ partial "debug.html" . }}
  {{ end }}

I hope this can be useful to somebody else!