s1-en
June 11, 2020, 4:10pm
1
I have a bunch of JSON files organized in subdirectories in the data
folder.
data
'- test1
'- 20
'- 06
'- mycontent.json
'- mycontent1.json
'- mycontent2.json
'- 05
'- myothercontent1.json
'- myothercontent2.json
Basically they’re organized by test1 / year / month / content
When I tried to do
{{ range .Site.Data.test1 }}
// do something
{{ end }}
In my template, it turns up empty but it shows up properly when the data files are moved to the top level folder data/test/*
.
How can I recursively parse these data files that are buried in subdirectories?
In Hugo you can’t. There might be some Golang lingo that enables you to recursively scan a folder, but not in Hugo. This might be a question for the developers of Golang templates.
s1-en
June 11, 2020, 4:38pm
3
That’s unfortunate, but I’m glad I asked here instead of spending hours trying to figure it out.
maiki
June 11, 2020, 6:06pm
4
Consider moving your data from data
into a page bundle, and access them as page resources: https://gohugo.io/content-management/page-resources/
bep
June 11, 2020, 6:13pm
5
Hmm. I kind of would expect the above to just work. Can you try to put a dummy JSON file insde /20 and see what happens?
1 Like
s1-en
June 12, 2020, 3:28am
6
Thanks I’ll give it a look once I wake up
It still doesn’t work. It only ever works when the data file is in the root of the path given
bep
June 12, 2020, 7:15am
7
That is not true in general, so there must be something odd with your setup.
s1-en
June 12, 2020, 2:57pm
8
You can test it out → GitHub - s1-en/hugodatatest
When you build the site, you will only see the message from zero level depth, not 1 or 2 levels dee[per
Your data structure looks like this:
map[string]interface {}{
"test":map[string]interface {}{
"20":map[string]interface {}{
"06":map[string]interface {}{
"entry_1":map[string]interface {}{
"test1":[]interface {}{
"Hello from two level depth"
}
}
},
"entry_2":map[string]interface {}{
"test1":[]interface {}{
"Hello from one level depth"
}
}
},
"entry_3":map[string]interface {}{
"test1":[]interface {}{
"Hello from zero level depth"
}
}
}
}
You could recursively get the value of test1
by replacing layouts/partials/test.html
with:
{{- template "print-value-of-test1" . -}}
{{- define "print-value-of-test1" -}}
{{- range . -}}
{{- if reflect.IsMap . -}}
{{- template "print-value-of-test1" . -}}
{{- else -}}
<li>{{ index . 0 }}</li>
{{- end -}}
{{- end -}}
{{- end -}}
There are probably other ways to accomplish this.
1 Like
s1-en
June 13, 2020, 1:55am
10
Thank you so much! This wasn’t exactly what I needed but it was enough to push me in the right direction to figure out what I was doing wrong.
I checked out the Page Resource that maiki suggested but in the end I settled for the data
approach.
system
Closed
June 15, 2020, 1:55am
11
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.