I have developed a go template (_content.gotmpl) which generates pages from the site/data folder and adds taxonomies from the JSON. The issue I am having is figuring out how to parse a JSON that contains multiple levels. The ideal scenario would be the content adapter adds a page for each entry in the JSON, and for child entries it creates sub pages, and recursively goes down through each level populating pages with both taxonomies and child pages.
I have prepared a JSON that follows this structure (items in parenthesis would be taxonomies):
School: Greenwood High (Location: New York)
Class: Grade 1
Student: Alice (ID: 101, Age: 6)
Student: Bob (ID: 102, Age: 7)
Class: Grade 2
Student: Charlie (ID: 201, Age: 7)
Student: Daisy (ID: 202, Age: 8)
School: Sunnydale Academy (Location: Los Angeles)
Class: Grade 3
Student: Ethan (ID: 301, Age: 10)
Student: Fiona (ID: 302, Age: 11)
Class: Grade 4
Student: George (ID: 401, Age: 12)
Student: Hannah (ID: 402, Age: 12)
School: Riverside School (Location: Chicago)
Class: Grade 5
Student: Ivy (ID: 501, Age: 9)
Student: Jack (ID: 502, Age: 10)
Class: Grade 6
No students
The expected result where I put the go template would be: 3 page links for each school
Above is one of my many attempts to do nested loops and add pages within pages.
My question is: what are the limitations of go templates, is it possible to use the addPage method multiple in a nested loop, or am I limited to Hugos integration of golang and templates?A recursive loop seems like it would be relatively simple in base go- but in the hugo framework things are evidently more complex.
Thanks @jmooring and @ethanjose1 - this is great - I have a similar use case that has recursion that could go down deeper - I would want to almost have a json that could extend here - so you could do a family tree:
This cascading of information and presentation into a site has a ton of use cases - so thanks for helping us here guys - in my json the educational level would be a taxonomy term.
The template example above explicitly creates three levels… it is not recursive. You could certainly create a recursive content adapter, but it could get tricky if the level structures are not consistent.
Would it be expected for taxonomy tags to appear at each individual page? If I click “Bob” do I need to change the params definition at the student level to show “age” and “student_id” as taxonomy tags? (given I have configured a taxonomies.toml, and params.toml to render them)
You would need to set a tags (or whatever taxonomy) key under the params key. I didn’t do that in the example above.
Keep in mind that you cannot define a taxonomy within a content adapter. You have to know the taxonomies ahead of time so that you can define them in your site configuration.
And looking at the metadata in your JSON, it wasn’t clear to me why any of these would be a taxonomy, with the possible exception of location.
In the above note that we’re setting locations to a slice, despite the fact that there’s only one value. Most templates/themes that I have seen expect a slice, so it’s good to get into the habit.
This assumes you’ve defined the taxonomy in your site configuration:
I can safely say that my issue is resolved - using explicit declarations of each level.
I must admit I am curious how a recursive go template could be created to do this in a one-size-fits all sort of way (in terms of JSON structure) this would allow for bulk page creation without needing to define levels in the go template- but rather let the loop crank through all levels.
I would love to explore how Hugo would handle this tree like structure for pages- as it could offload a lot of manual work structuring the site and designing loops.
My ask is: could I declare this issue solved and open a discussion to investigate this? Any help from the community would be amazing!