日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

《PHP設計模式介紹》第十七章 MVC 模式(2)_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:《PHP設計模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關模式
前一章中使用動態(tài)記錄模式對數(shù)據(jù)庫表進行建立,獲取,更新(通過擴展實現(xiàn)刪除)每一行的操作。動態(tài)記錄模式是一種簡單的抽象數(shù)據(jù)庫連接的方式,但是這種簡潔性也正是它的弱點。動態(tài)記錄類只處理

使用savant的實例:

總有一些有復雜模版引擎甚至是"Plain Old PHP Pages"(popp)的模板無法可變換替換,而且嵌入了控制結構和其他邏輯到頁面里。然而,給結果到你的應用程序的表述層的業(yè)務邏輯,維護就會變得相當困難。

注:寫模版引擎

似乎寫摸版引擎是php社區(qū)里的一種passage權利,搜索模版引擎逐字發(fā)現(xiàn)上百的結果。(這方面的實驗例子可以看http://www.sitepoint.com/forums/showthread.php?t=123769)如果你不選擇用普通的引擎,而是用你自己的,這兒有豐富的實例代碼可以看。

地址http://wact.sf.net/index.php/TemplateView很好的概述了什么樣式的標記可以被模版視圖使用。包括一個屬性語言,自定義標簽,html備注以及自定義語法。

非常流行的模版引擎smarty(http://smarty.php.net/)是一個使用自定義語法方法的模版引擎的實例。

裝載smarty引擎就像:

require_once ‘Smarty.class.php’;
$tpl =& new Smarty;
$tpl->assign(array(
‘title’ => ‘Colors of the Rainbow’
,’colors’ => array(‘red’, ‘orange’, ‘yellow’,
‘green’, ‘blue’, ‘indigo’, ‘violet’)
));
$tpl->display(‘rainbow.tpl’);


rainbow.html的自定義語法就像:

<html><head>
<title>{$title}</title>
</head><body>
<h1>{$title}</h1>
<ol>
{section name=rainbow loop=$colors}
<li>{$colors[rainbow]}</li>
{/section}
</ol>
</body></html>

wact(http://wact.sf.net/)效仿了martin fowler在poeaa中概述的那種自定義標簽。雖然wact支持一個與smarty相似的自定義語法作為快捷方式,wact的自定義標簽列陣如下:

require_once ‘wact/framework/common.inc.php’;
require_once WACT_ROOT.’template/template.inc.php’;
require_once WACT_ROOT.’datasource/dictionary.inc.php’;
require_once WACT_ROOT.’iterator/arraydataset.inc.php’;
// simulate tabular data
$rainbow = array();
foreach (array(‘red’, ‘orange’, ‘yellow’,
‘green’, ‘blue’, ‘indigo’, ‘violet’) as $color) {
$rainbow[] = array(‘color’ => $color);
}
$ds =& new DictionaryDataSource;
$ds->set(‘title’, ‘Colors of the Rainbow’);
$ds->set(‘colors’, new ArrayDataSet($rainbow));
$tpl =& new Template(‘/rainbow.html’);
$tpl->registerDataSource($ds);
$tpl->display();


rainbow.html的模版如下:

<html><head>
<title>{$title}</title>
</head><body>
<h1>{$title}</h1>
<list:list id=”rainbow” from=”colors”>
<ol>
<list:item><li>{$color}</li></list:item>
</ol>
</list:list>
</body></html>

在這個wact例子里有相當多的包含的文件。這是因為框架有各種各樣的要素來處理網(wǎng)站應用問題的各個部分。只需包含你需要的元素。在上面的例子中,模板就是一個View,dictionary data source 作為model的代理,php腳本本身是作為一個controller.許多自定義標簽設計成與表格數(shù)據(jù)一起運用--像你從數(shù)據(jù)庫中提取的記錄集---轉換成簡單數(shù)組以后把它用在模版里。

最后一個樣式是擁有一個模版的有效的xml文件,使用各自的要素的屬性作為目標替換你的模版。這里有一個是用PHP- TAL的技術實例(http://phptal.motion-twin.com/)

// PHP5
require_once ‘PHPTAL.php’;
class RainbowColor {
public $color;
public function __construct($color) {
$this->color = $color;
}
}
// make a collection of colors
$colors = array();
foreach (array(‘red’, ‘orange’, ‘yellow’,
‘green’, ‘blue’, ‘indigo’, ‘violet’) as $color) {
$colors[] = new RainbowColor($color);
}
$tpl = new PHPTAL(‘rainbow.tal.html’);
$tpl->title = ‘Colors of the Rainbow’;
$tpl->colors = $colors;
try {
echo $tpl->execute();
}
catch (Exception $e){
echo $e;
}

rainbow.tal.html的模版文件如下

<?xml version=”1.0”?>
<html>
<head>
<title tal:content=”title”>
place for the page title
</title>
</head>
<body>
<h1 tal:content=”title”>sample title</h1>
<ol>
<li tal:repeat=”item colors”>
<span tal:content=”item/color”>color</span>
</li>
</ol>
</body>
</html>

當然,所有的解決方法都是將model數(shù)據(jù)的顯示從model以及應用程序本身分離出來。每個前期的實例都是實質上產(chǎn)生了同樣的內容,所以選擇哪個是個人喜好的問題。

分享:《PHP設計模式介紹》第十四章 動態(tài)記錄模式
到目前為止,您所看到的這些設計模式大大提高了代碼的可讀性與可維護性。然而,在WEB應用設計與開發(fā)中一個基本的需求與挑戰(zhàn):數(shù)據(jù)庫應用,這些設計模式都沒有涉及到。本章與接下來的兩章—

來源:模板無憂//所屬分類:PHP教程/更新時間:2008-08-22
相關PHP教程