PHP | Working Example - Use Combined files for CSS and Javascript
<?php
class Page_Controller extends ContentController {
public function init() {
parent::init();
$combinepath = 'themes/'.SSViewer::current_theme().'/combined';
Requirements::set_combined_files_folder($combinepath);
$css = array();
$css [] = "themes/".SSViewer::current_theme()."/css/layout.css";
$css [] = "themes/".SSViewer::current_theme()."/css/form.css";
global $GMapApi;
Requirements::javascript("http://maps.google.com/maps?file=api&v=2&key=".$GMapApi."&hl=nl");
$js = array();
$js[] = 'sapphire/thirdparty/jquery/jquery-packed.js';
$js[] = 'mysite/javascript/roundedcorners.js';
$js[] = 'mysite/javascript/gmap.js';
$js[] = 'mysite/javascript/fn.js';
foreach($css as $c){
Requirements::css($c);
}
foreach($js as $j){
Requirements::javascript($j);
}
if(Director::isLive()){
Requirements::combine_files('combined.css',$css);
Requirements::combine_files('combined.js',$js);
Requirements::process_combined_files();
}
//compress the styles on flush :)
if(isset($this->request) && $this->request->getVar('flush') == 'all'){
if(file_exists(Director::baseFolder() . '/' .$combinepath.'/combined.css')){
$bf = file_get_contents(Director::baseFolder() . '/' .$combinepath.'/combined.css');
$bf = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $bf);
$bf = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $bf);
$fh = fopen(Director::baseFolder() . '/' .$combinepath.'/combined.css', 'w');
fwrite($fh, $bf);
fclose($fh);
}
}
}
}
Put the files to combine in a seperate array, to allow some files to be excluded.
|
|
| Tags : Combine files css javascript | |
27/10/2010 5:15am
PHP | Solution - John
$css [] = $this->ThemeDir() . '/css/form.css';
$js[] = THIRDPARTY_DIR . '/jquery/jquery-packed.js';
27/10/2010 5:12am
PHP | Solution - John
You may also want to check the file is writable before doing this operation.
if (isset($this->request) && $this->request->getVar('flush') == 'all') {
$file = Director::baseFolder() . '/' . Requirements::backend()->getCombinedFilesFolder() . '/combined.css';
if (file_exists($file) && is_writable($file)) {
$bf = file_get_contents($file);
$bf = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $bf);
$bf = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $bf);
$fh = fopen($file, 'w');
fwrite($fh, $bf);
fclose($fh);
}
}26/09/2010 4:17pm
PHP | Solution - Jörn
Hi,
I updated your script to work with custom combined-files-folders.
Other rules are here:
http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/CSS/Compressor.php
if (isset($this->request) && $this->request->getVar('flush') == 'all') {
$file = Director::baseFolder() . '/' . Requirements::backend()->getCombinedFilesFolder() . '/combined.css';
if (file_exists($file)) {
$bf = file_get_contents($file);
$bf = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $bf);
$bf = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $bf);
$fh = fopen($file, 'w');
fwrite($fh, $bf);
fclose($fh);
}
}
Here are some nicer ways to do the includes.