1. config/main.php里增加layout字段
return array(
'defaultController'=>'index',
'layout' => 'main',
2. layouts里创建3个文件, main.php, header,footer
main.php文件内容(
index.php会做为$content输出,render 设置哪个模板就显示哪个)
<
?php $this
-
>beginContent(
'//layouts/header');
?
>
<
?php $this
-
>endContent();
?
>
<
?php
echo
$content;
?
>
<br
>main.php
--
--
< ?php $this - >beginContent( '//layouts/footer'); ? > < ?php $this - >endContent(); ? >
< ?php $this - >beginContent( '//layouts/footer'); ? > < ?php $this - >endContent(); ? >
header.php和footer.php分别输出
header.php---和<br>footer---
3. views/index.php里做简单输出
<br><?php echo $post->m; ?>
4. C 控制
$post->m = 'index----';
$this->render('index', array(
'post' => $post
));
5最终执行结果
header.php--- index---- main.php---- footer.php---
!!! 如果控制器里使用 renderPartial !!!
$this->renderPartial('index', array(
'post' => $post
));
则只输出
index----
不调用layouts
批注:render会渲染layout,而renderPartial不会渲染。
关于layouts传参
需要在控制器里定义一个变量
public $a = null;
然后在Action里赋值
然后就可以在layout里调用了
以下是代码
class IndexController extends CController { public $a = null; public function actionIndex() { $post->m = 'index----'; $this->a = 'hello a!'; $this->render('index', array( 'post' => $post, ));
layouts.main.php里调用方法
<?php echo $this->a; ?>
输出结果:hello a!