How to sort a range over a headless bunldle

Given a content structure as follows:

 services
├── _index.md
└── serviceA
    └── index.md //headless:true, weight: 3
    └── resources
└── serviceB
    └── index.md //headless:true, weight: 1
    └── resources
└── serviceC
    └── index.md //headless:true, weight: 2
    └── resources

I want to create a “layouts/services/list.html” template that can range over the headless bundles where they are sorted by a param “weight”

I can get the content to display correctly with:

{{ define "main"}}

//content from _index.md
{{ .Content}}

{{ $page := . }}
{{ range (where (readDir (print "content/" .File.Dir)) ".IsDir" true ) }}
{{ with $page.GetPage .Name }}

//content from each index.md
{{ .Content}}
  
{{ end }}
{{ end }}

{{end}}

However I am not sure how to pull off the sort within the range.

Is there a better way to go about this?

Instead of using headless pages, you could try the new build options, specifically the example with listing pages without publishing them. Then sorting by weight is straightforward.

1 Like

@pointyfar looks great thanks

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