How to render all sections

Hi, how would you show a list of links to all sections? I understand they should be available in .Site.Sections.

I have the following which seems to iterate the right array but I couldn’t find any examples of what the title and permalink variables are.

<ul>
  {{ range .Site.Sections }}
    <li>no idea what to put here but I'd like the title of the section with a link.</li>
  {{ end }}
</ul>

.Site.Sections is just a map[string]Pages.

The key in the map is URL root of the section, so you can do something like:

<ul>
  {{ range $section, $pages := .Site.Sections }}
    <li><a href="/{{ $section }}">{{ title $section }}</a></li>
  {{ end }}
</ul>

The above is far from perfect. There are other threads in here that discusses this topic.

2 Likes

Thank you. I did search a lot without finding what I need. But yours work, thank you.

I have a content/about.md that appears empty using your snippet. Any ideas how to only include real sections?

Not sure what “empty” means, but you should be able to put a conditional in the loop, something like:

<ul>
  {{ range $section, $pages := .Site.Sections }}
    {{ if ne $section "" }}
      <li><a href="/{{ $section }}">{{ title $section }}</a></li>
    {{ end }}
  {{ end }}
</ul>

By empty I meant it renders the row but $section is empty. Your fix works, just getting used to get Go syntax :slight_smile: