# How to list sections?

**URL:** <https://discourse.gohugo.io/t/how-to-list-sections/1606>\
**Category:** support\
**Created:** [August 23, 2015, 6:41pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606 "2015-08-23T18:41:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vanellope](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@Vanellope](https://discourse.gohugo.io/u/Vanellope)\
**Post date:** [August 23, 2015, 6:41pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/1 "2015-08-23T18:41:21Z")

</div>

Hello, I’m trying to make a list of sections that are available for users to navigate into. My thinking is to treat them like categories. I tried this code:

```
{{ range .Site.Sections }}
  <li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}

```

This was the resulting output:

```
<li><a href="/[%7b0%20%7d]">[{0 }]</a></li>

```

How do I get the name of the section for output?

---

<div class="post-metadata">

**Author:** ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)\
**Post date:** [August 24, 2015, 12:52pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/2 "2015-08-24T12:52:53Z")

</div>

I think a better approach is to use the [variables][2] like `.Permalink` to make it explicit what you’re trying to do instead of the `.`.

```
{{ range .Site.Sections }}
  <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}

```

> [@Vanellope](#):
>
> How do I get the name of the section for output?

With `.Type` I thought (see variables page).  
[1]: [Functions | Hugo](http://gohugo.io/templates/functions/)  
[2]: [Variables | Hugo](http://gohugo.io/templates/variables/)

---

<div class="post-metadata">

**Author:** ![Vanellope](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@Vanellope](https://discourse.gohugo.io/u/Vanellope)\
**Post date:** [August 25, 2015, 3:20am UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/3 "2015-08-25T03:20:36Z")

</div>

Jura - I tried that but it didn’t work. I get the following errors:

> ERROR: 2015/08/24 template: theme/partials/footer.html:16:27: executing “theme/partials/footer.html” at \<.Permalink\>: can’t evaluate field Permalink in type hugolib.WeightedPages in theme/partials/footer.html

> ERROR: 2015/08/24 template: theme/partials/footer.html:16:27: executing “theme/partials/footer.html” at \<.Type\>: can’t evaluate field Type in type hugolib.WeightedPages in theme/partials/footer.html

Just to make sure we are on the same page, I am trying to list out all sub directories of `/content`. Not all sections that a page is a part of.

---

<div class="post-metadata">

**Author:** ![Vanellope](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@Vanellope](https://discourse.gohugo.io/u/Vanellope)\
**Post date:** [August 31, 2015, 5:41am UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/4 "2015-08-31T05:41:38Z")

</div>

I’ve figured it out. I needed to turn on “SectionPagesMenu”. I did not expect to find the answer there.

---

<div class="post-metadata">

**Author:** ![digitalcraftsman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/digitalcraftsman/32/4142_2.png) [@digitalcraftsman](https://discourse.gohugo.io/u/digitalcraftsman)\
**Post date:** [September 16, 2015, 12:17pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/5 "2015-09-16T12:17:01Z")

</div>

I tried the same but the docs regarding `.Site.Sections` are a bit confusing. The variable is defined as:

> .Site.Sections Top level directories of the site.

So, after knowing this I wanted to print them out:

```auto
{{ range .Site.Sections }}
    <a href="#">{{ . }}</a>
{{ end }}

```

And my used dummy folder structure:

```auto
content
├── about
│ ├── foobar.md
│ ├── privacy.md
│ └── showcase.md
└── api
    └── features.md

```

The final output is disapointing:

```auto
[{0 } {0 } {0 }] 
[{0 }] 

```

Instead of showing the section’s name I get an array with weird items for each sections whose length matches the number of files in that section.

Can somebody spot my error?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [September 16, 2015, 1:05pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/6 "2015-09-16T13:05:33Z")

</div>

> [@digitalcraftsman](#):
>
> The final output is disapointing:

Disappointing?

The section list is of type Taxonomy. You don’t have to guess – this is all documented:

[http://godoc.org/github.com/spf13/hugo/hugolib#Taxonomy](http://godoc.org/github.com/spf13/hugo/hugolib#Taxonomy)

---

<div class="post-metadata">

**Author:** ![digitalcraftsman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/digitalcraftsman/32/4142_2.png) [@digitalcraftsman](https://discourse.gohugo.io/u/digitalcraftsman)\
**Post date:** [September 16, 2015, 2:05pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/7 "2015-09-16T14:05:37Z")

</div>

Is there a reason why this variable has the type `Taxonomy`? I would interpret the documentation’s definition of `.Site.Sections`

> .Site.Sections Top level directories of the site.

as all direct sub-directories of `content`, e.g. in `content/post/` the section would be `post`. And that’s also the way how you [explained them](http://discuss.gohugo.io/t/type-vs-sections-whats-the-difference/178/4?u=digitalcraftsman):

> Section is the first folder under content (content/blog =\> blog)

* * *

However, I found a slightly hacky workaround in order to list all sections and the files that are stored in a section. Here’s my script if somebody finds it useful:

```html
<!-- Find all top-level subfolders in content (e.g. content/post/) -->
{{ range .Site.Menus.main }}
    <!-- Get the section name -->
    {{ $section := index (split .URL "/") 1 }}
    <!-- If the section isn't found yet add him with a delimiter-->
    {{ if (not (in (split ($.Scratch.Get "sections") "|" ) $section)) }}
        {{ $.Scratch.Add "sections" $section }}
        {{ $.Scratch.Add "sections" "|" }}
    {{ end }}
{{ end }}
<ul>
    {{ $allSections := (split ($.Scratch.Get "sections") "|") }}
    <!-- Now $allSections contains all sections. But the splitting caused that the last index is an empty string (or a non-valid section). Let's ignore this index by correcting the number of indexes. -->
    {{ $numOfSections := sub (len $allSections) 1 }}
    {{ range first $numOfSections $allSections }}
        <li> <b>Section "{{ . }}" contais:</b>
            <!-- Find all pages in the current section -->
            {{ range where $.Site.Pages "Section" . }}
                <li>{{ .Title }}</li>
            {{ end }}
        </li>
    {{ end }}
</ul>

```

**This only works if the sections don’t contain nested subfolders!**

```
/content/section/foobar.md # is valid
/content/section/subsection/foobar.md # is NOT valid

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [September 16, 2015, 2:16pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/8 "2015-09-16T14:16:22Z")

</div>

> [@digitalcraftsman](#):
>
> Is there a reason why this variable has the type Taxonomy?

Because a section kind of is a special kind of taxonomy? Why does it matter what the name of the type is?

---

<div class="post-metadata">

**Author:** ![digitalcraftsman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/digitalcraftsman/32/4142_2.png) [@digitalcraftsman](https://discourse.gohugo.io/u/digitalcraftsman)\
**Post date:** [September 16, 2015, 2:24pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/9 "2015-09-16T14:24:57Z")

</div>

It just confused me what `.Site.Sections` returned and what I falsely expected. But you already explained it in the first sentence why the type is taxonomy.

* * *

**Update:**

What actually confused me was that I didn’t know the return values of `range`. Instead of using the terrible workaround from above you can [achieve the same more elegantly](http://discuss.gohugo.io/t/adding-weights-to-sections-to-order-on-the-homepage/856/3?u=digitalcraftsman):

```auto
{{ range $section, $taxonomy := .Site.Sections }}
    <h4>{{ replace $section "-" " " }}</h4>
    <ul>
        {{ range $taxonomy.Pages }}
        <li><a href="{{ .Permalink}}"> {{ .LinkTitle }} </a> </li>
        {{ end }}
    </ul>
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![John\_Hashim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/john_hashim/32/7791_2.png) [@John\_Hashim](https://discourse.gohugo.io/u/John_Hashim)\
**Post date:** [April 16, 2019, 9:26pm UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/11 "2019-04-16T21:26:53Z")

</div>

im stack on this i want to show subsections

---

<div class="post-metadata">

**Author:** ![darshanbaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/darshanbaral/32/7601_2.png) [@darshanbaral](https://discourse.gohugo.io/u/darshanbaral)\
**Post date:** [May 14, 2019, 4:12am UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/12 "2019-05-14T04:12:08Z")

</div>

@digitalcraftsman, when I do the following, I get nothing. The part inside `<ul>...</ul>` works fine. What am I missing?

> [@digitalcraftsman](#):
>
> #### {{ replace $section "-" " " }}

---

<div class="post-metadata">

**Author:** ![digitalcraftsman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/digitalcraftsman/32/4142_2.png) [@digitalcraftsman](https://discourse.gohugo.io/u/digitalcraftsman)\
**Post date:** [May 14, 2019, 10:00am UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/13 "2019-05-14T10:00:31Z")

</div>

Hello @darshanbaral,

honestly, I don’t know why I used `$section` at all in the code example above. This was 4 years ago when I started using Hugo.

Instead you can use `<h4>{{ $taxonomy.Title }}</h4>`. This replace function, I guess, is invoked to avoid dashes that are commonly used in folder names.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [August 25, 2019, 9:45am UTC](https://discourse.gohugo.io/t/how-to-list-sections/1606/14 "2019-08-25T09:45:42Z")

</div>

A post was split to a new topic: [How to list sections](https://discourse.gohugo.io/t/how-to-list-sections/20414)
