Study Guide: How to use Range, Where and Sort in Hugo

Various combinations of using range, where, first, sort, uniq, union, intersect, etc. in Hugo and Go Templates. Many are based on the Hugo Docs, but collected here in one place for easier access.

Syntax


Sort in Ascending or Descending Order

{{ sort site.Params.grades }} <!-- "value" "asc"  is optional for ascending -->
{{ sort site.Params.grades "value" "asc" }}
{{ sort site.Params.grades "value" "desc" }}

Sort a Map in Ascending or Descending Order

{{ range sort site.Params.authors "firstname" }}
  {{ .firstName }}
{{ end }}

{{ range sort site.Params.authors "firstname" "asc" }}
  {{ .firstName }}
{{ end }}

{{ range sort site.Params.authors "firstname" "desc" }}
  {{ .firstName }}
{{ end }}

Range with Where

{{ range where .Data.Pages "Section" "post" }}
	{{ .Content }}
{{ end }}

Range and Where with Intersect

{{ range where .Site.Pages ".Params.tags" "intersect" .Params.tags }}
{{ if ne .Permalink $.Permalink }}
	{{ .Render "summary" }}
{{ end }}
{{ end }}

Create collection of Pages using Variables, Type, Union, and Intersect

  1. Find regular pages excluding .Type “page” or “about”,
  2. unless they are pinned,
  3. excluding all pages with no images set in the Page parameter “images”.
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}

Range, Where and Intersect with Variables

{{ $var1 := where .Site.Pages "Params.a" "var1" }}
{{ $var2 := where .Site.Pages "Params.b" "var2" }}
{{ $filtered := $var1 | intersect $var2 }}
{{ range $filtered }}
{{ end }}

Range through data with Where and Sort

  1. range through data files and grab the first 5,
  2. where the data directory matches [directory], and
  3. sort by the data field [date] in descending order.
{{ range first 5 ((where .Site.Data.[directory] "[data_field]" "eq" .Params.[name]) sort .Site.Data.[directory] "date" "desc") }}

Range with Multiple Where Clauses

  1. Find all pages where the section name matches [section_name].
  2. In that set, find all pages where the param [param_name] equals “true”.
{{ range where (where .Data.Pages "Section" "[section_name]" ) ".Params.param_name" "eq" "true" }}

Range Through a Map, Filtering on a Field, and Removing Duplicates

  1. range through the map $result,
  2. collect only unique items (eliminate duplicates).
  3. find the first 10,
  4. where the description field is not missing or empty.
{{ range first 10 (where ($result | uniq) ".description" "gt" "") -}}  

  1. range through all RegularPages (real content pages, not .Section, _index.md, etc.),
  2. where the param topics matches the current page’s param,
  3. but don’t include the current page.
{{ $posts := where (where .Site.RegularPages ".Params.topics" "intersect" .Params.topics) "Permalink" "!=" .Permalink -}}
{{- with $posts -}}
    <hr>
    <div class="no-print">
        <h4>Related Content</h4>
        <ul>
            {{ range first 8 . -}}
                <li class="relatedPost"> <a href="{{ .RelPermalink }}">{{ .Title | title }}</a></li>
            {{- end }}
        </ul>
    </div>
{{- end }}


Related Content

Source: //study/hugo/hugo-range-where-and-sort/