I am enjoying using Hugo. It’s pretty solid and I love the freedom we get to organise the content. I am pretty new to it and despite having read the documentation, I might have misunderstood some concepts.
Here is a sample of the data structure of my website:
We have videogame systems. Each System contains Games and Images. Each Game contains Ratings, Reviews and Images.
Each Game, Rating and Review entry has a type, respectively type: game, type: rating and type: review.
What I managed to do:
display a System thanks to Section Template
fetch Games from a System page {{ $Games := (index (where .Sections "Title" "Games") 0).Sections }}
fetch Ratings and Reviews from a Game page {{ $RatingsAndReview := union (where .Pages "Type" "review") (where .Pages "Type" "rating") }}
What I’m missing:
I can’t get to give the Game section its own template. Their section is always systems. In spite of having a type, the top section template is always in use. (I’ve tried layouts/games/list.html, ``layouts/system/games/list.htmlandlayouts/game/single.html`)
When I rename a game from _index.md to index.md, I get a Type template (layouts/game/single.html) but then .Data.Pages is empty. .Sections as well. I did not find a way to populate $RatingsAndReview.
I am not sure if I have architectured the content well or if I am missing something with the behaviour of the Nested Sections. Let me know if you have pointers. I an happy to try out and investigate further.
I kept digging a bit and found a few interesting things.
Adding empty _index.md seems to be the way to go to make sections explicit. I added one in the folder games and was able to fetch games easily this way. I don’t know if that’s what I was supposed to do.
While using {{ printf "%#v" . }} to debug templates, I noticed the unexported key targetPathDescriptor contains a Sections dict. It is an ordered list of all the sections leading to the .CurrentSection (Sections:[]string{"systems", "3do", "games", "alone-in-the-dark"} when I am viewing a game for example).
And .CurrentSection (courtesy of https://github.com/gohugoio/hugo/pull/3671)! It’s great and I wonder if that should be part of the lookup algorithm for templating as well:
Reading Different templates for nested sections?, it seems there is no way to assign a specific layout for a nested section, wether layout and/or type are set in front-matter.
Using content/systems/3do/games/alone-in-the-dark/index.md with type: game enables me to use layouts/game/single.html as a template. I’m unsure if it is the right way to go though as it makes fetching sub pages more complicated.
I suspect a Single Page is considered to be a leaf in a content tree. Is that why I can’t list the reviews and ratings in this tree? (it is alright if index.md is named _index.md).
Thanks @bep I documented my thinking along the way, hence the confusion.
I think I have been wondering wether to declare stuff as pages to benefit from custom layout (upside: custom layout, downside: struggle to get children contents) or to declare content as sections (upside: content navigation is , downside: no custom layout).
It seems a Type attribute cannot be assigned to a section. Neither a Layout.
I’ll probably end up using nested sections. I will determine which partial/template to use to render based on a content Type.
No problem Once I have managed to migrate my content to Hugo, I’m happy to share the code and content hierarchy. Especially if it can fuel your thinking in how to handle nested sections.
I finalised the migration of my website: https://emunova.net/
It is now entirely built with Hugo.
I preprocess a few things to compute average values and ratings breakdowns. I store that in the data directory in yaml files, the keys matching the .Dir value of the sections they belong to.
Hugo spends most of its time on the _default/section.html. This is where is decided which template will be used depending on the section/content type: