Skip to content
Snippets Groups Projects
Unverified Commit 38205eae authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Add an asset() function to twig.

This function can be used to load an asset from a twig template, adding a "tag" parameter to it with a few dynamic bytes depending on the version of SimpleSAMLphp, or the last modification time of the file if we are running master. This behaves as a cache-buster, forcing browsers to reload assets when a new version of SimpleSAMLphp gets installed.
parent 0bc66fe1
No related branches found
No related tags found
No related merge requests found
......@@ -143,6 +143,29 @@ class Template extends Response
}
/**
* Return the URL of an asset, including a cache-buster parameter that depends on the last modification time of
* the original file.
*
* @param string $asset
* @return string
*/
public function asset($asset)
{
$file = $this->configuration->getBaseDir().'www/assets/'.$asset;
if (!file_exists($file)) {
// don't be too harsh if an asset is missing, just pretend it's there...
return $this->configuration->getBasePath().'assets/'.$asset;
}
$tag = $this->configuration->getVersion();
if ($tag === 'master') {
$tag = substr(hash('md5', filemtime($file)), 0, 5);
}
return $this->configuration->getBasePath().'assets/'.$asset.'?tag='.$tag;
}
/**
* Get the normalized template name.
*
......@@ -289,6 +312,9 @@ class Template extends Response
)
);
// add an asset() function
$twig->addFunction(new \Twig_SimpleFunction('asset', [$this, 'asset']));
if ($this->controller) {
$this->controller->setUpTwig($twig);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment