# Macro for image carousel

**URL:** <https://zola.discourse.group/t/macro-for-image-carousel/2529>\
**Category:** Support\
**Created:** [March 16, 2025, 2:09pm UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529 "2025-03-16T14:09:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alirezaalavi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@alirezaalavi](https://zola.discourse.group/u/alirezaalavi)\
**Post date:** [March 16, 2025, 2:09pm UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529/1 "2025-03-16T14:09:55Z")

</div>

Hi guys,

I want to create a macro for image carousel, which takes the path of a directory, and renders the image of that directory in the carousel.

I have created the macro with static images but I don’t know how I can read the filenames within a directory.

this is how the simple carousel looks

```auto
{% macro image_carousel(directory_path="") %}
<script type="text/javascript" defer src="/js/carousel.js"></script>
<div class="carousel">
    <img
        loading="lazy"
        src="/images/screenshots/2024-12-07_12-57-55.png"
    />
    <img
        loading="lazy"
        src="/images/screenshots/Screenshot_20220105_173608.png"
    />
{% endmacro image_carousel %}

```

The only needed thing is to loop `<img>` and replace `src` with filenames.

---

<div class="post-metadata">

**Author:** ![adrianboston](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/adrianboston/32/907_2.png) [@adrianboston](https://zola.discourse.group/u/adrianboston)\
**Post date:** [April 11, 2025, 12:48am UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529/2 "2025-04-11T00:48:45Z")

</div>

Pretty certain you cannot read filenames within a directory.  
Perhaps the best is cleaving path and filenames

> {% macro image\_carousel(path, files) %}  
> …  
> for x in files

Use in a template

> {{image\_carousel(path = “/images/screenshots/”, files=[“2024-12-07\_12-57-55.png”, “Screenshot\_20220105\_173608.png”])}}

alternatively pass a csv string

---

<div class="post-metadata">

**Author:** ![alirezaalavi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@alirezaalavi](https://zola.discourse.group/u/alirezaalavi)\
**Post date:** [April 12, 2025, 8:45am UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529/3 "2025-04-12T08:45:38Z")

</div>

Thank you. this is an option, although having a carousel of let’s say 10 images gets pretty messy, but it’s still better than hardcoding them.

---

<div class="post-metadata">

**Author:** ![alirezaalavi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@alirezaalavi](https://zola.discourse.group/u/alirezaalavi)\
**Post date:** [April 12, 2025, 9:15am UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529/4 "2025-04-12T09:15:09Z")

</div>

Thank you very much adrian.  
This solution fixed my problem. But I still think there should be the ability to read files within a directory. It is quite useful.

---

<div class="post-metadata">

**Author:** ![adrianboston](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/adrianboston/32/907_2.png) [@adrianboston](https://zola.discourse.group/u/adrianboston)\
**Post date:** [April 14, 2025, 6:53pm UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529/5 "2025-04-14T18:53:43Z")

</div>

Imho one would need to write a Tera extension in rust. 🦀

> **[tera - Rust](https://docs.rs/tera/latest/tera/)**
>
> Tera

> **[Tera](https://keats.github.io/tera/docs/#advanced-usage)**
>
> Tera - A template engine for Rust inspired by Jinja2 and Django templates

---

<div class="post-metadata">

**Author:** ![adrianboston](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/adrianboston/32/907_2.png) [@adrianboston](https://zola.discourse.group/u/adrianboston)\
**Post date:** [April 14, 2025, 7:47pm UTC](https://zola.discourse.group/t/macro-for-image-carousel/2529/6 "2025-04-14T19:47:35Z")

</div>

An even easier solution is renaming your files using an index for the carousel

```auto
images/carousel_1/
                               image_001.png
                               image_002.png

images/carousel_2/
                               image_001.png
                               image_002.png

```

I now use .webp for almost all images.

Heres a shell script for converting filenames

> <https://superuser.com/questions/1842886/adding-001-002-003-etc-to-all-files-in-the-same-folder>
