Last active
December 19, 2015 09:49
-
-
Save zulfajuniadi/5935950 to your computer and use it in GitHub Desktop.
PHP Compile Handlebars Templates on Modification
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 | |
class HandlebarsCompile | |
{ | |
private static $tmpFile = 'cache/handlebarscompile.txt'; | |
private static $templateFiles = 'views/templates/*'; | |
private static $outputFile = 'assets/js/templates.js'; | |
public static function Check() { | |
$tmpFile = self::$tmpFile; | |
if(!file_exists($tmpFile)) { | |
file_put_contents('hbs', "#!/bin/bash\nPATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin\nhandlebars $1 -f $2 -m"); | |
chmod('hbs', 0777); | |
touch($tmpFile); | |
} | |
$tmpStat = stat($tmpFile); | |
$lastmod = $tmpStat['mtime']; | |
$dirty = FALSE; | |
foreach (glob(self::$templateFiles) as $file) { | |
if(filemtime($file) > $lastmod) { | |
$dirty = TRUE; | |
} | |
} | |
if($dirty) { | |
touch($tmpFile); | |
exec('./hbs "'.self::$templateFiles.'" "'.self::$outputFile.'"'); | |
} | |
} | |
} | |
HandlebarsCompile::Check(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment