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:
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:
<code><pre>{{ dumpvar . }}</pre></code>
Which gives me something like:
{
"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
?