That actually behaves the way I’d expect when I put it into a layout like this:
<h2>This correctly lists the dictionary entries one-by-one</h2>
{{ range .Params.dictionary}}
<li>{{.}}</li>
{{end}}
My goal is to be able to filter the dictionary and display “All words that start with ‘A’”, “All words that start with ‘B’”, etc.
The where function seems like a great fit for this, but I can’t get it to work even with a basic exact match:
<h2>This does not even print the exact match</h2>
{{ range where .Params.dictionary "." "ABHOR"}}
<li>{{.}}</li>
{{end}}
I’ve also tried variations of if with hasPrefix. Still nothing:
<h2>No luck with this either</h2>
{{ $s := slice .Params.dictionary }}
{{ range $s}}
{{ if hasPrefix "." "A"}}
<li>{{.}}</li>
{{ end }}
{{end}}
So it seems like I’m missing a fundamental concept here, and I’d love for someone to explain:
What the heck am I missing?
How can I filter my list of words by first letter (A, B, C, etc)?
How can I filter my list of words by last letter (not mentioned above, but my goal is to have separate listings of words based on first letter and last letter)
How can I make those filters “dynamic” based on a separate front matter property that lists the first and last letters of interest in the dictionary. I.e., a front matter property for "letterlist": ["A","J","P"], and I’d iterate over that letterlist to generate sections for “Words starting with ‘A’”, “Words starting with ‘J’”, “Words starting with ‘P’”, “Words ending with ‘A’”, etc.
I really appreciate the help in advance. I want to get proficient with Hugo, but this Go templating just isn’t sinking in for me.
Thank you for the quick response, @jmooring. I will look this over and try to make sense of it. I’m trying to avoid my temptation to just process add’l JSON structures into my data using nodeJS, which I’m much more proficient with.
Can you expound on why my simple examples might be failing? I seem to be following the documented examples exactly, and yet they’re still not working.
Two problems with this:
1) .Params.dictionary is already a slice. You are setting $s to a slice of a slice.
2) You are quoting the context (the dot) in the if block
The corrected code:
{{ $s := .Params.dictionary }}
{{ range $s }}
{{ if hasPrefix . "A" }}
<li>{{ . }}</li>
{{ end }}
{{ end }}
Your suggestion to jsonify into a <pre> tag was fantastic; that’s exactly what I needed to understand how this JSON is being generated under the covers and debug with some visibility. Repeated here for others who’ll stumble upon this thread: <pre>{{ jsonify (dict "indent" " ") $words }}</pre>
So… can I use a where function with a basic array/slice? I.e., What is the conventional way to filter a slice of strings in Hugo, e.g., using findRE?
(Thank you for your patience. I’ll stop asking questions in this thread since you’ve already solved my primary problem; just want to make sure I understand what you’ve written.)