首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eleventy:输出集合中数据的子集

Eleventy:输出集合中数据的子集
EN

Stack Overflow用户
提问于 2020-08-18 18:31:00
回答 1查看 144关注 0票数 0

我有一个products集合,我希望从其中输出一系列图像。我的前置内容包括以下…

代码语言:javascript
复制
---
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'
---

然后我的部分人员就会打电话给传送带。

代码语言:javascript
复制
{% 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 -%}

但是,结果输出是否具有空的src和alt属性?我不明白为什么?我唯一的猜测是,这与我试图循环通过前面物质的子集的方式有关?

在相同的products集合中,我有一个在前面定义为…的主图像

代码语言:javascript
复制
img_main:
  image: '/assets/img/img.jpg'
  imageAlt: 'text'

使用以下模板…

代码语言:javascript
复制
{% 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 -%}

这就产生了想要的输出。

代码语言:javascript
复制
<figure class="outer-1by1">
  <img class="inner-1by1" src="/assets/img/img.jpg" width="160" height="143" alt="text" loading="lazy" />
</figure>

因此,我假设item.data.carousel.image可以工作?

EN

回答 1

Stack Overflow用户

发布于 2020-08-21 15:45:29

通过设置两个循环来工作,第一个循环遍历整个集合。第二,数据的子集(数组)。

代码语言:javascript
复制
{% set allProductPosts = collections.featuredProduct -%} // First we call all posts in the collection
<div class="product_carousel">
  <div class="product_carousel_featured">
    {% for item in allProductPosts -%} // We then loop through the collection
    {% for carousel in item.data.carousel %} // Then loop through the subset (array) of data
    <div class="product_carousel_cell">
      <figure class="outer-5by3">
        <img class="inner-5by3" src="{{ carousel.image }}" alt="{{ carousel.imageAlt }}" loading="lazy" />
      </figure>
    </div>
    {% endfor -%}
    {% endfor -%}
  </div>
</div>

感谢Raymond Camden将我推向一个解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63466768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档