Jsonify filter not working on front matter object


#1

Hey there!

I just build my first Hugo site and I love it so far, but I am stuck on this one issue.
I want to parse a front matter object to JSON, but the build-in jsonify filter is always throwing errors.

The front matter in question:

lightbox:
  - image: teaser/lorem.jpg
    text: >-
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
      eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
      voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
      clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
      amet.
  - text: >-
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
      eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
      voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
      clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
      amet.
    image: teaser/lorem.jpg

The template:

{{ .Params.lightbox | jsonify }}

I also tried:

{{ dict "lightbox" .Params.lightbox | jsonify }}

I am also not sure on how to debug this issue since, the server isn’t throwing any good errors…

ERROR: 2017/01/11 16:58:38 general.go:236: Error while rendering page : template: _default/list.html:12:57: executing "_default/list…

So yeah any help would be appreciated!


#2

It looks like you are treating lightbox like an array. Have you tried ranging through the values instead?


#3

Yes I tried ranging through the values and running jsonify on them threw an error as well…


#4

GH repo you can point me to?


#6

@natorboy Okay, cloned your repo. I think your issue is that you are assume that jsonify creates a full json object rather than just escapes things appropriately to fit into the value in a key:value pair.

I don’t have any issues essentially stringifying your yaml front matter. I assume you want to add this to text.html here: https://github.com/mgrandl/30one/blob/master/site/layouts/partials/text.html

This is an example of how to get it to work. That said, if you want to make a json object, you’re going to need to add something like safeJS if it’s enclosed inside two script tags. I don’t know how you’re creating your lightbox:

{{with .Params.lightbox}}
    {{range $ind,$val := .}}
      {{$val.text | jsonify}}
    {{end}}
{{end}}

HTH.


#7

So it was basically just a misunderstanding of what jsonify was doing. I thought it could be translating nested go maps to json.