New global functions `get_files` or similar

Sometimes it’s useful to iterate over files to use them in templates. You can do this using page.assets, but this does not include files that are not colocated with that page.

We could have a get_files(path=<some path>) that returns a list of files in path. Optionally we could make that recursive.

Additionally, it would be useful for get_files to accept some glob, for example get_files(path=<some_path>, glob="*.jpg"), because otherwise looping over the returned vector might be a big cumbersome.

With the glob, if you want to iterate over images in static/mypost you could just do:

{% for asset in get_files(path=static/page.slug, glob="*.jpg") %}
{% if loop.index  is odd %}<div class="row">{% endif %}
  <div class="column">
    <a class="gallery-item" href="{{ get_url(path=asset) }}" >
      <img src="{{ get_url(path=asset) }}">
    </a>
  </div>
{% if loop.index is even or loop.last %}</div>{% endif %}  
{% endfor %}

Otherwise you would need to filter the elements inside a loop, which messes with the loop.* variables, which are sometimes useful for templating, you would have to write something like:

{% set_global idx = 0 %}
{% for asset in page.assets %}
{% if asset is matching("[.](jpg)$") %}
{% set_global idx = idx + 1 %}
{% if idx is odd %}<div class="row">{% endif %}
  <div class="column">
    <a class="gallery-item" href="{{ get_url(path=asset) }}" >
      <img src="{{ get_url(path=asset) }}">
    </a>
  </div>
{% if idx is even or loop.last %}</div>{% endif %}
{% endif %}
{% endfor %}

What do you think?

I think this is a better idea than https://github.com/getzola/zola/pull/931 but at the same time the workaround is not that bad so I don’t know if it’s useful enough to warrant being added.

You mean the second code sample above? That would only work with https://github.com/getzola/zola/pull/931 though.

If the assets are colocated with a post, why not put them next to the post?

Because of the reason you mention here: Overview | Zola

Depending on your organizational needs, this may be better or worse.

Basically my posts have high resolution photos that I don’t want to commit into git, would be easier if they would be in static in a separate folder so I can back them up and add them to .gitignore easily.

Hi,

This thread seems a bit old, but is there a way to do this now in Zola? I would like to render the contents of several directories in static as tables on the same page. Is this possible? Thanks