# Revisiting template debugging via printf

**URL:** <https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379>\
**Category:** support\
**Created:** [January 2, 2016, 6:48am UTC](https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379 "2016-01-02T06:48:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_henderson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/michael_henderson/32/10490_2.png) [@michael\_henderson](https://discourse.gohugo.io/u/michael_henderson)\
**Post date:** [January 2, 2016, 6:48am UTC](https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379/1 "2016-01-02T06:48:52Z")

</div>

I’ve been looking at `{{ printf( "%v" ...) }}`. It works well for simple variables, but when I’m looking at a larger level, it’s just too hard for me to read.

I’ve been looking at using `reflect` in a pretty-print function. It’s crufty.

I tried adding `String()` methods to `Node`, `Page`, etc, that wrap up the output in `<li>...</li>` pairs doesn’t feel like a very clean solution.

My favorite is using the JSON marshaling function. Except for the (expected) quoting, it does what I’m looking for. It’s a simple function, too:

```auto
func DumpVar(v interface{}) string {
	j,err := json.MarshalIndent(v, "", " ")
	if err != nil {
		return ""
	}
	return string(j)
}

```

Mapping “dumpvar” to `DumpVar` in `tpl/template_funcs.go` allows for:

```auto
<code><pre>{{ dumpvar . }}</pre></code>

```

Which gives me something like:

```auto
{
  "Version": "0.16-DEV",
  "Generator": "\u003cmeta name=\"generator\" content=\"Hugo 0.16-DEV\" /\u003e",
  "CommitHash": "",
  "BuildDate": ""
}
"\u003cmeta name=\"generator\" content=\"Hugo 0.16-DEV\" /\u003e"
{
  "RSSLink": "http://localhost:1313/index.xml",
  "Data": {
    "Pages": [
      {
        "Params": {
          "series": [
            "using"
          ]
        },

```

And I can create a partial to wrap the code+pre for me.

Is this a general enough solution or should I keep banging away on `reflect`?

---

<div class="post-metadata">

**Author:** ![Shannon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/shannon/32/8158_2.png) [@Shannon](https://discourse.gohugo.io/u/Shannon)\
**Post date:** [November 16, 2016, 3:07am UTC](https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379/2 "2016-11-16T03:07:27Z")

</div>

I know this was a long time back… but are you still working on this? I have some interest in this type of debugging.

I believe this, or something like it would be helpful in understanding what Hugo and/or the Go template system is doing.

Something simpler would be nice as well, like dumping nested data:

main  
menu  
apple  
imac.html  
mini.html  
macpro.html

…that sort of thing. taxonomies, menus, etc.

---

<div class="post-metadata">

**Author:** ![michael\_henderson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/michael_henderson/32/10490_2.png) [@michael\_henderson](https://discourse.gohugo.io/u/michael_henderson)\
**Post date:** [November 16, 2016, 1:14pm UTC](https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379/3 "2016-11-16T13:14:40Z")

</div>

The PR was rejected. The concept didn’t add value.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [November 16, 2016, 5:39pm UTC](https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379/4 "2016-11-16T17:39:54Z")

</div>

> [@michael\_henderson](#):
>
> \<code\>\<pre\>{{ dumpvar . }}\</pre\>\</code\>

The dumpvar func above is the same as doing:

```html
<code><pre>{{ . | jsonify }}</pre></code>

```

There are, however, two problems with the above:

- It doesn’t handle cyclic references. It will not panic, but you will not get .Page.Site and some other entities.
- It doesn’t handle funcs – and that will miss out on a lot

We can make some custom marshaler for this, but that is work, and I want to wait with that until we have a stable object model.

---

<div class="post-metadata">

**Author:** ![michael\_henderson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/michael_henderson/32/10490_2.png) [@michael\_henderson](https://discourse.gohugo.io/u/michael_henderson)\
**Post date:** [November 16, 2016, 9:34pm UTC](https://discourse.gohugo.io/t/revisiting-template-debugging-via-printf/2379/5 "2016-11-16T21:34:45Z")

</div>

Yep. That’s part of why the concept didn’t add value. It needed much more work.
