《PHP設(shè)計(jì)模式介紹》第十七章 MVC 模式(2)_PHP教程
推薦:《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式前一章中使用動態(tài)記錄模式對數(shù)據(jù)庫表進(jìn)行建立,獲取,更新(通過擴(kuò)展實(shí)現(xiàn)刪除)每一行的操作。動態(tài)記錄模式是一種簡單的抽象數(shù)據(jù)庫連接的方式,但是這種簡潔性也正是它的弱點(diǎn)。動態(tài)記錄類只處理
使用savant的實(shí)例:
總有一些有復(fù)雜模版引擎甚至是"Plain Old PHP Pages"(popp)的模板無法可變換替換,而且嵌入了控制結(jié)構(gòu)和其他邏輯到頁面里。然而,給結(jié)果到你的應(yīng)用程序的表述層的業(yè)務(wù)邏輯,維護(hù)就會變得相當(dāng)困難。
注:寫模版引擎
似乎寫摸版引擎是php社區(qū)里的一種passage權(quán)利,搜索模版引擎逐字發(fā)現(xiàn)上百的結(jié)果。(這方面的實(shí)驗(yàn)例子可以看http://www.sitepoint.com/forums/showthread.php?t=123769)如果你不選擇用普通的引擎,而是用你自己的,這兒有豐富的實(shí)例代碼可以看。
地址http://wact.sf.net/index.php/TemplateView很好的概述了什么樣式的標(biāo)記可以被模版視圖使用。包括一個(gè)屬性語言,自定義標(biāo)簽,html備注以及自定義語法。
非常流行的模版引擎smarty(http://smarty.php.net/)是一個(gè)使用自定義語法方法的模版引擎的實(shí)例。
裝載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中概述的那種自定義標(biāo)簽。雖然wact支持一個(gè)與smarty相似的自定義語法作為快捷方式,wact的自定義標(biāo)簽列陣如下:
| 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> |
在這個(gè)wact例子里有相當(dāng)多的包含的文件。這是因?yàn)榭蚣苡懈鞣N各樣的要素來處理網(wǎng)站應(yīng)用問題的各個(gè)部分。只需包含你需要的元素。在上面的例子中,模板就是一個(gè)View,dictionary data source 作為model的代理,php腳本本身是作為一個(gè)controller.許多自定義標(biāo)簽設(shè)計(jì)成與表格數(shù)據(jù)一起運(yùn)用--像你從數(shù)據(jù)庫中提取的記錄集---轉(zhuǎn)換成簡單數(shù)組以后把它用在模版里。
最后一個(gè)樣式是擁有一個(gè)模版的有效的xml文件,使用各自的要素的屬性作為目標(biāo)替換你的模版。這里有一個(gè)是用PHP- TAL的技術(shù)實(shí)例(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”?> |
分享:《PHP設(shè)計(jì)模式介紹》第十四章 動態(tài)記錄模式到目前為止,您所看到的這些設(shè)計(jì)模式大大提高了代碼的可讀性與可維護(hù)性。然而,在WEB應(yīng)用設(shè)計(jì)與開發(fā)中一個(gè)基本的需求與挑戰(zhàn):數(shù)據(jù)庫應(yīng)用,這些設(shè)計(jì)模式都沒有涉及到。本章與接下來的兩章—
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-《PHP設(shè)計(jì)模式介紹》第十七章 MVC 模式(2)
。