Hello,
I have a test page in Markdown as follows:
+++
title = "Custom front matter with list values in TOML"
tags = ["custom-fm", "list-values"]
draft = false
animals = ["dog", "cat", "penguin", "mountain gorilla"]
strings-symbols = ["abc", "def", "two words"]
integers = [123, -5, 17, 1_234]
floats = [12.3, -5.0, -1.7e-05]
booleans = [true, false]
+++
blah
Link
This is the single.html in layout that I use for debug. The relevant portion is:
<hr>
<h3>All Page Params (for debug)</h3>
<p>
{{ range .Params }}
{{ printf "%#v" . }} <br/>
{{ end }}
</p>
But that outputs: https://ox-hugo.scripter.co/test/posts/custom-front-matter-with-list-values-toml/
Notice that for animals = ["dog", "cat", "penguin", "mountain gorilla"], it is printing only the value portion. How do I print the key portion (animals) too?
Thanks.
Shot in the dark: haven’t tried it but would a different printf verb work? Like %+v, %s etc.
Thanks. I tried that but that didn’t work.
And of course printing within that range won’t work, as {{ printf "%#v" foo }} would ofcourse print foo's value.
So I got the whole .Params to print with keys and values with:
{{ printf "%#v" .Params }}
but it’s a big mess. Would anyone know how to pretty print a map?
Another way to solve this would be if there’s a way to print a variable name instead of it’s value… is that possible in Go templates? Something like {{ printf "%#v" (variable-name .) }}?
OK, I got what I wanted with:
{{ range $key, $value := .Params }}
{{ printf "%#v = %#v" $key $value }} <br/>
{{ end }}
Making it a bit better visually:
<table>
{{ range $key, $value := .Params }}
{{ printf "<tr><td>%#v</td><td>%#v</td></tr>" $key $value | safeHTML }}
{{ end }}
</table>
6 Likes
This is super useful, thanks!