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.
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?
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.
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" }}
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?