Group by multiple front matter entries

I’m using GroupByParam to group pages based on a front matter string. It works quite well for a single piece of data, but I’m confused how to handle more than one entry.

If I want to aggregate movie directors, it only works when there is one director. For a film with two or more directors I can’t figure out how to handle grouping. Using an array doesn’t seem to work.

Is there an easy / obvious way to deal with this that I’m missing? Sample code including below.

Working front matter

director: Martin Scorsese

Problem front matter

director: Danny Boyle, Loveleen Tandan

Or

director: Danny Boyle
director: Loveleen Tandan

Or

director:
- Danny Boyle
- Loveleen Tandan

Template

{{ range .Site.Pages.GroupByParam .Params.key }}
    <p>{{ .Key }}</p>
    {{ range .Pages }}
        <p>{{ .Title }}</p>
    {{ end }}
{{ end }}

You could configure your director param to be a taxonomy: https://gohugo.io/content-management/taxonomies/

Then you can use taxonomy templates to list content related to each director: https://gohugo.io/templates/taxonomy-templates/#list-content-with-the-same-taxonomy-term

Ahh, brilliant, thank you. That looks very close to what I need. Two follow up questions.

If I use the following code:

{{ range $key, $taxonomy := .Site.Taxonomies.featured }}
{{ end }}

How do I then insert my .Params.key variable into .Site.Taxonomies? Eg. to replace the hard-coded taxonomy featured.

And secondly, how do I get the $key value as the original text? My output currently displays “danny-boyle”.

Sorry if they’re basic questions.

Have a look at this example: https://gohugo.io/templates/taxonomy-templates/#site-getpage-for-taxonomies

So in your case you would use {{ $taxo := "directors" }}.

Then we add the innermost range .Pages to list the content associated with each term.

{{ $taxo := "directors" }}
<ul class="{{ $taxo }}">
  {{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
    {{ range .Pages }} <!-- list terms associated with taxo -->
      <li><a href="{{ .Permalink }}">{{ .Title}}</a>
        <ul>
          {{ range .Pages }} <!-- list content associated with term -->
          <li>{{ .Title }}</li>
          {{ end }}
        </ul>
      </li>
    {{ end }}
  {{ end }}
</ul>
1 Like

That is perfect, thank you very much.