Iterate over all files in Twig, not just media

I have a template that lists media files of a directory in a table and allow downloading them this way. Up to now I just used it for pdf documents and it worked quite well.

Now I want to extend this to also show non-media files, e.g. LibreOffice odt documents and unknown media formats, e.g. mkv containers. At the moment it doesn’t even work with mp3 files, although they should be detected as media. I didn’t change anything in the Grav default media.yaml config.

A simple

      {% for file in page.media.files %}
      <p>{{ file }}</p>
      {% endfor %}

only shows known media files (e.g. pdf or svg), but no odt/mp3/mkv.

How can I achieve this?

Can’t check for sure right now, but I think it’s page.media.all you’re looking for

1 Like

@Karmalakas
Unfortunately page.media.all doesn’t work.

Try debugging what’s in page.media and you will quickly see which key holds all of the attachments to page. Sadly I currently don’t have Grav set up on my fresh PC install so I could check myself

{{ dump(page.media) }} shows me

  #path: "/home/www/test/user/pages/randompath"
  #images: []
  #videos: []
  #audios: []
  #files: array:3 [
    "wappen-1.svg" => Grav\Common\Page\Medium\VectorImageMedium {#1126}
    "wappen-2.svg" => Grav\Common\Page\Medium\VectorImageMedium {#1127}
    "wappen-3.svg" => Grav\Common\Page\Medium\VectorImageMedium {#1128}
  ]
  #media_order: []
  -iteratorUnset: false
  -_upload_defaults: array:7 [
    "self" => true
    "avoid_overwriting" => false
    "random_name" => false
    "accept" => array:1 [
      0 => "image/*"
    ]
    "limit" => 10
    "filesize" => null
    "destination" => null
  ]

in the debugbar. Additionally in items I can see some details about the 3 svg files, but nothing else. It seems strange that it says accept => image/*. On the other hand it also shows the same, if I only have pdfs in the folder, but the pdfs are properly iterated.

Edit: I just saw that the accept => image/* is for file uploading.

With an entry like that

  mkv:
    type: file
    thumb: media/thumb-mkv.png
    mime: video/x-matroska

in media.yaml it works now. type: video doesn’t work.

1 Like