List all pages in sub folder

Hi,

I have the following structure of content:

- content
-- news
--- news-1.md
--- news-2.md
-- blog
--- tips
---- tip-1.md
---- tip-2.md
--- tutorials
...

I don’t have neither index.md, nor _index.md in tips subfolder because I don’t want page https://example.com/blog/tips to be created.
How can I get list of pages inside blog/tips directory to output it somewhere on the site as a list of links?

{{ range where (where .Site.RegularPages "Type" "blog") "File.Dir" "blog/tips/" }}

See:

2 Likes

Thanks for your reply!

Is it possible to get list of folders (tips, tutorials …) to build an ordered list of links?
I could do it by using _index.md with order parameter for each section (tips, tutorials …), but as I mentioned I don’t want the pages like https://example.com/blog/tips to be created.

Maybe there is a more elegant solution to this requirement?

1 Like
{{ range (readDir "content/blog/") }}
  {{ if .IsDir }}
    <p>{{ .Name | title }}</p>
    <ul>
    {{ $path := printf "blog/%s/" .Name }}
    {{ range where (where $.Site.RegularPages "Type" "blog") "File.Dir" $path }}
        <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
    {{ end }}
    </ul>
  {{ end }}
{{ end }}

But I’m also beginning to question your content structure.

First, keep in mind that the structure of your site, from the perspective of visitors, does not need to match the structure of your content directory. See permalinks.

Second, the first level directory under the content directory determines a content’s type. In my view:

  • A tip is a “type” of content
  • A tutorial is a “type” of content
  • A news article is a “type” of content

So you could do this:

content
├── news
│   ├── news-1.md
│   └── news-2.md
├── tips
│   ├── tip-1.md
│   └── tip-2.md
└── tutorials
    ├── tutorial-1.md
    └── tutorial-2.md

with config.toml

[permalinks]
tips = "/blog/tips/:title/"
tutorials = "/blog/tutorials/:title/"

You might also consider using Hugo’s taxonomy system to categorize your blogs posts as either tips or tutorials.

Just some alternatives to consider…

7 Likes

A post was split to a new topic: Directory listing by weight