Finding Count on a set of files with specific Front Matter

Hello Community.

A few days ago I was introduced to the amazing software GoHogo by a friend who is into development of websites. I good hooked up and started reading the documentation and to be honest, I found it interesting, easy and fun.

However, I soon have run into problems when I tried building some test stuff with it. I do hope that joining this forum will not only help me understand GoHugo better, but also help me grow in my capacities of developing static websites with it for my clients in the near future.

Okay so being a novice, who has just started off, this might sound lame but here is what I want to do -
I have a folder called “fruits” in the content folder. And this folder has about 50 files about fruits named in the format - fruitname.md

Each of these files have a Front Matter entry - fruitcolor: value - where value is the generic color of the fruit. So basically, it has values like red, green, yellow etc.

Now I have been trying to write some code where I would like the output on the page rendered in this format -
Red:
Total Fruits with Red Color: xx
Apples
Pomegranates
… [List of fruits which have the Value Red for the front matter fruitcolor.

For the Total Fruits with Red Color - I have been trying out the len function using the where and ideally it should be something as -

<p>Total Fruits with Red Color: {{ len (where .Site.RegularPages "fruitcolor" "==" "red") }}.</p>

Unfortunately I am unable to figure out the how. I understand the issue is somewhere here - “.Site.RegularPages” as these md files are not regular pages (I hope this is for pages/files that are directly in the content folder, not a subfolder inside content).

And, if this would help - the code is placed in an HTML file (list.html) in the folder /layouts/fruits

Thanking you community for reading out the post and it would be great if you could also let me understand and/or resolve the problem.

On another note, I am sorry if I posted it in the wrong section or there was some other info that was needed and I missed out. I am still getting used to this forum. So I hope, I would be excused and even corrected where I commit mistakes.

Have a great time.
Sid.

The following puts all pages in the section “blog” into a page collection:

{{ $section := where site.Pages "Section" "blog"}}

You can then use $section as you would use site.RegularPages (count frontmatter, etc).

Does that help? If not, I probably didn’t understand your issue properly.

1 Like

Thanks @davidsneighbour

Here is what I tried -

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

The output was 0, but I have 5 files where fruitcolor: red.

I think I am definitely using a wrongly formatted condition - and using the where above, I am not clear in specifying the frontmatter. Asking you for a fix to my code would surely be impolite on my part. Can you help me understand it better, please? Maybe some URL or anything.

hmm… can you put a simple sample online? I am not sure about the "==" part, what happens if you use eq there (equals). I think math-notation should work, but I am always using things like eq, ne at these locations.

paging @jmooring (sorry).

{{ where (where site.RegularPages "Section" "fruits") "Params.fruitcolor" "red" | len }}
2 Likes

@davidsneighbour and @jmooring

Thank you for being there. It worked. To be honest, I still could not understand why the “Params.fruitcolor” worked as expected (it accessed the fruitcolor value and compared it with my value of “red” - right? I mean is that how that worked) and why Params.fruitcolor (without quotes) failed. I was experimenting with .Params, Params, $section.Params as well (and all were failing).

After getting the idea that I needed to use Quotes, the following worked -

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

And of course, this single statement with nested where did work as well.

{{ where (where site.RegularPages "Section" "fruits") "Params.fruitcolor" "red" | len }}

Oh, is that possible without an investment? I did not know that we could host some of our gohugo codes directly without converting and generating the HTML version. I would find out more on this one as this would surely be a great help as I am learning.

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