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:

β”œβ”€β”€ /data/
β”‚   β”œβ”€β”€ /chars/
β”‚   β”‚   │── angelica.json
β”‚   β”‚   └── thomas.json
β”‚   └── /items/
β”‚       β”œβ”€β”€ dragonlance.json
β”‚       └── bag-of-holding.json
└── /some-other-assets/

Thank you!


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:

└── 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.

1 Like

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 hugo-forum-topic-49842
cd hugo-forum-topic-49842
hugo server
1 Like

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

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