Getting "values" of a dict

Assuming I have a dict that is created somehow dynamically and then sorted and processed, is there a way to get all values of the dict or will I always have to range though the dict to get the items in a slice?

Sample dict:

{{ $something := dict
    "key1" "value1"
    "key2" "value2"

sample result:

{{ $values := "value1" "value2" }}

I know I could do a simple range, but is there a command to do this quickly?

{{ $values := slice }}
{{ range $key, $value := $something }}
  {{ $values := $values | append $value }}
{{ end }}

The specific use case is a method that collects pages that are in several sections where each section has a different way to sort it’s pages.

Currently, yes.

In the future, possibly not.

The Go maps.Keys and maps.Values functions are still experimental. When they are no longer experimental, I think it would make sense to create matching template functions in Hugo.


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