Trying to order a list of page resources

So, what I’m trying to achieve is a listing page of sorts but I don’t want subpages for details to be exposed. So for now I have index.html not a listing _index.html. I like the page resources approach, it’s working great for me except for one thing. I can’t find a way to order the resources in the listing. Ideally I’d have some sort of weight ordering but .ByWeight doesn’t seem to work with resources.

{{/* layouts/section_name/single.html */}}
{{ partial "head.html" . }}
<div class="site-wrap clearfix">
  {{ partial "header.html" . }}
  <div class="post p2 p-responsive wrap" role="main">
    <div class="measure">
      <div class="post-header mb2">
        <h1 class="py2">{{ .Title }}</h1>
      </div>

      <article class="post-content">
        {{ .Content }}
      </article>

      {{ range .Resources.ByType "page" }}
        {{ .Render "listing" }}
      {{ end }}
    </div>
  </div>
</div>
{{ partial "footer.html" . }}

And

{{/* layouts/section_name/listing.html */}}
<article>
  <h3><a href="{{ .Params.params.url }}" target="_blank">{{ .Title }}</a></h2>
  {{ .Content }}
</article>

This should help you: https://gohugo.io/functions/sort/

1 Like

Hey! That looks like it’s working.

It took some fun experimentation to figure out what the things should be, so for completeness sake, here’s my solution:

  {{ range sort (.Resources.ByType "page") "Params.weight" }}
    {{ .Render "listing" }}
  {{ end }}

And an example of the frontmatter for the resources:

---
title: "Google"
date: 2018-03-31T12:25:14+11:00
draft: false
weight: 5
params:
  url: "https://google.com/"
---

Confusingly, to access params for a resource it’s actually .Params.params.foo instead of .Params.foo, hence the ability to directly access weight in the sort field.

4 Likes