Misunderstanding and misusing "where"

Hey all,

I’ve been working with Hugo for a few weeks and it’s been very interesting. Of the static site generators I’ve tried, I definitely like it the most. I haven’t quite figured out when to use title-case notation with dots, but I’ll get there.

Anyway, I’m unable to get where to behave the way I think it’s supposed to, based on the docs. I have a directory at /data/en/team/ that contains a few files, john-smith.yml and jane-doe.yml. Each is formatted roughly like this:

---
portrait: "/images/client/client-2.jpg"
title: John Smith
hidden: false
role: brain-dead slob
blurb: HOW DO YOU TURN THIS ON
about: Cillum do aute do minim deserunt cupidatat enim dolor mollit amet tempor ipsum qui nulla. Magna proident tempor consectetur ipsum dolor ...
social_media: []

Then, in a debugging partial, I have

{{ $team_members := index site.Data site.Language.Lang "team" }}

{{ warnf "found %d team members:" (len $team_members) }}
{{ range $team_members }}
  {{ warnf "\t- %s (hidden: %t)" .title .hidden }}
  {{ range $key, $val := . }}
    {{ warnf "\t\t%T|%v => %T|%v" $key $key $val $val }}
  {{ end }}
{{ end }}

{{ warnf "hidden members:" }}
{{ range where $team_members ".hidden" true }}
  {{ warnf "\t- %s" .title }}
{{ end }}

{{ warnf "DONE" }}

Then, when launching the server, I get this in the log:

WARN 2020/08/09 22:20:56 found 2 team members:
WARN 2020/08/09 22:20:56 	- John Smith (hidden: false)
WARN 2020/08/09 22:20:56 		string|about => string|Cillum do aute do minim deserunt cupidatat enim dolor mollit amet tempor ipsum qui nulla. Magna proident tempor consectetur ipsum dolor ...
WARN 2020/08/09 22:20:56 		string|hidden => bool|false
WARN 2020/08/09 22:20:56 		string|portrait => string|/images/client/client-2.jpg
WARN 2020/08/09 22:20:56 		string|role => string|brain-dead slob
WARN 2020/08/09 22:20:56 		string|social_media => []interface {}|[]
WARN 2020/08/09 22:20:56 		string|title => string|John Smith
WARN 2020/08/09 22:20:56 	- Jane Doe (hidden: true)
WARN 2020/08/09 22:20:56 		string|about => string|Cillum do aute do minim deserunt cupidatat enim dolor mollit amet tempor ipsum qui nulla. Magna proident tempor consectetur ipsum dolor ...
WARN 2020/08/09 22:20:56 		string|blurb => string|I pretend to do things with stuff
WARN 2020/08/09 22:20:56 		string|hidden => bool|true
WARN 2020/08/09 22:20:56 		string|portrait => string|/images/client/client-2.jpg
WARN 2020/08/09 22:20:56 		string|role => string|Employee Impersonator
WARN 2020/08/09 22:20:56 		string|social_media => []interface {}|[]
WARN 2020/08/09 22:20:56 		string|title => string|John Doe
WARN 2020/08/09 22:20:56 hidden members:
WARN 2020/08/09 22:20:56 DONE

Effectively I can’t seem to filter down a set – I either have it all or I have none. And I can’t find the problem in my usage compared to the docks. I’ve tried variants of the invocation, including

  • where $team_members ".hidden" true
  • where $team_members ".Hidden" true
  • where $team_members ".hidden" "true"
  • where $team_members ".Hidden" "true"

(as I mentioned I don’t entirely get when you’re supposed to use upper vs lower-case attribute access, and sometimes Hugo wants them as strings too, idk)

Hopefully my example is adequately complete so as to be obvious to someone more seasoned than I.

Many thanks!

where:

Filters an array to only the elements containing a matching value for a given field.

$team_members is a map:

{{ printf "%#T" $team_members }} => map[string]interface {} 

y’ever stared at something so long you still see it when you close your dry, itchy eyes?

Thanks for pointing this out. I’ll just use an if. :laughing:

Edit: searching based on this also lead me to this very similar question from 2015. I’ve looked but haven’t found any functions that quite fit this use case, maybe it’s worth making one? Assuming the if pattern is adequately inelegant.

we’ve all been there… :sweat_smile:

1 Like

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