Dynamically group pages by front matter

Consider the following template code:

    {{ $downloads := where .Site.Pages ".Params.type" "download" }}

    {{ $apple := where $downloads ".Params.product" "apple" }}
    {{ $android := where $downloads ".Params.product" "android" }}

    {{ range first 1 $apple }}
      <h2>{{ .Params.product }}</h2>
      {{ .Render "download" }}
    {{ end }}
    {{ range first 1 $android }}
      <h2>{{ .Params.product }}</h2>
      {{ .Render "download" }}
    {{ end }}

Is there a way to shorten this to dynamically generate sections for each product front matter entry?

Additionally, in the above example, .Params.product would just render apple. How would I turn that into a more descriptive title, like “Apple-specific Product Downloads” when it is rendered.

Checkout the docs on grouping by param. I think you could do something like this (untested):

{{ $downloads := where .Site.Pages ".Params.type" "download" }}

{{ range $downloads.GroupByParam "product" }}
  <h2>{{ .Key }}-specific Product Downloads</h2>
  {{ .Render "download" }}
{{ end }}
1 Like