How to get the number of page resources matching a specific wildcard?

I’m have several resources defined for each page. It’s usually one or more images, sometimes one video or a combination of both:

resources:
- name: image_1
  params:
    alt: "Alternate text."
  src: image-1.webp
# (...)
- name: video
  params:
    container: mp4
  src: video.mp4
# (...)

I’m accessing these resources from list.html with a range loop:

{{ range .Resources.Match "video*" }}
    <video controls preload="metadata" width="1280">
        <source src="{{ .RelPermalink }}" type="video/{{ $container }}">
    </video>
{{ end }}
<!-- (...) -->
<div class="gallery">
    {{ range .Resources.Match "image*" }}
        <a href="{{ .RelPermalink }}">
            {{ with .Resize "640x" }}
                <img src="{{ .RelPermalink }}" width="{{ .Width }} height="{{ .Height }} alt="{{ .Params.alt }}">
            {{ end }}
        </a>
    {{ end }}
</div>

What I’m trying to figure out is how to determine the number of image resources (image*) that each page has. Iif it has only one image, I need to scale it by a smaller factor. From what I understand, .Resources.Match returns a collection of resources matching a criteria. But how to count how many items are in that collection?

{{ .Resources.Match "image*" | len }}

https://gohugo.io/functions/len/

2 Likes

Perfect! Many thanks!

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