I have a products collection, from which I wish to output a carousel of images. My front matter includes the following…
---
carousel:
- image: '/assets/img/img.jpg'
imageAlt: 'text'
- image: '/assets/img/img-02.jpg'
imageAlt: 'text'
- image: '/assets/img/img-03.jpg'
imageAlt: 'text'
- image: '/assets/img/img-04.jpg'
imageAlt: 'text'
- image: '/assets/img/img-05.jpg'
imageAlt: 'text'
---
And then my partials calls the carousel.
{% for item in collections.products %}
<figure class="outer-5by3">
<img loading="lazy" class="inner-5by3" src="{{ item.data.carousel.image }}" alt="{{ item.data.carousel.imageAlt }}">
</figure>
{% endfor -%}
However, the resulting output has empty src and alt attributes? And I do not understand why? My only guess is that it has something to do with how I am trying to loop through the subset of front matter?
Within the same products collection, I have a main image defined in the front matter as…
img_main:
image: '/assets/img/img.jpg'
imageAlt: 'text'
With the following template…
{% for product in collections.products -%}
<figure class="outer-1by1">
<img class="inner-1by1" src="{{ product.data.img_main.image }}" width="160" height="143" alt="{{ product.data.img_main.imageAlt }}" loading="lazy" />
</figure>
{% endfor -%}
And this results in the desired output.
<figure class="outer-1by1">
<img class="inner-1by1" src="/assets/img/img.jpg" width="160" height="143" alt="text" loading="lazy" />
</figure>
Hence why I assumed item.data.carousel.image
would work?
carousel: [{ image: '/assets/img/KB09-01.jpg', imageAlt: 'High temperature KB09 beam style Stirling Engine' }, { image: '/assets/img/KB09-02.jpg', imageAlt: 'Rear view of beam Stirling Engine showing crank' }, { image: '/assets/img/KB09-03.jpg', imageAlt: 'KB09 Stirling engine showing conrod and beam' }, { image: '/assets/img/KB09-04.jpg', imageAlt: 'Front view displacer piston of Stirling engine' }, { image: '/assets/img/KB09-05.jpg', imageAlt: 'Top view of brass heatsink on the Stirling engine' }],
– Grant Smith