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 }}
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.
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:
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:
<!-- 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
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:
{{ range $section, $taxonomy := .Site.Sections }}
<h4>{{ replace $section "-" " " }}</h4>
<ul>
{{ range $taxonomy.Pages }}
<li><a href="{{ .Permalink}}"> {{ .LinkTitle }} </a> </li>
{{ end }}
</ul>
{{ end }}