首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Yii Framework 教程(13):CContentDecorator 版面裝飾器範例

     編輯:PHP 綜合知識

如 Java Swing 允許版面嵌套,Yii Framework 的 Layout 也支援嵌套,透過 CContentDecorator 實現。代碼中不直接使用 CContentDecorator,而是在定義 layout 時使用:

$this->beginContent('path/to/view');
// ... 要被裝飾的內容
$this->endContent();

其中 view 為另一個版面配置檔。

四層嵌套 Layout 範例

本例定義四個 Layout,層層嵌套:

Layout 嵌套結構圖

/// main.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://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(); ?>

顯示結果

嵌套 Layout 執行結果

範例下載:ContentDecoratorDemo.zip

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved