HUGO

Using where with range over .Site.Data not working

Hi, I’ve been trying to figure out a puzzling issue that’s affecting my templates (running 0.55.5). I’m trying to modify a range call: {{ range .Site.Data.testimonials }} using a where clause, like so: {{ range where .Site.Data.testimonials "category" "consulting" }} but while the initial version works just fine, once the where clause is present, it simply doesn’t work, regardless of the condition I test for. I’ve trawled through the docs and the forums here and I just can’t see what I’m doing wrong.

Here’s example content of my .yaml files:

text: "Client text goes here"
name: "A Client"
category: "consulting"

And a test template I set up:

<ul>
    {{ range .Site.Data.testimonials }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>
<!-- Should return all -->
<ul>
    {{ range where .Site.Data.testimonials "name" "ne" nil }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>
<!-- Should return all -->
<ul>
    {{ range where .Site.Data.testimonials "dummy" "eq" nil }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>
<!-- Should return some -->
<ul>
    {{ range where .Site.Data.testimonials "category" "consulting" }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>
<!-- Should return some -->
<ul>
    {{ range where .Site.Data.testimonials "category" "==" "consulting" }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>
<!-- Should return some -->
<ul>
    {{ range where .Site.Data.testimonials "category" "!=" "consulting" }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>

I’ve also attempted variations using braces around the where clause, prefixing the .Site with $.Page, all to no effect. Whatever I do, only the unfiltered list above is rendered. Any pointers would be deeply appreciated.

I can’t reproduce your issue. Please put together a project with the issue and share it. :slight_smile:

1 Like

Sorry for the delay, I’ve set up a demo repo at https://github.com/Genyus/hugo-bug-demo.git (and tested a freshly-cloned instance still shows the same behaviour on my system)

FYI, I’ve stripped out most of the theme to simplify it and I also tried upgrading my local Hugo install to 0.68.1, to no avail. Thank you for taking the time to look at this.