Yii安装EClientScript插件扩展实现css,js文件代码压缩

扩展插件下载地址,解压后复制到/protected/vendor/

https://github.com/muayyad-alsadi/yii-EClientScript

main配置文件配置插件,components里面增加

//js,css代码压缩,合并 'clientScript' => array( 'class' => 'application.vendor.yii-EClientScript.EClientScript', 'combineScriptFiles' => TRUE, // By default this is set to true, set this to true if you'd like to combine the script files 'combineCssFiles' => TRUE, // 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 ),

工具类Unit.php放于/protected/vendor/components,类中定义加载方法

/** * 注册JS 文件 */ public function jsFile($file,$position=CClientScript::POS_HEAD,$media=array()){ $cs=Yii::app()->getClientScript(); $cs->registerScriptFile($file,$position,$media); } /** *注册CSS文件 */ public function cssFile($file,$media=''){ Yii::app()->getClientScript()->registerCssFile($file,$media); }

模板调用css文件,js文件

<?php //注册CSS文件, Unit::cssFile('https://www.jb51.net/css/home/base.css'); //result to:<link type="text/css" href="https://www.jb51.net/css/home/base.css" /> //IE6下加载CSS文件 Unit::cssFile('https://www.jb51.net/css/form.css','lte IE 6'); //result to:<!--[if lte IE 6]><link type="text/css" href="https://www.jb51.net/css/form.css" /><![endif]--> //注册JS文件, Unit::jsFile('https://www.jb51.net/js/jquery.lazyload.js'); //result to:<script src="https://www.jb51.net/js/jquery.lazyload.js"> //IE9下加载JS文件 Unit::jsFile('https://www.jb51.net/js/common.js', CClientScript::POS_HEAD, array('media' => 'lt IE 9')); //result to:<--[if lt IE 9]><script src="https://www.jb51.net/js/common.js"><![endif]--> ?>

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/28dc6b3b00933963fbec37ecdf290799.html