Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

How to get file date/time in twig?

Solved by pamtbaau View solution

Started by M 6 years ago · 3 replies · 1517 views
6 years ago

With {% for file in page.media.files %} in Twig I can iterate over the files in a specific folder. Then I can access properties, like file.url, file.name, file.extension and file.size. How can I access the creation or modification date of a file?

6 years ago Solution

@myscha, Have you tried dumping the image object in Twig to reveal its properties:

TWIG
{{ dump(page.media['myimage.jpg']) }}

This will yield something like:

YAML
Array(1) object: Grav\Common\Page\Medium\ImageMedium
  *gettersVariable: "items"
  *items: Array(15)
    type: "image"
    filename: "myimage.jpg"
    basename: "myimage"
    extension: "jpg"
    modified: 1237805256   <-- Unixepoch date
    ...

And finally:

TWIG
{{ page.media['myimage.jpg'].modified | date('Y/m/d') }}
6 years ago

@pamtbaau:
{{ page.media['myimage.jpg'].modified | date('Y/m/d') }}

Thank you very much. Using
{{ file.modified | dateTranslate(grav.config.system.pages.dateformat.long) }}
even considers the configured time format.

6 years ago

@myscha, Nice!

By the way... Variable system is already defined in Twig as an alias to grav.config, like many others.

Suggested topics

Topic Participants Replies Views Activity
Support · by TomW, 1 day ago
1 42 22 hours ago
Support · by TomW, 1 day ago
0 48 1 day ago
Support · by TomW, 6 days ago
6 132 2 days ago
Support · by Anna, 2 weeks ago
2 165 6 days ago
Support · by Anna, 4 weeks ago
1 333 4 weeks ago