Created
April 5, 2016 17:49
-
-
Save lorenzulrich/ec8e37bbdd0d31e4a052d35409d8aff3 to your computer and use it in GitHub Desktop.
ViewHelper used to return a version number of a resource based on the MD5 hash of a file.
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 | |
namespace Acme\Demo\ViewHelpers; | |
/** | |
* ResourceBuiltVersion | |
* | |
* ViewHelper used to return a version number of a resource based on the MD5 hash of a file. | |
* | |
* Useful to prevent caching issues with static resources. | |
* | |
* Usage: | |
* | |
* <acme:resourceBuiltVersion name="resource://Acme.Demo/Public/Scripts/Build/headScripts.js" /> | |
* | |
* Inline usage for loading a static resource: | |
* | |
* <script src="resource://Acme.Demo/Public/Scripts/Build/headScripts.js?{acme:resourceBuiltVersion(name:'resource://Acme.Demo/Public/Scripts/Build/headScripts.js')}"></script> | |
*/ | |
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper; | |
class ResourceBuiltVersionViewHelper extends AbstractViewHelper | |
{ | |
/** | |
* @param string $name | |
* @return mixed | |
*/ | |
public function render($name) | |
{ | |
if (file_exists($name)) { | |
return substr(md5_file($name), 0, 12); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment