Mecha CMS

Code snippet collection for developers.

Static Time Field

Automatically generate static time data on the first page visit, so that updating the page files will not change its publication time.

Hook::set('enter', function() use($site) {
    $page = Lot::get('page');
    if ($page && $page->exist) {
        $time = Path::F($page->path) . DS . 'time.data';
        if (!$site->is('error') && !File::exist($time)) {
            File::put($page->time)->saveTo($time);
        }
    }
});

Note: The default shield uses $page->update to show the current date which will output the file modification time as the page date. To show the current page date created by this snippet, you can use $page->time or $page->date->{$format}:

<p><?php echo $page->date->en_us; ?></p>

Related: link broken