Is there a way to iterate through folders and display them?

I have an articles folder containing directories to articles.
Is there a way to iterate over these folders and display their names?

{{ range readDir "/content/articles" }}
  {{ .Name }}
{{ end }}

The path can be either /content/articles or content/articles; the leading / is irrelevant, as the path is relative to the project root.

References:

1 Like

Thanks! Is there a way to include links to the articles? There is an index.md file per article folder.

I think I completely misunderstood your question.

Do you want a list page, showing all articles, grouped by the folder in which they reside?

If your file structure is like this:

content/
└── articles
    β”œβ”€β”€ article-1
    β”‚   └── index.md
    β”œβ”€β”€ article-2
    β”‚   └── index.md
    └── article-3
        └── index.md

Create layouts/articles/list.html:

{{ define "main" }}
  {{ .Content }}
  {{ range .RegularPages }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
    <p>{{ .Summary }}</p>
    {{ if .Truncated }}
      <p><a href="{{ .RelPermalink }}">Continue reading...</a></p>
    {{ end }}
  {{ end }}
{{ end }}

This assumes you have layouts/_default/baseof.html with a β€œmain” block.

2 Likes

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