Back when output formats (http://gohugo.io/extras/output-formats/) I tried my best to grok the docs but am still failing to get how this is used in the real world. I’m starting with what I assume is a simple demo. I want to build a page to output JSON.
Which I recognize isn’t valid JSON (the stuff at the bottom), but I assumed that hitting /feed/json would return the content by itself with no layout. However, it still uses the main site layout. So am I just using this feature wrong?
As a follow up, obviously I don’t want hard coded JSON, but something dynamic. Hugo still does not want you to write code in your /content folder, correct? So I’d need to build a shortcode perhaps and put the logic there?
Have a look at this theme’s example site. It defines the outputs on a site level (i.e. in config.toml), but it is also do specify this on page level i.e. in page frontmatter.
If you look at the above, it specifices that it wants the home page rendered as HTML, RSS and JSON, and then provides templates for the JSON homepage:
So given a new page, let’s say foo.md. If I specify JSON as an output, and only json, should it expect to find /layouts/foo.json to handle rendering it?
No, but again, this is well documented and I have given you a concrete example. Re. the templates, the behaviour mimics that of the HTML output format. And you would never have expected Hugo to have a 1:1 mapping between the Markdown pages and the templates?
I apologize as I’m simply having difficultly mapping the example you linked to to the more general concept of how this is supposed to work. So given my page, let’s say /content/foo.md, where my intent is for it to output JSON, it sounds like you are saying I’m perhaps missing the layout front matter to tell it what layout to use. Is that right? But I had thought that this new feature supported the layout automatically.
I think my issue is, given /content/X,md, how do I get it to run a layout that will be just JSON and nothing more. Using the outputs front matter is required, but I don’t know the next step.
You need a template. You can either be specific (by setting the layout=somelayout in frontmatter), but just to get something working I would suggest just adding:
layouts/_default/single.json
Hugo’s template handling is a core feature to understand, even if your output format is just plain old HTML.
Ok, so I’ve made some progress. I created /layouts/_default/single.json. With my content page saying it outputs just JSON, my assumption is that it would load this template automatically. Now when I run my request, I don’t get the regular HTML output, I get absolutely nothing. So I think I’m on the right track, but can you explain why nothing is being shown?
My template is just this for now:
hELLO
{{ .Content }}
which I know isn’t valid JSON output, but for now I just want to see my output - I’ll make it real later.
And like magic - I finally got it. Running the output helped flesh out where the file was being created. I think one of the biggest issues I have with Hugo is that I think of layouts as always being wrappers, not actually generating page content as well. Thank you for being patient. My goal is to generate a new template for this: https://jsonfeed.org/
When I get this working, I’ll blog, and post back here. I’d love your opinion on my solution (in case I do something stupid
I have not been able to get it to work. I have a simple search index I am trying to create with json. My file is at content/json/search.json (same if I do search.md or put it at /content/search.json (or search.md), it just doesn’t appear to run it through the template.