[SOLVED] Filter pages where a value is in a parameter

In my pages I have several tags in an array.

tags = ["books", "movies]

If I want to select only the pages that contain the tag “books”, what would be the correct way to filter them?
I’m looking for something like (pseudo code):

range .Site.Pages where in .Params.brands $tag

Check the examples in the taxonomy templates docs.

The examples there won’t work with a variable.
I think I can get it with ‘where’ and ‘intersect’

i think use with .Page.Site.Taxonomies.brands.books … there is a lot in the forum on that particular question, I just can’t recall the exact posts. I might not be right about the syntax, but it’s something like that. with is considered a filter.

Yeah, that’s the documented way to filter taxonomies. But that doesn’t take care of the variable.
Also, my question has to do with the correct syntax to filter content by searching a variable in an array.

I found it. There is an example in the docs for the where method, that does exactly what I’m looking for, and in fact, it uses ‘intersect’ operator:

{{ range where .Site.Pages ".Params.tags" "intersect" .Params.tags }}
  {{ if ne .Permalink $.Permalink }}
    {{ .Render "summary" }}
  {{ end }}
{{ end }}

But for some reason it just doesn’t work on my site. I’m doing this:

<ul>
  {{ $tags := ( slice .Title) }}
  {{ range where .Site.Pages ".Params.brands" "intersect" $tags }}
    <li>
      <a href="{{ .Permalink }}">{{ .Title }}</a>
    </li>
  {{ end }}
</ul>

In this case, the title of the page I’m looking for is the name of the tag I’m trying to use to filter pages. That’s why I’m putting it in an array, with slice, and then comparing it to the ‘.Params.brands’ array.

But it doesn’t return any results… any ideas?

2 Likes

You might want to echo $tags somewhere to see what it is returning. Maybe .Title needs to be formatted to match the tags from the array.

Yeah, it seems it might have to do with that. But anyway… according to the docs, that’s the answer to the OP.

This seems very strange to me, and I can’t help but feel like this might be the outgrowth of the kind of hackish (not saying hacks are always bad) taxonomy-related threads that have been bouncing around lately. If the title of the page is the tag, then why not just use a normal taxonomy page and leverage .Data? The only time you should need to use .Site.Taxonomies... is when rendering to pages other than taxonomy templates and terms templates…

You’re very right about that. The OP It’s about how you can find if a value is IN an array, which is useful in many ways. I was trying to use the method ‘in’ but the right way to do it was using the method ‘where’ with an ‘intersect’ operator. I found that example in the documentation, so I tried to use it. That’s why I marked it as solved, and tried no to go off topic by solving other problems.

But, while trying to do this I finally found that the actual way to filter taxonomies dynamically is by using Data.Pages inside the terms template. Hugo already filters your taxonomies for you. That was the answer I’ve been looking for for a week. All the time I invested in hacks could have been saved if the docs had an example of the usage of .Data . pages in the taxonomy templates section. It is in the docs for taxonomy variables, but with no examples. Maybe that could be my first contribution to the docs. I’d love to give back to this awesome software.

1 Like