I am opening a new topic here to discuss and learn how to create a recursive go template using a JSON file- with a tree like nested structure.
This topic stems from a support post I made previously regarding help with content adapters on a nested JSON with defined levels. Which can be found here: https://discourse.gohugo.io/t/using-content-adapters-to-add-pages-nested-structure/52813/9
This original post has made me curious about recursively iterating over a JSON and its levels instead of manually doing so. The potential here would be to offload manual creation of pages/structures and instead allow for bulk uploads per a formatted JSON. Another potential use case would be in situations where levels of nesting differ for different child nodes- could be more flexible than a rigid hierarchy.
Below is some sample data I was considering.
{
"topics": [
{
"id": "1",
"title": "Root Topic 1",
"description": "This is the first root topic.",
"metadata": {
"created_by": "User1",
"created_at": "2024-12-20"
},
"children": [
{
"id": "1.1",
"title": "Child Topic 1.1",
"description": "This is the first child of Root Topic 1.",
"metadata": {
"created_by": "User2",
"created_at": "2024-12-21"
},
"children": [
{
"id": "1.1.1",
"title": "Sub-child Topic 1.1.1",
"description": "This is the first sub-child of Child Topic 1.1.",
"metadata": {
"created_by": "User3",
"created_at": "2024-12-22"
},
"children": []
}
]
},
{
"id": "1.2",
"title": "Child Topic 1.2",
"description": "This is the second child of Root Topic 1.",
"metadata": {
"created_by": "User4",
"created_at": "2024-12-23"
},
"children": []
}
]
},
{
"id": "2",
"title": "Root Topic 2",
"description": "This is the second root topic.",
"metadata": {
"created_by": "User5",
"created_at": "2024-12-24"
},
"children": []
}
]
}
My limited understanding makes me think we would expect something like this: where we define how to add pages, then call it through each level
{{/* Recursive function to create pages from nested data */}}
{{ define "createPages" }}
{{- $data := site.Data.nested_recursive_data -}}
{{- $basePath := .basePath -}}
{{ range $data }}
{{ $title := .title }}
{{ $content := dict
"mediaType" "text/markdown"
"value" .description
}}
{{ $params := dict }}
{{ $path := (path.Join $basePath $title) }}
{{ $page := dict
"content" $content
"kind" "section"
"linkTitle" $title
"path" $path
"params" $params
"title" $title
"type" "docs"
}}
{{ $.AddPage $page }}
{{/* Check if children exist and recursively process them */}}
{{ if .children }}
{{ $childContext := dict "data" .children "basePath" $path }}
{{ template "createPages" $childContext }}
{{ end }}
{{ end }}
{{ end }}
{{/* Initialize and start the recursive process */}}
{{ template "createPages" (dict "data" site.Data.nested_recursive_data.children"basePath" "") }}
However I get confused when it comes to things like the final line- as I understand it - this: site.Data.nested_recursive_data.children is only storing the second level containing children- not continuing deeper.
Any help, thoughts, explantions would be super helpful here! Thanks a lot!