Loop through content in current section only

I’m sure this probably been asked a thousand times but I can’t find a solution for it. I’ve got content that is 3 levels deep in the content folder and I’m trying to loop over the current pages in its .Section but it appears to always return blank. All folders have _index.md files which I believe makes them a section:

content
├── _index.md
├── contact.md
├── docs
│   ├── _index.md
│   ├── general.md
│   └── v1
│       ├── _index.md
│       ├── examples
│       │   ├── _index.md
│       │   └── word.md
│       └── endpoints
│           ├── _index.md
│           └── geo.md
├── privacy.md
└── tos.md

I’ve got the current little snippet:

<div class="tabs">
  <ul>
    {{ range where .Site.Pages "Section" .CurrentSection }}
    <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
    {{ end }}
  </ul>
</div>

But it always returns blank without anything inside the list. If I remove the .CurrentSection filter (range .Site.Pages "Section") it works but returns everything from under the docs portion of the file tree.

I seem to recall this being the intended functionality but I can’t find anything in the docs pointing to this being true.

Basically, I’m wanting it to return everything at its current level in the tree. At the bottom level it’d return everything under the endpoints level, in the examples folder it’d return everything in there etc.

So, .Section will always be the name of the root section (“docs”) – this is monstly for historical reasons.

But there are many examples in the wild talking about building navigation from the section tree, e.g. https://github.com/bep/hugotest/blob/master/layouts/partials/sections-tree.html

This ended up doing exactly what I wanted!

<div class="tabs">
  <ul>
    {{ $curPage := .UniqueID }}
    {{ with .Parent }}
    <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
    {{ end }}
    {{ range .CurrentSection.Pages }}
    <li{{ if eq $curPage .UniqueID }} class="is-active"{{ end }} ><a href="{{ .Permalink }}">{{ .Title }}</a></li>
    {{ end }}
  </ul>
</div>
2 Likes

Note that pages are comparable, so you can also do:

{{ if eq $curPage $}}
``
1 Like