Content adapters: build sections and pages from site.Data

Current examples are like this:

{{/* Get remote data. */}}
{{ $data := dict }}
{{ $path := "data/verses/reality01.json" }}
{{ with resources.Get $path }}
  {{ with . | transform.Unmarshal }}
    {{ $data = . }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get global resource %q" $path }}
{{ end }}

You have to hardcode the file itself. Is there a way for it to process all files inside a directory the way .Site.Data works?

I’m looking for something like this, so it’s easier to manage once the db gets bigger:

/assets/
├── /data/
│   ├── /chars/
│   │   │── angelica.json
│   │   └── thomas.json
│   └── /items/
│       ├── dragonlance.json
│       └── bag-of-holding.json
└── /some-other-assets/

Thank you!

PS

Walk the data structure (a map) using a recursive partial.

I didn’t notice this was in assets. Let me think about it.

And out of curiosity, why does the data need to be in the assets directory?

Oh, I followed this one:

In the example:

assets/
└── data/
    └── books.json

You can do the same thing with the data files in the data directory, and you don’t need to unmarshal them either.

This example builds a site with three sections, all based on files in the data directory. None of the section names or data files are hardcoded in the content adapter.

git clone --single-branch -b hugo-forum-topic-49842 https://github.com/jmooring/hugo-testing hugo-forum-topic-49842
cd hugo-forum-topic-49842
hugo server

Ahh! That’s how you do it. I didn’t think of repeating {{ $.AddPage $page }} and the firstlevel and secondlevel when I first tried it on root /data/ (which ended up with me trying unmarshal method).

Thank you very much! bow /respect