Unable to print Count of Entries using substr or hasPrefix

Hello,

I have been struggling to find out the count of fruits based on letters.

I have been exploring and learning goHugo for about 45 days now and the more I work on it the more complicated it gets.

Here is what I have -
I have about 100 files (now) with some data about fruits stored in content/fruits folder. Accompanying these, I also have some images of fruits that are in the static folder. The fruit files are named as Apple.md, Banana.md like so.

Here is what I want to do -
I want to have a list of letters followed by the count of fruits that start with this letter and they should have the “has_picture” field set to “Y” (which would mean that they have pictures). So

A (5 Fruits)
B (3 Fruits)
C (6 Fruits)

And here is what I have been trying out with -
(Substr Function - )

{{ $alpha := slice "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" }}
{{- range $alpha -}}
     {{ $letter := . }}
      {{ $fruitsByName := ( sort ( where (where $.Site.Pages "Section" "fruits") "Params.has_picture" "=" "Y") ".Params.fruit_name"  ) }}
     {{ $fruitCount := len ( where $fruitsByName (substr ".Params.fruit_name" 0 1) "==" (upper $letter) ) }}
.
.
.
.
{{ end }} 

The result is always Zero - and I guess it is because I am enclosing the .Params.fruit_name in double quotes. If I dont, I get an error stating

 can't evaluate field Params in type string

I also tried with the hasPrefix function but could not get it working as well -

{{ $fruitCount := len (where $fruitsByName ( hasPrefix ".Params.print_name" (upper $letter) ) )  }} 

I would appreciate if someone around could provide me some help.

The dot inside your range is what you ranged, not the page context. You need to put the dot into a variable BEFORE you range and then inside of the range use that variable.

{{ $page := . }}
{{ range $stuff }}
{{ so something with $page and . }}
{{ end }}

Well, the first statement with the sort works good. I have tested it. After it was sorting the right way, I added the second statement - and it fails on the substr part. Expecting the $fruitsByName to contain all that I needed, I even tried

(substr $fruitsByName.Params.fruit_name 0 1) "==" (upper $letter)

And even

(substr $fruitsByName.fruit_name 0 1) "==" (upper $letter)

But both fail here.

You cannot apply a function to the left side of a where clause. Try this:

{{/*
Build a map of pages. For each element:
- The key is the first letter of the title (lowercase)
- The value is a slice of related pages
*/}}
{{ $mp := dict }}
{{ range $p := where (where site.RegularPages "Section" "fruits") "Params.has_picture" "Y" }}
  {{ $firstLetter := strings.ToLower (strings.Substr $p.Title 0 1) }}
  {{ with index $mp $firstLetter }}
    {{ $mp = merge $mp (dict $firstLetter (append . (slice $p))) }}
  {{ else }}
    {{ $mp = merge $mp (dict $firstLetter (slice $p)) }}
  {{ end }}
{{ end }}

{{/* Display links to the pages, grouped by the first letter with count. */}}
{{ range $firstLetter, $pages := $mp }}
  <h2>{{ strings.FirstUpper $firstLetter }} ({{ len $pages }})</h2>
  {{ range $pages.ByTitle }}
    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a><br>
  {{ end }}
{{ end }}
2 Likes

Sir, first, thank you so much for this - I would try out tomorrow as it is almost 12 am here at my place.

If I get your correctly on that quoted text, and there is no type, then, if functions could not be applied to the left side of a where clause, the following from this URL - Finding Count on a set of files with specific Front Matter - #6 by Sids - works. I would be thankful if you could help me understand the difference.

{{ $section := where site.Pages "Section" "fruits"}}
	<p>Number of Red Fruits:
		{{ len (where $section "Params.fruitcolor" "==" "red") }}</p>

{{ where site.Pages "Params.foo" "eq" "something" }}
                    ------------      -----------
                     left side        right side
                     of where         of where
                     clause           clause
                     comparison       comparison
2 Likes

@jmooring

Oh, I was so wrong in interpreting what you said above. Thank you always for being around and making things look so simple for many of newbies like me.

1 Like