Create a folder listing with twig

I’m trying to create automatically the list of files in a folder,
for example in /page/downloads/ where there is pdf, zip, or whatever.

Then I want to display a direct link to the listed files almost like :

{% for file in  ??? | sort %}
  <li><a href="{{ file.url }}">{{ file.??? }}</a></li>
{% endfor %} 

Any suggestion ?
thanks for your help

hi sensomusic

try this, it should work

example for the same folder of page:

{% for file in %}
    {{ file }} - {{ file.url }}
{% endfor %}

example for a selected folder:

{% for file in pages.find('/images').media.all %}
    {{ file.url }}: {{ file.href }}
{% endfor %}

to select only images for example change media.all to media.images

Thanks a lot, the first example works if I put all the files in the current page folder. It does the trick.
It seems that page.find doesn’t work for a folder which contains only zip files and no .md files.
Thanks anyway, you found the soluce.


it is “pages.find” and not “page.find” , maybe this does the trick

i tested the second example with page.find and pages.find

and it should work on a folder with just zip-files

My folder is located in /user/pages/downloads and contains only zip files

the following code doesn’t work for me:

{% for file in pages.find('/downloads').media.all %}
    {{ file.url }}: {{ file.href }}
{% endfor %}

Not dramatic at all, I use the first option.



i could reproduce this, if i create the empty folder by hand on the filesystem and start to get the media - it dont work.

if i put a md file into the folder and delete it afterwards, it works. I cant tell you why it works, but it works on my setup.

What works for me all the time, is creating the folder through the admin.

Maybe this is the solution to your problem, i dont know.

That what I thought. The folder is not listed as a page by GRAV.
But I found a soluce, it’s great for me.