【问题标题】:Jekyll - creating a gallery with front matter dataJekyll - 创建一个包含前沿数据的画廊
【发布时间】:2018-08-01 15:27:07
【问题描述】:

我想从我的帖子前端的数据中创建一个标签“列表”,以便与灯箱插件一起使用。

所以,假设我在帖子前面有以下内容:

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">

我正在考虑在帖子布局中插入以下内容:

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

但我对如何实现这一点没有丝毫线索, 请帮忙!
谢谢!

【问题讨论】:

    标签: jekyll shopify liquid static-site jekyll-extensions


    【解决方案1】:

    试试这个:

    {% 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 %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      相关资源
      最近更新 更多