[Solved] Filter .Data.Pages based on content/subdirectory without using a page parameter

Without having to put a page parameter in each md file is it possible to filer .Data.Pages based on the subdirectory of content/ in which the md file was grabbed.

for example say I have an md file content/book/chapter1.md

I was hoping to do something like this. Range through only the pages that are in the book subdirectory

 {{ range $i, $e := .Data.Pages.book  }}

but apparently Hugo just lumps all the md “pages” into a single data key and does not distinguish using a sub key for each subdirectory.

So again my question. Any way to distinguish or “filter” pages based on the subdirectory the md file was in without resorting to putting a page parameter in each of those pages?

Newbie here, so I could be way off, but:

“section” is Hugospeak for “top-level subdirectory of content/”. Try using a where to filter out the non-books.

@adiabatic is right. I think what you need is:

{{ range $i, $e := (where .Site.RegularPages "Section" "book")  }}

In fact, you can get quite elaborate with these statements and pull out pretty much any data you like.

@budparr I saw there .Section in the Page Variables https://gohugo.io/templates/variables#page-variables

but didn’t realize I could not access subkeys via dot notation like in say js or like in go template if statements. This is a bit confusing actually someplaces yea someplaces you access subkey with " "

This means I now have three conditions in my where filter (see my other post you answered) and no clue if it is possible to do them all within the where construct itself.


Right. the dot notation works with things like parameters. You can indeed nest those where statements. Just for example (not precisely related to your issue, but along those lines) here’s something I was just working on:

{{ range first 1 (after $after (where (where .Site.RegularPages "Section" "video") ".File.BaseFileName" "in" .Params.videos ))  }}

so here’s what I’m doing:

first 1 
after 1
where ".File.BaseFileName" "in" .Params.videos 
(where .Site.RegularPages "Section" "video"

1 Like

Yes Thx @budparr ! this work…nesting where filters.

{{ range where ( where .Site.RegularPages  "Section" "sections" ) ".Params.hidden" "!=" true }}