Count content based on front matter variable


#1

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?


#2

Can anyone help please?


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

#4

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


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

#6

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?


#7

There is uniq.


#8
{{ $postCount := len (uniq $posts) }}

#9

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


#10

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 }}

#11

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.


#12

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?


#13

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.


#14

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.


#15

I have no more ideas.:frowning:


#16

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) }}

Get unique committers in hugo
#17

Appreciate all your help on this


#18

Boss man! That works great.

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

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

Thanks!