Last active
December 18, 2015 01:19
-
-
Save deanrather/5703377 to your computer and use it in GitHub Desktop.
CacheBusting function for Nutshell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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