Skip to content

Instantly share code, notes, and snippets.

@zulfajuniadi
Last active December 19, 2015 09:49
Show Gist options
  • Save zulfajuniadi/5935950 to your computer and use it in GitHub Desktop.
Save zulfajuniadi/5935950 to your computer and use it in GitHub Desktop.
PHP Compile Handlebars Templates on Modification
<?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