Count content based on front matter variable

I’m trying to figure out how to count the number of unique companies I have in my Content folder. The front matter for each post is as follows:

company = "Acme Inc"
date = "2017-10-01T01:08:19+02:00"
product = "Apples"

company = "Acme Inc"
date = "2017-10-01T01:08:19+02:00"
product = "Pears"

company = "Dumbo Inc"
date = "2017-10-01T01:08:19+02:00"
product = "Oranges"

In this example, the count would be 2.

I’ve tried to do things like:

{{ $posts := (where .Site.RegularPages ".Params.company") }}
      {{ $postCount := len $posts }}

But this doesn’t work as I’m not passing in an argument since I don’t want to count how many times a specific company appears.

Suggestions, please?

Can anyone help please?

{{ $posts := (index .Site.RegularPages .Params.company) }}

Thanks for helping out. I’ve tried your code, but I receive the following error message:

Error while rendering "home": template: index.html:53:13: executing "index.html" at <index .Site.RegularP...>: error callingindex: cannot index slice/array with nil

 {{ $posts := (where .Site.RegularPages ".Params.company" "ne" nil) }}
 {{ $postCount := len $posts }}
2 Likes

Mikhail, that works. Thank you.

If I may, how would I modify your code to only show the number of unique companies and not the total number of companies?

There is uniq.

1 Like
{{ $postCount := len (uniq $posts) }}

Tried doing that last night and several other similar versions, but kept getting the following error message:

ERROR 2017/10/11 12:55:24 Failed to render "index.html": reflect.Set: value of type hugolib.Page is not assignable to type *hugolib.Page

If uniq does not work, and it looks like it does not work, then it can be done through range+.Scratch or mybe:

{{ $posts := len $.Site.Taxonomies.company }}

Alright, thanks. I’m not using taxonomies so I’ll play around with range and scratch and see if I can get that to work.

I’ve never used scratch before, but I’ve now managed to get the total number of companies:

  {{$posts := .Site.RegularPages}}
    {{$.Scratch.Set "postCount" 0}}
    {{range $posts}}
    {{$.Scratch.Add "postCount" 1}}
    {{end}}
    {{$.Scratch.Get "postCount"}}

This gives the same result as

{{ $posts := (where .Site.RegularPages ".Params.company" "ne" nil) }}
 {{ $postCount := len $posts }}

But I still can’t get the number of unique companies. I’ve tried different versions such as

    {{$posts := .Site.RegularPages ".Params.company" "intersect" (slice .Params.company)}}

But that doesn’t work. Can you anyone help?

To activate the taxonomy enough to add two lines to the config.yaml.

taxonomies:
  company: ""

As far as I remember, if you add a taxonomy with an empty value, it will become available as $.Site.Taxonomies.company, but Hugo will not generate an unnecessary folder for it.

Sorry about going back and forth on this problem.

$.Site.Taxonomies.company does not work. I’m using toml, but that shouldn’t matter. The taxonomy is an empty value. I’ve tried to get this to work and show all company names but it just returns 0 so of course the counter doesn’t work either.

I have no more ideas.:frowning:

1 Like

My untested offering:

{{ range where .Site.RegularPages ".Params.company" "ne" nil }}
{{   $.Scratch.Add "companies" (slice .Params.company) }}
{{ end }}
{{ $.Scratch.Set "companies" ($.Scratch.Get "companies" | uniq) }}
2 Likes

Appreciate all your help on this

Boss man! That works great.

I just added the following to your code to get the count

{{len ($.Scratch.Get "companies")}}

Thanks!