Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Created July 14, 2019 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhrozenByte/fa97d431984b76b6a0476b3af94273ae to your computer and use it in GitHub Desktop.
Save PhrozenByte/fa97d431984b76b6a0476b3af94273ae to your computer and use it in GitHub Desktop.
A simple Pico plugin registering a Twig filter to add a asset's modification time. Pico is a stupidly simple, blazing fast, flat file CMS. http://picocms.org/
<?php
/**
* Pico assets modification time plugin
*
* Registers a Twig filter to add a asset's modification time. Pass a path
* to a file and it will return its corresponding URL with a time suffix.
*
* Example:
*
* ```twig
* <link rel="stylesheet" type="text/css" href="{{ ("themes/" ~ config.theme ~ "/style.css")|asset }}"/>
* ```
*
* @author Daniel Rudolf
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
* @version 0.0.1
*/
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;
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment