Sort range by page weight in taxonomy range

Trying to build simple faq pages with different sections.

Build this dynamic range loop to output all ‘onderdelen’ taxonomies. And for every term in the taxonomy, I output the first 3 pages.

{{ range $name, $taxonomy := .Site.Taxonomies.onderdelen }}
      <div class="w-1/3">
        <h4 class="text-lg lg:text-2xl font-medium mb-4">{{ $name | humanize }} <span
            class="opacity-50 text-lg">({{ $taxonomy.Count }})</span></h4>
        <ul>
          {{ range $name := sort $taxonomy "Weight" | first 3 }}
          <li>
            <a href="{{ .Permalink }}"
              class="font-medium opacity-75 leading-large lg:text-lg border-b border-grey-lightest block pb-2 mb-2">
              <i class="fal fa-file mr-2"></i>
              {{ .Title }}</a>
          </li>
          {{ end }}
        </ul>
      </div>
      {{ end }}

This is working, but I can’t find a way to sort the first 3 pages by their weight.

{{ range $name := sort $taxonomy "Weight" | first 3 }}

This is untested as I don’t know your full code, but try

{{ range $taxonomy.ByWeight }}

Resulting in a build error.

ERROR 2019/05/02 15:39:26 Failed to render pages: render of "section" failed: ".... execute of template failed: template: ondersteuning\list.html:74:42: executing "main" at <$taxonomy.ByWeight>: can't evaluate field ByWeight in type hugolib.WeightedPages

Share your code, and one of use will be able to help you more

I’m guessing $taxonomy in your case is a list of WeightedPages. Try

$taxonomy.Pages.ByWeight

Please note that with your current template, if a single page has multiple “onderdelen”, it will appear multiple times (in separate <ul>).

Next time you can just see the type of your variable by printing it out in the template ({{ $taxonomy }}), which in your case will probably yield [WeightedPage(0,"Title"), ...], and searching for it in the docs. You will probably find answers for these kinds of problems a lot faster

1 Like