Extending client script
Yii 1.1.x or above, (*1)
EClientScript (an extension for Yii's CClientScript), (*2)
This is an optimizing client script manager for Yii framework, that can minify and combine CSS/JS files., (*3)
It will automatically detects changes in file content (based on modification time) and generates a unique file name accordingly., (*4)
This will reduce the HTTP calls for resources files by merging several resources filesinto a single (or more) files., (*5)
Since version of 1.5, also support conditional loading js/css file in IE browser, see example codes please., (*6)
The source code is hosted under github:, (*7)
https://github.com/muayyad-alsadi/yii-EClientScript, (*8)
CSS files are merged based on there media attribute, background images with a relative path in file can also be displayed correctly., (*9)
Script files are merged based on their position, If you use the 'CClientScript::POS_HEAD' you will end up with a single file for all the script files you've used on that page., (*10)
If you use 'CClientScript::POS_HEAD' and 'CClientScript::POS_END' for example then you'll end up with two files for each page on that request, becuase those resources are located in different positions., (*11)
CssMin used to optimize merged css file. You can set property 'optmizeCssFiles' of the component to enable this feature. JSMinPlus used to optimize merged script file. You can set property 'optmizeScriptFiles' of the component to enable this feature., (*12)
Using this extension is as simple as adding the following code to the application configuration under the components array:, (*13)
'clientScript' => array( 'class' => 'application.vendors.yii-EClientScript.EClientScript', 'combineScriptFiles' => !YII_DEBUG, // By default this is set to true, set this to true if you'd like to combine the script files 'combineCssFiles' => !YII_DEBUG, // By default this is set to true, set this to true if you'd like to combine the css files 'optimizeScriptFiles' => !YII_DEBUG, // @since: 1.1 'optimizeCssFiles' => !YII_DEBUG, // @since: 1.1 'optimizeInlineScript' => false, // @since: 1.6, This may case response slower 'optimizeInlineCss' => false, // @since: 1.6, This may case response slower ),
Then you can use the regular 'registerScriptFile' & 'registerCssFile' methods as normal and the files will be combined or optimized automatically., (*14)
Using to conditional loading js/css file for IE browser, you just need to specify the media property., (*15)
$cs = Yii::app()->clientScript; // result to: $cs->registerScriptFile('/js/html5.js', CClientScript::POS_HEAD, array('media' => 'lt IE 9')); // result to: $cs->registerCssFile('/css/ie.css', 'lte IE 6');
If you registered some external resource files that not in the web application root directory, they will be kept and not combined. Compression or optmization is a EXPERIMENTAL feature, please use it carefully(@since: 1.1), (*16)
Aug 13, 2013, (*17)
Aug 2, 2013, (*18)
Mar 29, 2013, (*19)
registerScript
and registerScriptFile
Mar 27, 2013 (by Muayyad Alsadi), (*20)
Dec 06, 2010, (*21)
@charset "xxx"
, it should be in the first line of file and not repeatly.Nov 23, 2010, (*22)
.pack.
Nov 6, 2010, (*23)
.min.
getRelativeUrl()
platform compatibility issue. (thanks to Troto)When some resource files can not be merged and they are strictly dependent on loading order, then may have some problems., (*24)
Reporting Issues and comments are welcome, please report issues to https://github.com/muayyad-alsadi/yii-EClientScript/issues, (*25)