List RegularPages In Same Folder in Single Template

I’ve been using Hugo for a few years, building several sites already. I’m currently rebuilding an old site in Hugo witch has a deep folder structure. As part of the template, I want to have a nav block that lists the other regular pages in the same folder in the single template. It seems like this should be possible but I haven’t figured out the code from the docs and I’m not finding any examples. I think the following code should be close but doesn’t produce any links:

       {{ with .File }}
          {{ $pages := where site.RegularPages "Dir" .Dir }}
          {{ range $pages }}
            <p><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></p>
          {{ end }}
        {{ end }}

Any ideas?

I would use the page tree instead

single.html
   {{ range .Parent.RegularPages }}
      {{ if ne $ . }}<!-- skip current page -->
         <p><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></p>
      {{ end }}
   {{ end }}

I think this should work

{{ with .File }}
  {{ $section := site.GetPage .Dir }}
  {{ range $section.RegularPages }}
      <p><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></p>
  {{ end }}
{{ end }}

@irkode example works perfectly when dealing with Hugo sections.

But only the first level of directories inside the content directory are proper Hugo sections. All the sub directories are just that, from Hugos point of view.

The sub dirs are also sections according to Parent | Hugo

A section is a top-level content directory, or any content directory with an _index.md file.

And it works for

/content/section/folder/file1.md
content/section/folder/file2.md

Being on page file1 my code will return a link to file2

If section pages are not needed we might use build options to skip rendering them (would have to test that)

1 Like

Absolutely right, missed that part.

So my answer is only relevant when one does not have a _index.md file in the sub sections.

1 Like

@frio absolutely right.

the .File version would need an index.md cause we need a File.
If there’s none (if that is possible) using Page.Path | path.Dir to get the parent page may be an option… just thought out loud

the .Parent will need at least an empty --- --- _index.md in each folder


you may even suppress the rendering of the list pages using build options but than ofc you will have to generate links to the pages somewhere else :wink:

[[cascade]]
  [cascade._target]
   kind = 'section'
   path = '/folder**'
  [cascade.build]
    render = 'never'
[[cascade]]
  [cascade._target]
   kind = 'page'
   path = '/folder**'
  [cascade.build]
    render = 'always'

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.