picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.82k stars 616 forks source link

Creation & modification time of file? #455

Closed omniperspective closed 5 years ago

omniperspective commented 5 years ago

Question:

Is picocms able to give me the creation and modification time of a file, is there a { page.??? } variable that holds that data? Can't find it in the twig documentation other than changing the format. Hense is there a possibility to use a unix library call to get info into the picocms framework? or do I have to write a plugin to get this done.

regards Henk.

PhrozenByte commented 5 years ago

You'll have to create a plugin to get this done - but don't despair, it's pretty simple. Just take the following few lines (taken from https://github.com/PhrozenByte/pico-robots):

class PicoModTimePlugin extends AbstractPicoPlugin
{
    public function onSinglePageLoaded(array &$pageData)
    {
        if ($pageData['id']) {
            $fileName = $this->getConfig('content_dir') . $pageData['id'] . $this->getConfig('content_ext');
            if (file_exists($fileName) && !isset($pageData['modificationTime'])) {
                $pageData['modificationTime'] = filemtime($fileName);
            }
        }
    }
}

You can then access the modification time of a page's content file using {{ page.modificationTime|date("m/d/Y") }}. Please note that this might not be what you actually want, the modification time might geht updated by your operating system even if you didn't change anything. Thus I'd recommend using a meta header instead.

Unfortunatly there's no way to tell when a file has been created on Linux (to be precise, whether it is possible or not depends on the filesystem you use, and if it is possible, the code necessary to access this information might vary). I again recommend using a meta header instead (e.g. Pico's default Date meta header).

omniperspective commented 5 years ago

Thanks Phrozenbyte for the extended reply, as always great support, and a good start for me.

This will be my first project to build a PicoCMS plugin.

kind regards Henk.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1: