Jekyll - 用前题数据创建一个画廊

Jekyll - creating a gallery with front matter data

我想根据 post 封面中的数据创建 "list" 个标签,以与灯箱插件一起使用。

所以,可以说我的 post 前言如下:

gallery: true
images:
 - name: image-1.jpg
   alt: image-1
 - name: image-2.jpg
   alt: image-2
 - name: image-3.jpg
   alt: image-3

我想遍历该数据并创建以下 html:

<img id="thumb01" class="thumbnail" src="/assets/images/image-1.jpg" data-src="/assets/images/image-1.jpg" data-prev="thumb03" data-next="thumb02" alt="image-1">
<img id="thumb02" class="thumbnail" src="/assets/images/image-2.jpg" data-src="/assets/images/image-2.jpg" data-prev="thumb01" data-next="thumb03" alt="image-2">
<img id="thumb03" class="thumbnail" src="/assets/images/image-3.jpg" data-src="/assets/images/image-3.jpg" data-prev="thumb02" data-next="thumb01" alt="image-3">

我想在 post 布局中插入以下内容:

{% if page.gallery %}
some type of loop
{% endif %}

但我对如何实现这一点一无所知, 请帮忙!
谢谢!

试试这个:

{% if page.gallery == true %}
  {%- for img in page.images -%}

    {% comment %}
      If we have more than one image,
      we calculate next and prev index
    {% endcomment %}
    {% if forloop.length > 1 %}
      {% if forloop.first %}
        {% assign prev = forloop.length %}
      {% else %}
        {% assign prev = forloop.index | minus: 1 %}
      {% endif %}

      {% if forloop.last %}
        {% assign next = 1 %}
      {% else %}
        {% assign next = forloop.index | plus: 1 %}
      {% endif %}
    {% endif %}

<img id="thumb{{ forloop.index }}" class="thumbnail"
     src="{{ site.basurl }}/assets/images/{{ img.name }}"
     data-src="{{ site.basurl }}/assets/images/{{ img.name }}"
     {% comment %} only necessary if we have more than one image {% endcomment %}
     {%- if forloop.length > 1 -%}
     data-prev="thumb{{ prev }}"
     data-next="thumb{{ next }}"
     {%- endif %}
     alt="{{ img.alt }}" >

  {%- endfor -%}
{% endif %}