How to range over the keys of a data file object in order?

E.g.,

content.yml

Getting Started:
- starting a new project
Strings:
- capitalizing a string
- concatenating strings

I’d like to create headings in the given order, "Getting Started", "Strings", etc.

Thanks in advance.

The Go map (where these sections gets loaded into) have no consistent ordering, so the Go template framework sort them when you iterate over them. This is unfortunate, but they probably had a good reason for this design choice (I suspect speed).

I think you have 2 options:

  1. Put Getting started/Strings into a slice.
  2. Make them into a object with a ordinal attribute or similar that you apply Hugo’s sort function to.
1 Like

I found a workaround which I think is what you’re saying as #1. I generate all the data file from a program. So I’m now generating a simple list of the keys in desired order in one data file, the two-level map in another data file, etc.

It’s very de-normalized, but it doesn’t matter because it’s read-only and my content generator is the official source of truth.

1 Like