Yii 渲染与布局(渲染页面的两种方式)

来源:互联网 发布:js防水涂料施工工艺 编辑:程序博客网 时间:2024/05/16 10:58
渲染页面的两种方式>>>以引入左部菜单栏为例第一种:在前端使用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' )?><!-- 渲染尾部文件 -->


0 0
原创粉丝点击