Indeterminate behaviour using range and reverse range in same partial

I can reproduce this issue quite simply using the “Quick Start” project.

Steps to reproduce:

  • Follow the steps to get a working Hugo site outlined in the Quick Start
  • Create a number of pages and add them to the main menu by adding menu: ["main"] to the front matter
  • Edit the themes/ananke/layouts/partials/site-navigation.html to add the following code directly after the matching block that renders the menu in a forwards direction. (Note the .Reverse added to the menu array)
        <ul class="pl0 mr3">
          {{ range .Site.Menus.main.Reverse }}
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="{{ .URL }}" title="{{ .Name }} page">
              {{ .Name }}
            </a>
          </li>
          {{ end }}
        </ul>

EXPECTED: the list of pages followed by the list of pages in reverse displayed consistenty on each of the pages as you click through them.

ACTUAL: Indeterminate ordering of the ranges as you click through the pages.

My guess is that the pages are being rendered in parallel and there is a range cursor that’s being trampled by different threads… or I’m missing something obvious and doing something wrong.

I’ll test it out if you wrap your test project in a repo and share it. :slight_smile:

The results prior to Reverse are not deterministic, and Reverse doesn’t cause a sort to occur [1]. You should sort prior to reversing. Try this:

{{ range .Site.Menus.main.Sort.Reverse }}
3 Likes

A post was split to a new topic: Sorting Menu by weight and reversed