Lego2012
2/7/2017 - 8:56 PM

Loop over all images in a folder

Loop over all images in a folder

<!--
My images are in a assets/images/slider directory.
-->

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

<!--
The image.path contains 'images/slider' makes sure that only images in that folder are inserted.
-->

<!--
Listing the jpg files in the current directory in Jekyll can also be done like this:
-->

{% for file in site.static_files %}
  {% assign pageurl = page.url | replace: 'index.html', '' %}
  {% if file.path contains pageurl %}
    {% if file.extname == '.jpg' or file.extname == '.jpeg' or file.extname == '.JPG' or file.extname == '.JPEG' %}
    <img src="{{ file.path }}" />
    {% endif %}
  {% endif %}
{% endfor %}