Advanced Hugo Filtering

Yesterday I received a call from a client who requested that I split a section’s list page into separate sub-pages based on location.

This was the first time that I really delved into the nested where territory and here is how I did it.

With this structure

└── content
    └── exhibitions
           ├── <---various markdown pages --->
           ├── athens
           |      └── _index.md
           └── thessaloniki
                  └── _index.md

And this complex combination of .Site.GetPage and nested where functions

{{- if in (.Permalink | string) "/exhibitions/athens/" -}}
{{- $section := (where (where (.Site.GetPage "section" "exhibitions").Pages ".Params.venue" "ne" "Thesalloniki") ".Params.venue" "ne" "Athens Festival") -}}
{{- range $section -}}
<--- Layout --->
{{- end -}}
{{- end -}}

I was able to render content pages from the main /exhibitions/ section into the nested section /exhibitions/athens/ while excluding pages from other venues.

All this is done directly from the template /layouts/_default/list.html

(I prefer having just one template to rule all of my list pages, hence the .Permalink check)

Also I repeated the above for other Nested Sections.

And I am sharing this because it’s pretty powerful, 100% Hugo and better than other Content Filters I’ve posted about in the past.

If you feel that this can be improved please share.

8 Likes

Very helpful and useful. Tnx!