Range where .Site.Pages ".Params.tags"

Please, help. This code did not work in any configuration:

{{ range where .Site.Pages ".Params.tags" "in" "tag_to_display" }}
  {{ . }}
{{ end }}

I tried intersect instead in with same no result. I tried to use variable $tag_to_display_var := slice "tag_to_display" instead of "tag_to_display" string - same empty result.

My temporary workaround is

{{ range where .Site.Pages ".Params.tags" "intersect" $.Page.Params.foo.array_of_tags_to_display }}
  {{ . }}
{{ end }}

But I need to use hardcoded string, not array of tags from the frontmatter. I need to output pages with a specific both category and tag.

The brilliant code could look like this (non-working code!):

{{ range where
   (where .Site.Pages ".Params.categories" "in" "category_to_display")
   ".Params.tags" "in" "tag_to_display"
}}
  {{ . }}
{{ end }}

I know that I can use Taxonomies but my finish aim is to get pages with the both tags and categories. Taxonomies didn’t give this possibility.

Now I use this code to get pages with both the tags and the categories:

{{ $filtered := "" }}

{{ $set1 := where .Site.Pages ".Params.categories" "intersect" $.Page.Params.foo.categories }}
{{ $set2 := where .Site.Pages ".Params.tags" "intersect" $.Page.Params.foo.tags }}

{{ if and (gt (len $set1) 0) (gt (len $set2) 0) }}
  {{ $filtered = $set1 | intersect $set2 }}
{{ else if (gt (len $set1) 0) }}
  {{ $filtered = $set1 }}
{{ else if (gt (len $set2) 0) }}
  {{ $filtered = $set2 }}
{{ end }}

{{ with $filtered }}
    {{ . }}
{{ end }}

Thanks a lot!

There is so much in your code samples that looks weird (in sense of the need for more information).

  • Variables are initialized by {{ $variablename := value }}. You appoint a value to something that might exist or not if you write {{ $variablename = value }}
  • the sample code on the where page uses a nice quick sample with variables (2nd code sample): https://gohugo.io/functions/where/#use-where-with-intersect
  • “This code did not work in any configuration” - does that mean:
    • running with this code crashed Hugo with a not posted error code?
    • running with this code resulted in warnings (also not posted)?
    • running with this code resulted in an empty page-collection being returned so nothing is rendered?

I would probably try to accomplish this by using your last code, with modifications:

  • use default on both variables with an empty page collection so I can always intersect without checking {{ $variable := something | default slice }}
  • wondering about the $gallery1 in the second if - and assuming it’s $set1 :wink:

Something like this:

{{ $set1 = where .Site.Pages ".Params.categories" "intersect" $.Page.Params.foo.categories | default slice }}
{{ $set2 = where .Site.Pages ".Params.tags" "intersect" $.Page.Params.foo.tags | default slice }}
{{ $filtered = $set1 | intersect $set2 }}
{{ with $filtered }}
    {{ range $filtered }}
    // print the page
    {{ end }}
{{ end }}

For the future:

  • code samples, if they don’t work, describe what “do not work” means. possibly with error messages.
  • markdown/content/structure samples - just to go sure it’s properly configured in the frontmatter.

And have a look at debugprint by kaushalmody - https://github.com/kaushalmodi/hugo-debugprint - that will help to look into the variables that end up failing.

If the first example does not return ANYTHING. then the issue is in your content. If you have a page with at least one tag “tag_to_display” it should come up with that line of code

tags: tag_to_display

is wrong

tags:
- tag_to_display

is the proper way.

Thanks a lot for so quick feedback!

Variables are initialized by {{ $variablename := value }}. You appoint a value to something that might exist or not if you write {{ $variablename = value }}

This is because of lost some code from another part. Now I corrected it.

the sample code on the where page uses a nice quick sample with variables (2nd code sample)

I was not able to get the pages with a specific hardcoded tag, not from the frontmatter. I expect this code to work, but it doesn’t work. Empty result. No errors. Just nothing happens:

{{ range where .Site.Pages ".Params.tags" "in" "my_tag" }}

This code did not work in any configuration

I meant that the code doesn’t do anything. No errors, no warnings - just zero results.

wondering about the $gallery1 in the second if - and assuming it’s $set1

It’s just an error. I fixed it. I tried to make the code more readable, and in some places I didn’t fix everything.

{{ $variable := something | default slice }}

Big thanks for the trick!

tags:
- tag_to_display

Yep, sure, I use this, only this in all frontmatters. (Not exactly the same, I use yaml frontmatters: tags: [“tag1”, “tag2”])

If the first example does not return ANYTHING. then the issue is in your content.

First example does not return ANYTHING. I ran an empty version of Hugo and created a page with tags: ["fav"] in the frontmatter and then tried to get this page by

{{ range where .Site.Pages ".Params.tags" "in" "fav" }}
  {{ . }}
{{ end }}

I was only able to achieve this, this code works:

{{ $t := slice "foo" }}
{{ range where .Site.Pages ".Params.tags" "intersect" $t }}
  {{.}}
{{ end }}

But this code does not work (it gives a zero result) (It is assumed that I have a page with these two tags foo,bar)

{{ $t := slice "foo,bar" }}
{{ range where .Site.Pages ".Params.tags" "intersect" $t }}
  {{.}}
{{ end }}

Thank you so much for your help! I’ve been working on this problem for two days now, trying to get pages with certain tags and categories from the collection. In other systems, this was a trivial task, but in Hugo, for some reason, I’m stuck.

{{ $t := slice "foo", "bar" }}

I also never saw the in used this way. The in function goes in collection search.

{{ $bool := in $tags "foo" }}

I made

{{ $t := slice "foo" "bar" }}

{{ range where .Site.Pages ".Params.tags" "intersect" $t }}
  {{.}}
{{ end }}

…and get all pages with foo plus all pages with bar, but not all pages with both foo and bar