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.


Thanks! Is there a way to include links to the articles? There is an 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:

└── articles
    ├── article-1
    │   └──
    ├── article-2
    │   └──
    └── article-3

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.

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