Directory listing by weight

[Split from https://discourse.gohugo.io/t/list-all-pages-in-sub-folder/28063]

I have one query

{{ range (readDir "content/blog/") }}
  {{ if .IsDir }}
    <p>{{ .Name | title }}</p>

In the above code we are reading a directory can we filter the directory listing by weight? Since it’s a β€œSection” and it has _index.md file.

Thank you :slight_smile:

Please provide more detail:

  • Content structure
  • Desired result

sure will do
folder structure -

|- content
  |- documentation
     |- Configuration
        |- _index.md
        |-  file.md
     |- Getting Started
        |- _index.md
        |-  fileA.md

my code -


   {{ range (readDir "content/documentation/") }}
      {{ if .IsDir }}
        <p>{{ .Name | title }}</p>
        <ul>
        {{ $path := printf "documentation/%s/" .Name }}
        {{ range where (where $.Site.RegularPages "Type" "config") "File.Dir" $path }}
        {{ $active := eq $.RelPermalink (.URL | relLangURL) }}
      {{ $active = or $active (eq $.Section (lower .Name)) }}
            <li><a {{ if $active }} class="active"{{ end }} href="{{ .RelPermalink }}">{{ .Title }}</a></li>
        {{ end }}
        </ul>
      {{ end }}
    {{ end }}

and using this code I’m trying to get all sections and inner files in a list view.

Now when I range through sections, it displays alphabetically. Can we range it by weight ??

I hope I clearly explained my query.

With this structure:

content/
β”œβ”€β”€ documentation/
β”‚   β”œβ”€β”€ configuration/
β”‚   β”‚   β”œβ”€β”€ conf-a.md
β”‚   β”‚   β”œβ”€β”€ conf-b.md
β”‚   β”‚   └── _index.md
β”‚   β”œβ”€β”€ getting-started/
β”‚   β”‚   β”œβ”€β”€ gs-a.md
β”‚   β”‚   β”œβ”€β”€ gs-b.md
β”‚   β”‚   └── _index.md
β”‚   └── _index.md
└── _index.md

I would do this:

{{ range (site.GetPage "documentation").Sections }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  <ul>
  {{ range .Pages}}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
  </ul>
{{ end }}

The default sort order is:

  1. Weight (asc)
  2. Date (desc)
  3. LinkTitle (asc)
  4. FilePath (asc)

Notes:

  1. The template code above is not recursive. It will only list the immediate children of the documentation section.
  2. When you assign weight, use positive or negative numbers, but not zero.
1 Like

works perfectly!! I think i complicated it.

Thank you so much @jmooring

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