Jekyll - 获取目录中的所有文件夹并生成页面

时间:2017-07-13 21:16:20

标签: jekyll jekyll-extensions

我有一个像这样的文件夹结构:

/gallery/images/category1/
/gallery/images/category2/
/gallery/images/category3/
/gallery/images/category4/
/gallery/images/category5/

and so on..

在这些文件夹中,有一堆图像。但是,这些类别文件夹总是在变化,名称也是如此。

我的目标是让jekyll自动为这些类别中的每一个生成一个单独的页面。然后在此页面中,循环浏览该文件夹中的每个图像并将其显示在页面上。

我需要帮助:

  • 如何查看/gallery/images文件夹并抓取所有文件夹
  • 一旦我知道了所有文件夹,您如何为每个文件夹生成/gallery/[FOLDER_NAME].html等页面

一旦我能够做到这一点,我知道我可以将以下内容作为页面的内容并且没问题。

{% for image in site.static_files %}
    {% if image.path contains 'gallery/{{ FOLDER_NAME }}' %}
        <img src="{{ site.baseurl }}{{ image.path }}" />
    {% endif %}
{% endfor %}

非常感谢任何帮助或指导。

1 个答案:

答案 0 :(得分:1)

您可以使用javascript并且不使用扩展程序来执行此操作。

步骤1.使用页面或布局将它们放在屏幕上。

<div id="cats"></div>
<div class="imagecontainer">
{% for image in site.static_files %}
    {% if image.path contains 'gallery/' %}
        {% assign imagepath = image.path | split: "/" %}
        <img src="{{ site.baseurl }}{{ image.path }}" class="
          {% for subpath in imagepath %}
            {% if forloop.index0 == 3 %}{{ subpath }}{% endif %}
          {% endfor %}
        " />
    {% endif %}
{% endfor %}
</div>

第2步。让javascript一个一个地显示/隐藏它们。

/* hide all images */
$('.imagecontainer img').hide();

/* define an array for the categories */
var cats = new Array();

/* fill the array with the categories */
$('.imagecontainer img').each(function() {
  cats[$(this).attr('class')] = 1;
});

/* create a link for each category */
$.each(cats, function(cat, value) {
  $('#cats').append('<a href="#" class="showcat" cat="'+cat+'">'+cat+'</a>');
});

/* make the links toggle the right images */
$('.showcat').click(function() {
  /* hide all images */
  $('.imagecontainer img').hide();
  /* show images in the clicked category */
  $('.imagecontainer img.'+$(this).attr('cat')).show();
});

/* make images rotate */
$('.imagecontainer img').click(function() {
  /* append the clicked image to its container */
  $('.imagecontainer').append($(this));
});

使用此CSS:

div#cats {}
div.imagecontainer {}
img {position: absolute}

您可以首先使用data-src而不是src来阻止它们加载图像,并使用javascript / jQuery交换这些属性。

关于Liquid split的文档:https://shopify.github.io/liquid/filters/split/

相关问题