picocms / Pico

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

How to implement a version of the files #502

Closed SciCrea closed 4 years ago

SciCrea commented 5 years ago

Hello, how can you implement the .css version of the files. For example: style.css?v=534634

As I understand, you need to do it through filectime, are there ready-made solutions?

PhrozenByte commented 5 years ago

I guess you're rather looking for filemtime() than filectime(). Anyways, AFAIK there's no plugin yet, however, creating one is super simple. Try the following (untested):

class PicoAssetsModPlugin extends AbstractPicoPlugin
{
    const API_VERSION = 2;

    public function onTwigRegistered(Twig_Environment &$twig)
    {
        $pico = $this->getPico();
        $twig->addFilter(new Twig_SimpleFilter('asset', function ($file) use ($pico) {
            $file = str_replace('\\', '/', $file);
            $fileParts = explode('/', $file);

            $assetParts = array();
            foreach ($fileParts as $filePart) {
                if (($filePart === '') || ($filePart === '.')) {
                    continue;
                } elseif ($filePart === '..') {
                    array_pop($assetParts);
                    continue;
                }
                $assetsParts[] = $filePart;
            }

            $asset = implode('/', $assetParts);
            $timeSuffix = file_exists($pico->getRootDir() . $asset) ? '?v=' . filemtime($pico->getRootDir() . $asset) : '';
            return $pico->getBaseUrl() . $asset . $timeSuffix;
        }));
    }
}
SciCrea commented 5 years ago

Thank you! Tell me please how Include this plugin?

PhrozenByte commented 5 years ago

Just create a plugins/PicoAssetsModPlugin.php :+1:

SciCrea commented 5 years ago

I guess I didn't put it right. :) What's the challenge with what settings are in the template? {{ PicoAssetsModPlugin | asset }} The path of the root of the site is displayed. And how to point the way to the desired file? How do you generally use your plugin?

PhrozenByte commented 5 years ago

The Twig filter takes a path to a file, to include e.g. style.css of your theme, use:

<link rel="stylesheet" type="text/css" href="{{ "themes/" ~ config.theme ~ "/style.css"|asset }}"/>
SciCrea commented 5 years ago

Result: <link rel="stylesheet" type="text/css" href="themes/defaulthttp://site.com/default/?v=1561794422"/>

PhrozenByte commented 5 years ago

Whoops, small typo in the code ($assetsParts instead of $assetParts). As I've said, untested :smile: And I guess we should switch file_exists() with is_file(), adding a version suffix for folders doesn't make much sense...

<?php

class PicoAssetsModPlugin extends AbstractPicoPlugin
{
    const API_VERSION = 2;

    public function onTwigRegistered(Twig_Environment &$twig)
    {
        $pico = $this->getPico();
        $twig->addFilter(new Twig_SimpleFilter('asset', function ($file) use ($pico) {
            $file = str_replace('\\', '/', $file);
            $fileParts = explode('/', $file);

            $assetParts = array();
            foreach ($fileParts as $filePart) {
                if (($filePart === '') || ($filePart === '.')) {
                    continue;
                } elseif ($filePart === '..') {
                    array_pop($assetParts);
                    continue;
                }
                $assetParts[] = $filePart;
            }

            $asset = implode('/', $assetParts);
            $timeSuffix = is_file($pico->getRootDir() . $asset) ? '?v=' . filemtime($pico->getRootDir() . $asset) : '';
            return $pico->getBaseUrl() . $asset . $timeSuffix;
        }));
    }
}

Twig's operator precedence sometimes puts a spoke in one's wheels, you must put the string concat in brackets:

<link rel="stylesheet" type="text/css" href="{{ ("themes/" ~ config.theme ~ "/style.css")|asset }}"/>
SciCrea commented 4 years ago

Well done! It's working! Thank you! :) I think this solution can be used in the overall collection of Pico Plugins :) The functionality is useful.

PhrozenByte commented 4 years ago

Good point @SciCrea :+1:

Done, see https://github.com/picocms/Pico/wiki/Pico-Plugins resp. https://gist.github.com/PhrozenByte/fa97d431984b76b6a0476b3af94273ae

SciCrea commented 4 years ago

That's great news! Unfortunately, there is not a single convenient Sitemap plugin. Maybe you should make a fork: https://github.com/DaveKin/Pico_Sitemap/

And add a ready-made PicoAssetsModPlugin solution to the latest file change to filemtime ()

PhrozenByte commented 4 years ago

Check out the official PicoRobots plugin.

stale[bot] commented 4 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: