1. config/main.php裡增加layout欄位return array('defaultController'=>'index','layout' => 'main',
2. layouts裡建立3個檔案, main.php, header,footermain.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(); ?>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!