Content adapters: using local data

I have a bunch of local data files named data/news/2024.json, data/news/2023.json etc., each one containing news items that I intend to use to build news pages on a site.

Historically, Hugo has not (AFAIK) provided a way to build pages from data files (a feature available in some other SSGs). In fact, I think that somewhere in the docs there was a stern warning saying “That’s not what it’s for; don’t do this”, or words to that effect.

In other words, there isn’t a way to take my data/news/2024.json and get a news/2024.md from it. I need to create a stub news/2024.md file and then use a layout template to pull in the data from data/news/2024.json.

With the new content adapters, however, it looks as if I can create a _content.gotmpl file in the appropriate place and then iterate over the local data, e.g.

{{ range .Site.Data.news }}
...
{{ $page := ... }}
{{ $.AddPage $page }}
{{ end }}

This seems to work pretty well, and means that I don’t need to repeat myself: I just put the data files in the data/news directory and my pages are generated for me.

My question is: is this a legitimate and safe use of content adapters? It seems reasonable to me, but as it’s not explicitly called out as a possibility in the docs, I wanted to be sure that this wasn’t abusing the feature in a way that might cause it to stop working in some future release.

Yes it is.

That said:

  • I like to think of the /data folder as a place for data that is used by many. It’s read into memory and kept there for the entire build.
  • For data that’s used once (or twice …) I would recommend putting it in /assets, see transform.Unmarshal | Hugo
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.