<?php echo $this->renderPartial('/layouts/header'); ?>
<?php echo $content;?>
<?php echo $this->renderPartial('/layouts/footer'); ?>
刚开始写YII的时候,对于页面布局的问题相当苦恼。
最开始的问题就是:
很多时候可能需要多种不同的页面布局
当在Controller使用render渲染 视图文件的时候。
默认都是加载main.php文件。
我当时针对这个问题的解决方法就是使用renderPartial
renderPartial这个函数 是直接渲染视图文件,而不会加载main.php
所以我写出来之后发现很多页面都包含了相同段代码。
现在想想当初的方法真是笨透了
1
2
3
4
5
6
7
8
9
class HomeController extends CController
{
public $layout = 'x_main.php';
public function actionIndex()
{
$this->render('index', array('assignKey'=>'assignValue'));
}
}
原来Contrlloer默认就有$layout变量, 只要在Controller 设置一下就可以了。
另外在main.php也可以有一种让页面整洁的方法
<?php echo $this->renderPartial('/layouts/header'); ?>
<?php echo $content;?>
<?php echo $this->renderPartial('/layouts/footer'); ?>