Obtaining a list of files in a static folder without an index?

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?

Not currently no

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:

[
"icon/colas-avatar-32.png",
"icon/colas-avatar-64.png",
"icon/colas-avatar-128.png",
"icon/colas-avatar-256.png"
]

A shortcode template can then be test_assets.html:

{% set images = load_data(url=url, format="json") %}

<div class="gallery">
  {% for img in images %}
    <img src="{{ get_url(path=img) }}" alt="Asset Image" loading="lazy">
  {% endfor %}
</div>

That you would use in a topic as:

{{ test_assets(url="http://localhost:6543/my-list-of-images") }}

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.

Yes, running a server was just for the fun of pushing the exploration to the extreme.

Just running a pre-build script is the obvious solution, with thousands of ways to implement it.

And I think most of us already use some pre and/or post build scripts anyway.

For instance, I build my site via a script that:

  • perform lots of consistency & coverage checks
  • create a version file to be included in footer
  • git commit
  • zola build
  • deploy to the server