Yii 渲染与布局(渲染页面的两种方式)
渲染页面的两种方式>>>以引入左部菜单栏为例 第一种:在前端使用renderPartial。(适用于个别) <!-- list页面 --> <div class="mian"> <!--引入layout文件夹下的左部菜单栏文件left.php--> <?php echo $this->renderPartial("//layouts/left");?> <div class="user-info"> <div>...</div> <div>...</div> <div>...</div> </div> </div> 第二种:通过layout属性指定所要使用的布局文件。(适用于全局) 第一步:在控制器中指定使用的布局属性layout所使用的布局文件, 指定的方式有(当前控制器默认继承了Yii的Controller,且Controller中有layout属性,如public $layout="//layouts/column1";): 方式一:在当前控制器中重写layout属性,如:public $layout="//layouts/my_column1"; 方式二:在当前控制器中添加或重写init()方法,并指定layout属性,如 public function init(){ parent::init(); //保留父类中的init方法 $this->layout="//layouts/my_column1"; 指定layout属性 } 第二步:假如控制器中的某个方法渲染了某个页面,这里以index页面为例: public function actionIndex() { $this->render( "list" ); } 第三步: <!-- list页面,与第一种的写法做个对比 --> <div class="user-info"> <div>...</div> <div>...</div> <div>...</div> </div> <!--my_column1文件--> <div class="mian"> <?php $this->beginContent("//layouts/main"); ?> <!-- 在-my_column1文件中使用beginContent(),endContent(),$content规划布局 --> <?php //左部菜单栏文件left.php的具体代码写在这 /*.......................................... ..............这是具体菜单代码............. ....(也可以用renderPartial把页面渲染过来).. ..........................................*/ ?> <?php echo $content; ?> <!-- $content代表list页面 --> <?php $this->endContent(); ?> </div> <!--main文件--> <?php $this->renderPartial( "//layouts/header" )?> <!-- 渲染头文件 --> <?php echo $content; ?> <!-- $content代表my_column1页面 --> <?php $this->renderPartial( "//layouts/footer" )?> <!-- 渲染尾部文件 -->
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: bat 逻辑运算
- 下一篇: yii外部action中如何渲染页面