Struggling to get "where" to behave like docs example

In the docs: https://gohugo.io/functions/where/
It says:

It can also be used with the logical operators != , >= , in , etc. Without an operator, where compares a given field with a matching value equivalent to = .

It then gives this example:

{{ range where .Pages "Section" "!=" "foo" }}
   {{ .Content }}
{{ end }}

I am trying to do something slightly more complex, but I think it should work. However, it doesn’t.
Note: $sources is a slice of strings, populated earlier on.

{{ $sourceSection := where $.Site.RegularPages "Section" "sources"}}
{{ range where $sourceSection ".Params.shortTitle" "in" $sources }}
<li><a href="{{ .Permalink }}">{{ .Title}}</a></li>
{{ end }}

I’ve tried a few combinations (putting quotes around $sources, changing the order etc.)
All the values/slices are as I expect them to be. This slightly longer code gives the desired result:

{{ range $sourceSection }}
{{ if in $sources .Params.shortTitle }}
<li><a href="{{ .Permalink }}">{{ .Title}}</a></li>
{{ end }}
{{ end }}

Using “where” just looks more elegant/correct, and I’d like to understand why it isn’t working.

Full code for the layout, as well as some test content files, are here: https://github.com/StarfallProjects/habermas-concepts/blob/master/layouts/concepts/single.html

I can’t quite tell what you are trying to accomplish. What kind of output are you looking for?

I ask, because you are pointing to code in your single.html template, but your concepts and sources are taxonomies, so that could mean you want to modify a different template… maybe. I don’t know.

On a “reference” page, what should be output in the HTML?

Try a lowercase param key:

{{ range where $sourceSection ".Params.shorttitle" "in" $sources }}

Also, small tip: build an empty slice with just:

{{ $sources := slice }}
1 Like

The template I’m working on at the moment is for concepts.I have both a “concepts” taxonomy and concepts content (https://github.com/StarfallProjects/habermas-concepts/tree/master/content/concepts) So that layout is rendering the concepts content.

I am trying to list links to all related references and sources.

So the website visitor goes to a single concept, and can view a list of links to all references to that concept, and all sources that discuss the concept.

Lowercasing the param worked - thank you!

Can you tell me why? The camelcase param worked in the other way of doing it:

{{ range $sourceSection }}
{{ if in $sources .Params.shortTitle }}
<li><a href="{{ .Permalink }}">{{ .Title}}</a></li>
{{ end }}
{{ end }}

And lowercasing fixed it without making it lowercase in the frontmatter.
So what is it about range where that means I need to refer to .Params.shortTitle as .Params.shorttitle?

And this doesn’t work:
{{ range where $referenceSection $title "in" ".Params.concepts" }}

It smells like a bug.

The problem likely lies in the key lookup logic of where. The in match works as expected when the key is found, so in is fine. @bep attempted to fix this scenario back in November in v0.60.0, but the problem seems to have resurfaced.

Can you open a bug report in GitHub for this issue?

Will do so tomorrow (it’s late here now), thank you :slight_smile:

Issue created: https://github.com/gohugoio/hugo/issues/7009

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