如何在 Jekyll 中遍历变量数组中的键数组?

How to iterate through a key's array within a variable's array in Jekyll?

这是我设置页面首页的方法。

---

title: Test page
layout: default

image:
  - url: image01.png
    caption: 'Logo design'
    class: ['text-white']
    alt: 'An alt text'
  - url: image02.png
    caption: 'Website development'
    class: ['text-white', 'fullwidth-figure']
    alt: 'An alt text'

---

在页面布局中,这是我循环访问 image: 变量数组的方式。

{% for image in page.image %}
<figure class="{% if image.class contains 'fullwidth-figure' %}fullwidth-figure{% else %}contained{% endif %}">
<img src="{{ site.photoset_url }}/{{ image.url }}" alt="{{ image.alt }}">
<figcaption>
{{ image.caption | textilize }}
</figcaption>
</figure>
{% endfor %}

这从页面的关联数组的前端列表中获取了我需要的所有内容。到目前为止一切顺利,但我希望能够遍历 class: 键数组。

因此,如果我设置 class: ['text-white', 'fullwidth-figure'],我如何检查 "fullwidth-figure" 值以及如何 "print" 这两个值?

仅当 "fullwidth-figure" 不存在时,您才可以使用 join filter to print all classes (save yourself from a for-loop), then the unless tag 打印 "contained":

{% for image in page.image %}
<figure class="{{ image.class | join: ' ' }}{% unless image.class contains 'fullwidth-figure' %} contained{% endunless %}">
    <!-- image -->
</figure>
{% endfor %}