Is it possible to use a function on keys of a collection when using `where`?

I want to filter a page based on its path. For this I thought of using where function and this is my attempt:

{{ $pageAuthors := .Params.authors }} 
{{ $authorName := where (where site.Pages "Section" "authors") (path.Base (path.Split .Path).Dir) "in" $pageAuthors }}

But this is not working for me as it returns a Page(0) collection. Please tell me where I might have went wrong, or hopefully a better approach for this use-case, which is to get the page resource of the author of the current page.

I’m not sure I understand what you are trying to do. Do you have your code somewhere I can clone?

I haven’t uploaded the code. But I’ll try to explain what I’m trying to do:
This is my directory tree:

content
  -> authors
    -> author1/_index.md
    -> author2/_index.md
... 

The names of the authors, author1 and author2 are also Params for my RegularPages. I want to be able to refer to the front matter inside content/authors/<some author>/_index.md while on a random RegularPage.
The above snippet is an attempt to access the page resource for the author.
Hope this clears things to some extent, if not, I’ll try to emphasize more.

You could just .GetPage the author page and then get the resources. So something like:

{{ $author := "authorname" }}
{{ $authorpage := site.GetPage (print "authors/" $author ) }}

{{ with $authorpage }}
  {{ .Resources }}
{{ end }}
1 Like

Thanks! But still, just curious, is it not possible to use functions on where keys?

It should be possible.

I would look at what this resolves to:

and then try to test with the value hardcoded.

I tested it, not working.

But does it make sense to be using that value? If it’s not a built-in page var like Section, Type etc, it should be .Params.something : https://gohugo.io/functions/where/

It should make sense, I’m filtering the collection based on its elements’ paths.

Is the value .Params.something? or a built-in page var?

It’s a built-in page var.

Then you need to show your site code if you want to use the nested where method because it should work. I can only guess without being able to test.

Well, I don’t want to push the changes upstream right now, since it’ll affect the deployment of my website etc.
I can, however, confirm that path.Base (path.Split .Path).Dir returns author1, author2 according to above directory tree. The issue happens when I use it in where.

I know that it’s not nice of me not to provide you with the code, but I really don’t want to. There are a lot of changes in the code since I’m doing a revamp of my website and hence I’m waiting to push it once everything’s well tested.

It isn’t a matter of niceness; we literally don’t care about your website, so you should be reproducing the issue in a minimal repo and sharing that. We just need to look at the source code to understand what’s happening.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.