Skip to content

Instantly share code, notes, and snippets.

@deanrather
Last active December 18, 2015 01:19
Show Gist options
  • Save deanrather/5703377 to your computer and use it in GitHub Desktop.
Save deanrather/5703377 to your computer and use it in GitHub Desktop.
CacheBusting function for Nutshell
<?php
// in the controller:
$this->view->setTemplate('index');
$scope = $this;
$this->view->getContext()->registerCallback
(
'cacheBustFilename',
function($publicPath) use ($scope)
{
$publicDir = Nutshell::getInstance()->config->application->dir->publicDir;
$pathToFile = $publicDir . $publicPath;
if(!is_readable($pathToFile))
{
throw new NutshellException("can't add script. file doesn't exist", $pathToFile);
}
$time = filemtime($pathToFile);
return $publicPath . '?v=' . $time;
}
);
$this->view->render();
// in the view:
<script type="text/javascript" src="<?php echo $tpl->cacheBustFilename('/path/to/file.js'); ?>"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment