I know that I can loop through colocated assets by interrogating section.assets or page.assets.
However, I’m working on a project that collects related images into their own static folders and I’m trying to obtain that list of images without needing an associated index file.
Is it currently possible in Zola to get a list of files from an arbitrary folder?
OK, thanks for the reply. Next question then: could Zola support this in the future? It would be an extremely useful feature to interrogate arbitrary folders (although I understand there could be context, security or os-level considerations).
EDIT: Looks like this request was previously made here:
By looking at this issue, I realized that Zola has a way to actually iterate on a dynamically-created list of files: dynamically load the list from an URL.
This is not a direct answer to your question, but I guess it is a way to implement a workaround today in a somewhat forward-compatible way with possible future native Zola implementations.
All you need to do is to have a small local web server with a CGI script returning the list of files as a JSON list.
E.g:
Another workaround not requiring a local web server would be to have a pre-build script called that would add the list of files into a json list in the frontmatter
+++
...
[extra]
static_images = [
"foo/bar/screen1.png",
"foo/bar/screen2.png"
]
+++
{% for img_path in page.extra.static_images %}
...
{% endfor %}
Thanks for the suggestions. I’m not really a fan of adding extra dependencies or external build scripts, but I think in this case you’re right: it’s really the only way to do it. Generating a list of files using something like Python’s os.listdir() should work in this case.
IIRC load_data can be used with local files, including CSV, so you could just use ls > my_listingto create a listing file then load it as CSV. Does require a build script, of course, but not much of one.
Yes, I use load_data all the time with local files, the previous suggestion of running a server with a CGI script to generate a file list is not necessary.