Getting a subsection?

I have seen Hugo supports {{ .Section }} which in my case returns something like posts in /content/posts. I want to get highlights from /content/posts/highlights. Is there an equivalent (what is the correct name for {{.}}?) for this?

One way would be to use .GetPage

{{ $highlights := site.GetPage "/posts/highlights" }}
{{ with $highlights }}
  {{ range .Pages }}
    ...
  {{ end }}
{{ end }}

Another way would be to use Front Matter Cascade in the Nested Section’s _index.md so that a parameter is inherited by all children pages, e.g.:

[[cascade]]
highlights = true

And then to render the list use:

{{ range where site.RegularPages "Params.highlights" "==" true }}
  ... 
{{ end }}
1 Like

I was thinking there was a short form of getting a nested section like {{.}} but it seems there is not. I wanted to change a category and use the subsection name as the category rather than bulk change articles. thanks anyway.

I’m sorry I didn’t understand your question.

The variable that holds Nested Sections below a Section is called .Sections.

Here is an example for creating a dynamic list of the various Nested Sections under a Section:

{{ with site.GetPage (printf "/%s" .Section) }} 
{{ $sections := .Sections }}
{{ range where $sections }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}
{{end}}

Ref: Creating a menu for all Nested Sections within a Section

I suppose that within the above list you could enter whatever condition you need.

1 Like

I usually show the category for every post in the terms list in a partial as follows <a href="/category/{{ .Params.category}}">{{ .Params.category }}</a> (every list page has 12 entries and each entry has the category shown below it). So, I was thinking instead to use the folder name which is the same as the category name nested inside content/posts. Like the root section has {{ .Section }} I was thinking there is a short form for a nested section which would allow me to get the title of the section to display as above.

For example, {{ .Section }} is returning “posts” as shown here. I want a similar ‘variable’ to return a folder inside posts instead, such as ‘highlights’ instead. An example would be ‘funny-cats’ here.

This returned an error failed: template: partials/category.html:3:9: executing "partials/category.html" at <where>: wrong number of args for where: want at least 2 got 1

The above snippet used to work but apparently it no longer does.
Things change in Hugo all the time and this is not my job.

In any case .Sections is what you need to use.

What does the following give you?

{{ range .Sections }}
<a href="{{ .Permalink }}">{{ .Title }}">
{{ end} }

EDIT
There is really no need posting external links to screenshots. If you want to show something then either share the project or share a project with dummy content that reproduces the setup.
Also see: Requesting Help

didn’t work either and returned blank. Repo is here https://github.com/askel45/test

As far as I know you need to include an _index.md to activate a Nested Section in Hugo.

Otherwise these pages will not be accessible.

So try the above and then try the above snippets again and let me know.

1 Like

I have one inside content/posts. Do you mean I put it inside content/posts/highlights too? If so, still doesn’t work. (In Jekyll, I used to have these nested folders as categories even without specifying in the frontmatter. I want the same with Hugo and how to link to them and use their names as the title).

Yes, you do need to have an _index.md in each subdirectory.

Push the latest changes and I will have another look. Make sure to include the Nested Sections snippet that does not work for you.

From the sound of the above it seems that perhaps it would be best to assign Taxonomies to your content files.

done

I have one site using the same style and format as the test site but has no category in the front matter. I have defined a category taxonomy, do you mean I add the category in the frontmatter instead?

Yes. First you configure the category taxonomy in the project config and then you assign it in the various content files. The Doc offers examples.

Ok. Taxonomies are separate than Sections in Hugo.
Taxonomies have no hierarchy.
You cannot render the Nested Sections from within a Taxonomy template.

The template that controls Sections and Nested Sections in your repo is /layouts/_default/section.html

Please have a better look at the Hugo documentation.

It is not working for index.html too.

I don’t use that coz file and just use it as a placeholder to avoid build errors.

So I think Hugo does not have this feature at all and is dependent on front matter variables?

To fetch the Nested Sections on the Homepage you first need to fetch their Parent Section and then within that context you may render the Nested Sections.

The simplest way for that would be to get the Parent Section with .GetPage

I gave you an informed reply about Hugo’s architecture.
The Section Lookup Order is quite specific and there is no way around it.

You need to assign a Taxonomy in a content file’s front matter.
You can semi-automate it by using Archetypes.

Taxonomies and Sections are different things. They do not mix. They may overlap but you cannot expect to place the content under a Section that bears a Taxonomy’s name and then render Nested Sections from within the Taxonomy template.

It doesn’t work like that.

I wish I knew this beforehand. I find the docs are bit difficult to understand when it comes to some things plus the absence of working examples. Let me decide if for this one site if it is worth adding the category to frontmatter or just stick with Jekyll. Cheers!

Another option you have is to use Front Matter Cascade

So that you can assign a category in the front matter of an _index.md and then all content files below a Nested Section will inherit it.

You can list all pages within each subsection by adding the _index.md pages as noted above, and ranging with .CurrentSection. This lists all pages in the currently viewed subsection.

{{- range .CurrentSection.Pages -}}
{{ .Title | humanize | title }}
{{ end }}