Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Smarty Booster Plugin
<?php
// tested on PHP 5.3 / Smarty 3.1
// Plugin wasn't exactly developed for convenience, more a proof of concept thingy
// This code is provided under the MIT License - http://www.opensource.org/licenses/mit-license.php
// define the (absolute) path to document_root
define('BOOSTER_HTDOCS', realpath(__DIR__ . '/../../htdocs/') . '/');
require_once BOOSTER_HTDOCS . 'booster/booster_inc.php';
function smarty_function_booster(array $params, Smarty_Internal_Template $template)
{
$root = $template;
$type = isset($params['type']) ? $params['type'] : null;
$media = isset($params['media']) ? $params['media'] : null;
$src = isset($params['src']) ? $params['src'] : null;
$output = isset($params['output']);
// sanity checks
if ($type !== 'css' && $type !== 'js') {
throw new Exception('{booster type="css|js"} - unknown type "'. $type .'"');
}
// TODO: allow arrays of src
if (!$output && (!$src || $src[0] !== '/')) {
throw new Exception('{booster src="/some/file.css"} - invalid src "'. $src .'"');
}
$filepath = BOOSTER_HTDOCS . substr($src, 1);
if (!file_exists($filepath)) {
throw new Exception('{booster src="'. $src .'"} file not found at "'. $filepath .'"');
}
// store stuff in the root template (NOT Smarty)!
while ($root->parent && $root->parent instanceof Smarty_Internal_Template) {
$root = $root->parent;
}
// create booster
$booster = $root->getTemplateVars('__booster');
if (!$booster) {
$booster = new Booster();
// TODO: import options from somewhere
$booster->js_minify = true;
$booster->css_totalparts = 1;
$root->assign('__booster', $booster);
}
// create cache
// Note: would've liked a reference, but can't count on getTemplateVars() allowing to return by ref :(
$cache = $root->getTemplateVars('__booster_cache');
if (!$cache) {
$cache = array(
'css' => array(),
'js' => array(),
);
}
// make sure paths are relative to the booster directory
// they are to be provided absolute from the doc root
// booster is supposed to be in the doc root
// so prepending .. should work fine
$src = '..' . $src;
$result = '';
if ($output) {
if ($type == 'css') {
if (empty($cache['css'][$media])) {
return $result;
}
$booster->css_source = $cache['css'][$media];
$cache['css'][$media] = array();
$booster->css_media = $media;
$result = $booster->css_markup();
} else {
$booster->js_source = $cache['js'];
$cache['js'] = array();
$result = $booster->js_markup();
}
} else {
if ($type == 'css') {
$cache['css'][$media][] = $src;
} else {
$cache['js'][] = $src;
}
}
$root->assign('__booster_cache', $cache);
return $result;
}
{* add some CSS files to the booster *}
{booster type="css" media="screen" src="/css/museo300.css"}
{booster type="css" media="screen" src="/css/screen.css"}
{booster type="css" media="screen" src="/css/keyvisuals.css"}
{* output boosted CSS *}
{booster type="css" media="screen" output=1}
{* add some Javascript files to the booster *}
{booster type="js" src="/js/prefixfree.min.js"}
{booster type="js" src="/js/jquery-1.7.1.min.js"}
{booster type="js" src="/js/jquery.cookie.js"}
{booster type="js" src="/js/jquery.placeholder.min.js"}
{booster type="js" src="/js/main.js"}
{* output boosted Javascript *}
{booster type="js" output=1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.