Using "where" and "sort" with a two dimensional array

Not the nicest solution but you can create a partial that converts a slice to dict:

partials/slice-to-dict.html

{{ return (dict "key" (index . 0) "value" (index . 1)) }}

then apply it like a function and sort by the key:

{{ range sort (apply $arr "partial" "slice-to-dict.html" ".") "key" }}
  <p>{{ .value }}</p>
{{ end }}

Unfortunately you cannot define the partial in-line with “define”, since “template” cannot be used as a function to “apply” with nor can it return any value like partials can.

3 Likes