Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Create a folder listing with twig

Started by Olivier Sens 9 years ago · 6 replies · 1334 views
9 years ago

Hello,
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 :

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

Any suggestion ?
thanks for your help

9 years ago

hi sensomusic

try this, it should work

example for the same folder of page:

TWIG
{% for file in page.media.all %}
    {{ file }} - {{ file.url }}
{% endfor %}

example for a selected folder:

TWIG
{% 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

9 years ago

hi,
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.
senso++++

9 years ago

hey,

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

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

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

last edited 10/27/17 by -
9 years ago

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

the following code doesn't work for me:

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

Not dramatic at all, I use the first option.

senso+++

9 years ago

okay,

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.

9 years ago

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

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 76 7 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago