Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to List Pages in Current Section

Tags:

hugo

I'm trying to list the pages in the current url section.

  • Get All Sections
  • Limit range to current Section
  • List pages in current Section

{{ range $value := .Site.Sections }}

    {{ range .Section }}

        {{ range $value.Pages }}
            <ul>
                <li>{{ .Title }}</li>
            </ul>
        {{ end }}

    {{ end }}

{{ end }}

Though it returns null because {{ range .Section }} is not valid code.

What is the correct way to do this?

https://gohugo.io/templates/variables/

like image 587
Matt McManis Avatar asked Mar 17 '17 22:03

Matt McManis


1 Answers

You need to filter .Site.Pages by section using the where function. Try this:

<ul>
{{ range where .Site.Pages "Section" .Section }}
  <li>{{ .Title }}</li>
{{ end }}
</ul>

If you want to avoid empty lists, you can store the slice of section pages in a variable and check its length before you output the ul tags.

{{ $sectionPages := where .Site.Pages "Section" .Section }}
{{ if ge (len $sectionPages) 1 }}
  <ul>
    {{ range $sectionPages }}
      <li>{{ .Title }}</li>
    {{ end }}
  </ul>
{{ end }}
like image 118
Jack Taylor Avatar answered Dec 09 '22 23:12

Jack Taylor