和Java Swing中類似Yii Framework 中的Layout 也允許嵌套,這是通過CContentDecorator來實現的,不過代碼中並不需要 直接使用CContentDecorator ,而是在定義layout使用
$this->beginContent('path/to/view'); // ... content to be decorated $this->endContent();
其中view 為另外一個布局。
本例定義四個Layout,一個嵌套一個:
///main.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo Yii::app()->name; ?></title> </head> <body> <h1>Widget Demo</h1> <?php echo $content; ?> </body> </html> ///row1.php <?php $this->beginContent('/layouts/row2'); ?> <center> <?php echo $content; ?> </center> <p /> <center> <?php echo 'row1 part'; ?> </center> <?php $this->endContent(); ?> //row2.php <?php $this->beginContent('/layouts/row3'); ?> <center> <?php echo $content; ?> </center> <p /> <center> <?php echo 'row2 part'; ?> </center> <?php $this->endContent(); ?> ///row3.php <?php $this->beginContent('/layouts/main'); ?> <center> <?php echo $content; ?> </center> <p /> <center> <?php echo 'row3 part'; ?> </center> <?php $this->endContent(); ?>
顯示結果如下:
本例下載:http://www.imobilebbs.com/download/yii/ContentDecoratorDemo.zip