How to list files in a directory with Liquid?

jekyll, liquid

Solution

edit (2015-11-09): Jekyll has since gotten some updates, especially `site.static_pages`. Check @raphael answer for a possible solution.

It is impossible without using a plugin. You do not have I/O capabilities inside Liquid Templates.

If your images have front matter, they might get processed by Jekyll and be stored in the site.pages array and thus being accessible, but I wouldn't recommend placing Front Matter on images (might make sense for SVGs, but not for anything else).

Your best bet is probably to write a little shell script that scans your folder for images and creates an images.json file. This file could then be loaded via Ajax. You'd just have to recreate your images.json file every time you upload a new file (if you are using Git, you could to that as a pre-commit hook).

Problem

I'm trying to learn how to use Jekyll along with Bootstrap; while studying them, I decided that I'd like to have an image carousel on my homepage. Since I'm really lazy I don't want to hard-code the paths needed to display every image inside the layout, and I wouldn't either prefer to use an array to store the list of images. I was wondering if there's any tag that could ask Jekyll to do these two steps: - Look inside a specific directory - For each file you found in that directory, repeat a block of code Basically what I'd like to write is something that vaguely resemble this piece of (imaginary) code: ``` {% for file in directory %} <img src="{{ file.url }}" /> {% endfor %} ``` So if, for example, I have a folder with three files named image01.jpg, image02.jpg, image03.jpg, I'd like that jekyll could build this HTML code for me: ``` <img src="folder/image01.jpg" /> <img src="folder/image02.jpg" /> <img src="folder/image03.jpg" /> ``` So I had a look at this reference page but I haven't found anything useful for my purpose. Please, could you give me any suggestion, and if possible, one that doesn't involve the use of a plugin? Thank you in advance.

Original source