I want to display all unique keywords
used in the section books
and group these
by genre
.
content/books/book1.md:
keywords = ["Adventure", "Climbing"]
genre = "Fiction"
content/books/book2.md:
keywords = ["Adventure", "Swimming"]
genre = "Fiction"
content/books/book3.md:
keywords = ["Biography", "War"]
genre = "Non-Fiction"
What I’m looking for:
Genre: Fiction
Keywords: Adventure, Climbing, Swimmming
Genre: Non-Fiction
Keywords: Biography, War
This will give all unique keywords
:
{{range (where site.Pages "Section" "books")}}
{{range .Keywords}}
{{$.Scratch.Add "keyword" (slice .)}}
{{end}}
{{end}}
{{range .Scratch.Get "keyword" | uniq}}
{{.}}
{{end}}
But I’m not sure how to group these keywords
by genre
so I was thinking I might need to do something like this:
{{$.Scratch.Add "index" slice}}
{{range (where site.Pages "Section" "books")}}
{{if .Params.genre}}
{{$.Scratch.Add "index" (dict "genre" .Params.genre "keyword" .Keywords)}}
{{end}}
{{end}}
This gives a map, but I’m not sure where to go from here.
zwbetz
2
You could group by page param:
{{ $books := where site.Pages "Section" "books" }}
{{ range $books.GroupByParam "param_key" }}
<!-- your keyword logic here -->
If I understood correctly, I should do this:
{{$books := where site.Pages "Section" "books"}}
{{range $books.GroupByParam "genre" }}
{{range .Keywords}}
{{$.Scratch.Add "keyword" (slice .)}}
{{end}}
{{end}}
This gives the following error: can't evaluate field Keywords in type hugolib.PageGroup
Also, how would I get the .Key (genre) for the keywords?
zwbetz
4
Look at the link in my previous reply and see the example. You have to do additional work after the grouping.
Thanks. I’ve been looking at the example you linked to as well as this thread: Get all unique taxonomies of a page, but I’m still struggling.
I’ve managed to get the .Key
and all keywords
, but I can’t get uniq
to work correctly.
{{$books := where site.Pages "Section" "books"}}
{{range $books.GroupByParam "genre"}}
<h3>{{.Key}}</h3>
<ul>
{{range .Pages}}
{{range .Keywords}}
{{$.Scratch.Add "keyword" (slice .)}}
{{end}}
{{$.Scratch.Get "keyword" | uniq}}
{{end}}
</ul>
{{end}}
I’ve tried placing {{$.Scratch.Get "keyword" | uniq}}
in different places, but can’t get it to display the unique keywords for each genre.
Using similar code to the example I linked to above, I also can’t get the correct values.
{{$books := where site.Pages "Section" "books"}}
{{$slice := slice}}
{{range $books.GroupByParam "genre"}}
<h3>{{.Key}}</h3>
{{range .Pages}}
{{range $tag := .Keywords}}
{{$slice = $slice | append $tag}}
{{end}}
{{range $uniqueTags := $slice | uniq}}
{{$uniqueTags}}
{{end}}
{{end}}
{{end}}
Basically, I’ve tried two different ways to achieve the same result.
zwbetz
7
Try this:
{{$books := where site.Pages "Section" "books"}}
{{range $books.GroupByParam "genre"}}
<h3>Genre: {{.Key}}</h3>
{{$keywords := slice}}
{{range .Pages}}
{{range .Keywords}}
{{$keywords = $keywords | append .}}
{{end}}
{{end}}
{{$keywords = uniq $keywords}}
<p>Keywords: {{delimit $keywords ", "}}</p>
{{end}}
2 Likes
That works! Many thanks for being such a helpful community member on here.
1 Like