To access nested tags and sum them?

I have a data structure that looks like this:

---
date: 
draft:
prods:
  - itemname: 
    itemdescription: ""
    tags:
    images:
      - ""
email: 
---

I want to list the ‘tags’ and give their count.
Nested tags don’t require definition in taxonomy in hugo.toml, so there is no taxonomy keys.

Now I’m stuck with this code:

{{- range .Site.Pages }}
  {{ range .Params.prods }}
    {{- if isset . "tags" }}
      {{ $tagCount := len .tags }}
      <li class="small">
        <a class="cat-item d-flex justify-content-between text-dark px-1"
           href="{{ `tag/` | relLangURL }}{{ .tags | urlize | lower }}">{{ .tags | title | humanize }}<span>{{ $tagCount }}</span></a>
      </li>
    {{- end }}
  {{ end }}
{{ end }}

The code throws no errors, but produces wrong figures that I don’t fully understand.
Some tags are listed in amounts that exceed existing, other items as many times as there are items, like
Cars 4
Cars 4
Cars 4
Cars 4

Can anyone help me what’s wrong here and how to access nested tags and count them?
Thanks!

“len” in the code example above count the characters in the “tags” string value.

{{ "cars" | len }} → 4

See the docs:

Thank you @frjo, you are right.
Here is code that ranges .Site.Pages, .Params.imgs, and lists the tags one by one, but I also need to count them and output a tag and its count.

{{- range .Site.Pages }}
  {{ range .Params.imgs }}
    {{- if isset . "tags" }}
      <li class="small">
        <a class="cat-item d-flex justify-content-between text-dark px-1"
           href="{{ `tag/` | relLangURL }}{{ .tags | urlize | lower }}">{{ .tags | title | humanize }}<span>1</span></a>
      </li>
    {{- end }}
  {{ end }}
{{ end }}

Next Hugo complains that range can not iterate over a tag name that is just a string. This happens because the range function expects an iterable value, such as an array, but I am trying to pass it a single string. To fix this, I need to convert the string value into an array before passing it to the range function. This leads to something like:

{{- range .Site.Pages }}
  {{ range .Params.imgs }}
    {{- if isset . "tags" }}
      {{- if not (eq (len .tags) 0) }}
        {{- $tagNames := split "," .tags }}  {{- range $tagName := $tagNames }}
          <li class="small">
            <a class="cat-item d-flex justify-content-between text-dark px-1"
              href="{{ `tag/` | relLangURL }}{{ $tagName | urlize | lower }}">{{ $tagName | title | humanize }}<span>{{ len ( ( where $.Site.Pages .Content.tags | has $tagName ) ) }}</span></a>
          </li>
        {{- end }}
      {{- end }}
    {{- end }}
  {{ end }}
{{ end }}

which does not work in Hugo.
Maybe someone knows how to fix it using inbuilt Hugo means?

Post a link to your repo and explain what it is exactly you want to count. Give us an example post and the value you expect.

Which is a bit convoluted. I’d rather put the tags into an array. That way, you don’t have to split a string at commas and all that and can directly iterate over tags.

Apart from that, I’m pretty confident that split does what it’s supposed to do. But you should read its documentation again and verify that you pass it the parameters in the correct order.

I don’t have a repo - will set it up when I have more time, but will try to explain what I want to count.

For instance this is a marketplace where people buy various types of products.
If the category is Cars, the tags would be subcategories, like BMW, Volvo etc.
This would allow a more specific search.

So far I’ve solved the issue by moving the ‘tags’ into the top-level.
But the nested level is still very interesting. Maybe someone will find the clue.

---
date: 
draft:
categories: cars
prods:
  - itemname: 
    itemdescription: ""
    tags: Volvo
    images:
      - ""
email: 
---

And you want to use static pages for that, one page per item? I think there might be a reason why marketplaces usually go for pages dynamically generated from a database (which can be queried a lot more flexible than static pages, I believe).