设为首页 加入收藏

TOP

Yii中引入js和css文件
2015-02-02 14:16:07 来源: 作者: 【 】 浏览:17
Tags:Yii 引入 css 文件

在视图层(../views/..)添加CSS文件或java script文件


Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/TableView.js");
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/datechooser.js");
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . "/css/datechooser.css");


批注1:在视图层引用与在控制层引用的方式一样。但在视图层中引用加载的要晚一些。
批注2:引用路径是使用baseUrl,而不是basePath。
批注3:关于参数CClientScript::POS_END,作用是延时加载,提高页面渲染效率。例如:
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/jqueryui/jquery-ui.min.js", CClientScript::POS_END);
全部参数一览:
CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
CClientScript::POS_END : the script is inserted at the end of the body section.
CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
注:这些参数仅适用于加载js文件,不适用于加载css文件。


三、引入jquery核心部件


Yii::app()->clientScript->registerCoreScript('jquery');


批注:不论在页面中的何种位置引用,最终yii会将jquery.js文件放入yii的assets文件夹下。即/projectName/assets/82qg58/jquery-1.6.1.min.js。


二、在控制层(../controllers/xxController.php)添加CSS文件或java script文件


public function init()
{?
? ? //parent::init();?
? ? Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css');
? ? Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js');
}


新增:
在控制层,还可以在ActionIndex中引入,而且还可以引入别的module文件夹中的js/css文件。甚至是任意文件夹下的js/css文件


public function actionIndex(){
? $modify,$reg = some_value;
? $js = $this->renderFile($this->getInstallViewPath(). '/asset/install.js',array('reg_mp'=>$reg), true);
? $js = $this->renderFile($this->getViewPath() . '/assets/install_params.js', array('modify' => $modify), true);
?
? $cs = Yii::app()->clientScript;
? $cs->registerScript('asset/install', $js, CClientScript::POS_END);
? $cs->registerCssFile(Yii::app()->baseUrl . '/css/launch_feed.css');
? $cs->registerScript('assets/install_params',$js,CClientScript::POS_END);
? $cs->registerScriptFile(Yii::app()->baseUrl . '/resources/jquery.form.js');
? $cs->registerCssFile(Yii::app()->baseUrl . '/css/install_params.css');


? $this->render('xxx');?
?}


public function getInstallViewPath() {
? ? ? ? return $this->getModule()->getBasePath().'/../operations/views';
}?


一、在../layouts/main.php中引入
1,直接引入








2,yii方式引入




? ? Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/jqueryui/jquery-ui.min.js", CClientScript::POS_END);




if($this->user->id) {
? Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('/account/info', array('format' => 'js')), CClientScript::POS_END);
?}


?if($this->user->id) {
? Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('site/baseJs'));
?}
?>


批注:在yii运行后,第一种在head中,第二种在body最后面,显然后者效率更高。但必须加载的js和css有必要写在head中。


3,区别


批注:至于为什么会有/assets/b729ab/js/jquery.js这样的文件生成,还在继续探索中。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Yii 用户登陆机制 下一篇Yii 不完全解决方案

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: