Skip to content

Instantly share code, notes, and snippets.

@masudiiuc
Last active August 29, 2015 14:26
Show Gist options
  • Save masudiiuc/f77eb12766c823e48f4a to your computer and use it in GitHub Desktop.
Save masudiiuc/f77eb12766c823e48f4a to your computer and use it in GitHub Desktop.
html2js
#!/usr/bin/env php
<?php
function bundleHtml($file)
{
$html = file_get_contents($file);
$html = str_replace(array("\r\n", "\n", "\r"), " ", $html);
$html = str_replace("'", "\\'", $html);
$html = preg_replace("/\s+/", " ", $html);
return $html;
}
$dirIterator = new DirectoryIterator(__DIR__ . '/../templates');
$bundle = "Templates = {};\n";
foreach ($dirIterator as $file) {
if ($file->isFile()) {
$html = bundleHtml($file->getPathname());
$bundle .= "Templates." . $file->getBasename('.html') . " = '" . $html . "';\n";
}
if ($file->isDir()) {
if ($file != '.' && $file != '..') {
$fDirIterator = new DirectoryIterator(__DIR__ . '/../templates/'. $file);
foreach ($fDirIterator as $f) {
if ($f->isFile()) {
$html = bundleHtml($f->getPathname());
$bundle .= "Templates." . $f->getBasename('.html') . " = '" . $html . "';\n";
}
}
}
}
}
file_put_contents(__DIR__ . '/../build/templates.js', $bundle);
if (php_sapi_name() == 'cli') {
echo "Templates bundle created at build/templates.js for inclusion.", PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment