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

bry*_*yan 5 jekyll jekyll-extensions

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

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

and so on..
Run Code Online (Sandbox Code Playgroud)

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

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

我需要什么帮助:

  • 如何查看/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 %}
Run Code Online (Sandbox Code Playgroud)

任何帮助或指导将不胜感激.

Joo*_*stS 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>
Run Code Online (Sandbox Code Playgroud)

步骤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));
});
Run Code Online (Sandbox Code Playgroud)

使用这个CSS:

div#cats {}
div.imagecontainer {}
img {position: absolute}
Run Code Online (Sandbox Code Playgroud)

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

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