Content Organization: Building a List Template that handles Multi-level subdirectories


#1

Hi, I am currently creating a new site with Hugo. To reduce cost and simplify my online presence I have decided to combine my professional, activism and hobby content into one site to be hosting on git hub and use netlify as the front end.

Currently I am trying to get Hugo to loop through a list of pages inside a few subdirectories inside of a sub directory on the root. The reason I need multi-leveled sub-directories is to keep my profession, activism and hobbit categories organized so I am sticking with the subdirectories approach incase I need to move a particular section. That way I only have to move the directory for the particular catagory, and also I dont like clutter. If someone has a better way of doing this based on what I want to accomplish, im all ears. The layout is included in the image attached. So far I have been unable to find a way to create a list template that will loop though a set of sub-directories and create a section page of from content in each subfolder for each subcatagory in my profession, activism and hobbies.

For instance in the image I have a directory what’s called “profession” in that directory i have a subdirectory called “services” and I would like to create a page of content for /profession/services from the subdirectories “computer”, “content”, “data”, and “information” I would like to do the same with the “activism” and “hobbies” directories as well.

I have read that Hugo’s list templates dont really recognize subdirecties when it comes to populating content. Right now when I vist my site “/profession/services” it renders a blank page. I am assuming that this is happening because there is not any markdown files in the service directory for the list template to render.

Is there a way to tell the template to recurse though subdirectories inside of a subdirectory that is attached to the content root and build a page inside of the 3nd level subdirectory based on what’s in the 4rd level subdirectories?

My development site is at: https://github.com/scottrlarson/academic-kickstart

Edit: I removed paragraphs that were confusing and re-evaluated my wording based on what maiki said.


#2

You posted so much, it is a lot to hold in my head. I am going to link to some pages in the docs which refer to my points, and hopefully some of it will apply to you. :slight_smile:

Here is my first suggestion: order your content however you want. Then figure out how to show the data in the way you want. You can figure out which template is looked up with the lookup order.

So your services directory will use the section lookup.

I think what you may have read is that one cannot set a specific template for a sub-section. Meaning all the directories in your content directory are sections, and directories inside those are sub-sections. It will happen one day, but currently those sub-sections use the same templates as the section where it lives.

So that means you need to figure out which templates you want a given section to use. I’d start by making a generic template for all section list pages, and then modifying them as you want.

Quoting this part again:

To clarify, _index.md pages are used to add custom content and front matter to a section. They are not required, so if I were you, I’d forget about them right now and focus on getting the templates to show as you expect.

Sorry this isn’t more useful, I just can’t quite parse what your issue is from the description. Maybe we could focus on a single template, get it working, and then ask the next question. Does that make sense? Which template is showing blank that you’d like to show content? :slight_smile:


#3

No that is very usefull thank you. I just found this right after I made this post, lol. Last night I was pulling my hair out and must have some how missed it. Thank you for the response and clarification. It revealed many things I have been confused on the last couple of days.

Sorry for the confusion about the _index.md file. I meant the list template html files in the layout section.

the /profession/services currently is showing blank, but I think based on your input I understand what’s going on enough to figure this out…


#4

Awesome! The good news is, I think what you are going for is possible, once you grok the templates and stuff. Good luck (and of course come back with yer questions)! :slight_smile:


#5

Just to clarify before I start this, If you look at my diagram you will notice I dont have any content that needs to be displayed on the root of content or any 1st level subdirectories as of yet, but I have 2nd level subdirectories, 3rd and 4th level subdiretories. I want to be able to display content on the 3rd level subdirectories using a section template from the content inside of the 4th level subdirectories.

So when i visit /profession/services I would like Hugo to build a page from the content in the 4th level subdirectories.

The question I have is: will the content I want to display from the 4th level diretories be able to be listed on the 3rd level from the default template when the page gets built?

Right now when I navigate to /profession/services a blank page with a title “Services” is displayed. The contents of the 4th level subdirectories are not listed on the page

It seems like Hugo only recognizes content on 2nd and 3rd max… am I incorrect in this assumption? I could find no documentation the limits of multi-level content creation for Hugo.

By the way I revised my initial post to make my initial question more clear.


#6

I don’t have first-hand experience with that many levels, so I can’t speak it. However, you definitely want to read the content sections doc, if you haven’t already:

The important part to understand is, that to make the section tree fully navigational, at least the lower-most section needs a content file. (e.g. _index.md).

To establish those sub-sections you need that _index.md, which is counter to what I said above, my bad. I wasn’t thinking about sub-sub-sub-sections. :slight_smile:

That doc lists examples, though, including breadcrumbs and variables to use in your templates!


#7

So I read the the content sections doc, I have a _index.md file at my lower level directory, but I think this document is talking about making a menu tree or something…

What I want to do is list the content from the 4th level directories on the 3rd level with a list template if that is possible. Do list templates allow for building content that are in subdirectories on each level?

Right now nothing is displayed but the title of the section itself on the 3rd level. One of the 4th level directories, “computer” has a _index.md file and a hours.pricing.md file. The hours-pricing.md file I would like to display on the 3rd level “services” using a list template. Eventually I would like to use the the 4th level directory names as headings for each section containing the files I would like to display using a list template if that possible

My site root is located on github in case you want to verify the directory structure: https://github.com/scottrlarson/academic-kickstart/tree/master/content


#8

Hmm it works when I take the _index.md file out of the bottom-level folder… Alright I think I figured this out.

Thank you for your help.


#9

Im still having problems with certain _index.md files generating top level menus for sub-sub content directories. I have _index.md files in my content section directories and in the sub-directories of the content section directories. but it does not generate links on pages that get generated from _index.md pages on when you visit the top level content section directories. Am I do something wrong?

I have a content section directory called: Services.
below that I have 4 sub directories named for the sub services each containing a _index.md file along with the specific named service.md files. if I navigate to the sub directories of the services folder the list template generates the content from within each of the sub folders. Is there a way to generate a site map for the top level content section page and everything below it?
I looked at the documentation on Content Sections Templates but I am a little confused on how it works. Do I just need to create a section template for that section for the top level content section for a page to render containing a site map of links below that location?


#10

What code are you using for range? As I understand, {{ range .Data.Pages }} does not include in the list _index.md.

{{ range where .Site.Pages "Section" "services" }}

include in the list _index.md, but also include all pages from subsections. As I understand it, there is no easy way to create a common list for articles and folders of the same level.


#11
{{ $nodes := where (where .Site.Pages "Section" "services") "Params.inlist" true }}
{{ $all := union $nodes .Pages  }}
{{ range  $all }}
    {{ .Render "li" }}
{{ end }}

In order for this code to work in every file _index.md inside the section, you need to add a line inlist: true. This will only work for two levels of the folder tree, if the levels are greater then the task is even more difficult.

upd: Also work

{{ $nodes := where (where .Site.Pages "Section" "services") "IsNode" true }}

#12

Thank you look at this and see how it works. I have moved into something more important and will come back to this later.


#13

Please test this code, it seems to me that it works without additional parameters in front matter.

{{ $link := .RelPermalink }}
{{ $all := where (where .Site.Pages "Section" "services") "Parent.RelPermalink" "eq" $link }}
{{ range $all }}
     {{ .Render "li" }}
{{ end }}

#14

Thank you for helping me. Should this work in the list template for services?
I created a list template for services and added that code but nothing displays.
What is the best way to get this code to render, there seems like there are multiple ways.