Closed SciCrea closed 4 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;
}));
}
}
Thank you! Tell me please how Include this plugin?
Just create a plugins/PicoAssetsModPlugin.php
:+1:
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?
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 }}"/>
Result:
<link rel="stylesheet" type="text/css" href="themes/defaulthttp://site.com/default/?v=1561794422"/>
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 }}"/>
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.
Good point @SciCrea :+1:
Done, see https://github.com/picocms/Pico/wiki/Pico-Plugins resp. https://gist.github.com/PhrozenByte/fa97d431984b76b6a0476b3af94273ae
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 ()
Check out the official PicoRobots
plugin.
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:
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?