Yii framwork renderPartial 的用法<?php echo $this->renderPartial('/default/add_news');?>
<?php echo $this->renderPartial('//public/footer');?>
absolute view within a module: the view name starts with a single slash '/'.
absolute view within the application: the view name starts with double slashes '//'.
当前module下的view文件使用/
外层views下的view文件用//
(Yii)使用renderPartial调用另外一个控制器的视图
我们可以使用renderPartial访问存储在不同控制器的视图文件夹中的部分视图文件。
在Yii1.1.3中,我们使用双斜线“//”,程序就会从视图文件夹开始搜索,例如:
protected/views/library/_book.php
renderPartial(’//library/_book’, array(’books’=>$books))
如果使用单斜杠“/”开始搜索,程序就会跳出当前的活动模块,从应用程序的视图路径去搜索。
yii renderPartial ajax validate(ajax验证)
yii的自带验证很好用,可以前后台一起实现,这样就少写了很多js代码.
今天在做项目的时候用yii中的renderPartial来指定视图文件,还是用原来的验证,结果不起作用了.经过查找资料找到了如下解决办法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php class SettingController extends Controller{ public function actionAddProduct(){ $data = array(); $data['McustomProduct'] = $McustomProduct = new CustomProduct(); if($_POST){ $this->performAjaxValidation($McustomProduct); $product = Yii::app()->request->getParam('CustomProduct'); $McustomProduct->attributes = $product; if($McustomProduct->save()){ $McustomProduct->save(); Yii::app()->user->setFlash('success', '新增产品成功'); }else { Yii::app()->user->setFlash('failed', '新增产品失败 '); } $this->redirect(Yii::app()->request->urlReferrer); } $this->renderPartial('modal/modal-add-product', $data, false, true); } //此方法用于renderPartial中的ajax验证 protected function performAjaxValidation($model) { if(Yii::app()->request->isAjaxRequest) { echo CActiveForm::validate($model); Yii::app()->end(); } } } |
高亮部分就是需要设置的地方
yii renderPartial不加载css js样式解决的方法
1、用YII框架实现后台框架页面。加载视图时(该视图是将以前的index.php内所有代码复制过来),始终显示了不最终页面。似乎是加载了视图又放到main.php布局里。后来研究发现,原来使用render()加载视图,会把需要的js,css等嵌入,同时也会将视图内容加载到布局中并最终显示。如果使用render()方法,又不想使用布局,在使用render()方法之前使用:$this->layout=false,即可。
1 2 3 4 5 | public function actionIndex()
{
$ this ->layout= false ;
$ this ->render( 'index' );
}
|
采用如上代码,更改actionTop()、actionLeft()、actionMain()等方法,最后成功显示框架页面。
这时回想到:renderPartial()方法不是不加载布局文件的吗?的确是,但是使用该方法则不会把需要的js,css等嵌入
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。