自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP開發(fā)必備 一步步學(xué)PHP模版引擎Dwoo

開發(fā) 后端
PHP 獨(dú)特的語法混合了 C、Java、Perl 以及 PHP 自創(chuàng)新的語法。它可以比 CGI或者Perl更快速的執(zhí)行動態(tài)網(wǎng)頁。用PHP做出的動態(tài)頁面與其他的編程語言相比,PHP是將程序嵌入到HTML文檔中去執(zhí)行,執(zhí)行效率比完全生成HTML標(biāo)記的CGI要高許多。本文介紹了一款PHP的模版引擎Dwoo,一起來看。

PHP目前是使用最廣泛的腳本解析動態(tài)語言之一。在PHP的開發(fā)當(dāng)中,開發(fā)者都很關(guān)心的一個(gè)問題是,如何最大程度地將頁面和商業(yè)邏輯分離。而目前的很多PHP的開發(fā)框架,在這方面都有很好的解決方案,比如Zend,Agavi,CakePHP和CodeIgniter。然而,假如你的項(xiàng)目不是太大而沒使用這些框架時(shí),則可以選用一些開源的PHP模版引擎來實(shí)現(xiàn)頁面和邏輯的分離,目前比較著名的有Smarty。本文將介紹另一款新興的PHP模版引擎Dwoo,它同樣有很多優(yōu)點(diǎn),值得讀者去學(xué)習(xí)。

一、安裝Dwoo

首先到Dwoo的官方網(wǎng)站下載(http://www.dwoo.org)最新的版本1.1.7。在下載后,解壓dwoo,將解壓目錄命名為dwoo,當(dāng)然,你也可以用pear的安裝方法安裝,方法為:

pear channel-discover pearhub.org

pear install pearhub/Dwoo

二、Dwoo模版簡介

在Dwoo中,跟象Smarty等模版引擎差不多的是,它允許用戶用普通的HTML編輯工具編輯表現(xiàn)層的頁面,然后在需要產(chǎn)生動態(tài)內(nèi)容的地方用占位符表示。模版引擎在解析的時(shí)候,會把如數(shù)據(jù)庫中的或者業(yè)余邏輯計(jì)算結(jié)果填充到這些占位符中。下面先看一個(gè)簡單的例子。

我們先建立一個(gè)模版文件,Dwoo的模版文件默認(rèn)是tpl,當(dāng)然你也可以改為其他文件后綴。模版文件名為knock.tpl,把它保存在template文件夾中,內(nèi)容為:

 

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <blockquote> 
  5. Knock knock! <br/> 
  6. Who's there? <br/> 
  7. {$name} <br/> 
  8. {$name} who? <br/> 
  9. {$punchline}  
  10. </blockquote> 
  11. </body> 
  12. </html> 

可以看到,在Dwoo中,模版文件中,把需要?jiǎng)討B(tài)更替的內(nèi)容用{$ }這樣的形式包裹起來,作為占位符,占位符當(dāng)中的內(nèi)容到時(shí)會被自動更替為實(shí)際的內(nèi)容。接下來看如何使用Dwoo,代碼如下:

 

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. // 創(chuàng)建dwoo實(shí)例  
  4. $dwoo = new Dwoo();  
  5. //讀取模版文件   
  6. $tpl = new Dwoo_Template_File('tmpl/knock.tpl');  
  7. // 對模版變量賦值  
  8. $data = array();  
  9. $data['name'] = 'Boo';  
  10. $data['punchline'] = 'Don\'t cry, it\'s only a joke';  
  11. // 將實(shí)際內(nèi)容輸出到模版  
  12. $dwoo->output($tpl$data);  
  13. ?> 

下面是使用Dwoo的幾個(gè)步驟:

1、首先要包含Dwoo自動裝載類dwooAutoload.php,這個(gè)類是自動加載了Dwoo模版所需要的其他依賴的類和工具類;

2、創(chuàng)建Dwoo類的實(shí)例;

3、通過new Dwoo_Template_File的方法加載模版,其中的參數(shù)為模版文件所在的路徑;

4、設(shè)置要向模版文件中輸出的替換內(nèi)容,在Dwoo中,只需要通過定義一個(gè)關(guān)聯(lián)數(shù)組的方法即可,數(shù)組中每個(gè)元素的名稱跟模版文件中的占位符一一對應(yīng),數(shù)組中的每個(gè)值,就是要替換模版中的實(shí)際內(nèi)容;

5、通過調(diào)用output方法,將數(shù)據(jù)向模版中輸出,傳入的參數(shù)為輸出的數(shù)組內(nèi)容和模版路徑。

下圖為輸出結(jié)果:

shuchujieguo

#p#

三、Dwoo語法講解

下面以實(shí)例的形式講解下Dwoo的語法,先來看最常用的if語句。

1) if 語句

下面是一個(gè)模版的例子:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {if $auth == 0}  
  5. Not logged in  
  6. {else}  
  7. Logged in as: Anonymous User   
  8. {/if}  
  9. </body> 
  10. </html> 

可以看到,Dwoo中的if語句其實(shí)跟普通的if語句沒什么區(qū)別。接下來我們看下控制這個(gè)模版的php文件,如下:

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. try {  
  4. $dwoo = new Dwoo();  
  5. $tpl = new Dwoo_Template_File('tmpl/auth.tpl');  
  6. $data = new Dwoo_Data();  
  7. $data->assign('auth', rand(0,1));  
  8. $dwoo->output($tpl$data);  
  9. } catch (Exception $e) {  
  10. echo "Error: " . $e->getMessage();   
  11. }  
  12. ?> 

注意,這里我們使用了new Dwoo_Data();這個(gè)Dwoo_Data()方法的優(yōu)勢在于,它比較容易存放大量的數(shù)據(jù),比用數(shù)組的方法去存儲數(shù)據(jù)方便多了,而且它本身提供了很多不同的方法去獲得,清理和刪除模版變量。這個(gè)例子中,用隨機(jī)數(shù)的方法產(chǎn)生了auth變量,結(jié)果可能為如下圖:

xiatu

當(dāng)然,可以使用if elseif語句,比如模版中:

 

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {if $auth == 1}  
  5. Logged in as: Anonymous User   
  6. {elseif $auth == 2}  
  7. Logged in as: Administrator   
  8. {else}  
  9. Not logged in  
  10. {/if}  
  11. </body> 
  12. </html> 

2) LOOP循環(huán)語句

在Dwoo中,可以使用{loop}進(jìn)行循環(huán),動態(tài)產(chǎn)生數(shù)據(jù),下面是例子:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <ul> 
  5. {loop $items}  
  6. <li>{escape($item)}</li> 
  7. {/loop}  
  8. </ul> 
  9. </body> 
  10. </html> 

下面是產(chǎn)生數(shù)據(jù)的php文件:

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. try {  
  4. $dwoo = new Dwoo();  
  5. $tpl = new Dwoo_Template_File('tmpl/list.tpl');  
  6. $data = new Dwoo_Data();  
  7. $items = array();  
  8. $items[] = array('item' => 'red');  
  9. $items[] = array('item' => 'yellow');  
  10. $items[] = array('item' => 'blue');  
  11. $items[] = array('item' => 'green');  
  12. $data->assign('items'$items);  
  13. $dwoo->output($tpl$data);  
  14. } catch (Exception $e) {  
  15. echo "Error: " . $e->getMessage();   
  16. }  
  17. ?> 

這里,我們生成了數(shù)組items,然后在模版文件中,通過{loop $items}即可循環(huán)輸出內(nèi)容。結(jié)果如下圖:

jieguo

注意,這里使用了{(lán)escape($item)}的方法輸出每一行的內(nèi)容,其中eascape是dwoo中使用的插件,是將所有內(nèi)容在輸出前使用HTML編碼格式過濾,這可以防止XSS攻擊,是個(gè)很好的實(shí)踐。

而在Dwoo中,可以同樣使用{foreach}而達(dá)到同樣的效果,代碼如下:

 

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <ul> 
  5. {foreach $items item}  
  6. <li>{escape($item)}</li> 
  7. {/foreach}  
  8. </ul> 
  9. </body> 
  10. </html> 

同樣,foreach也可以使用如下的用法,即:

 

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <ul> 
  5. {foreach $items key value}  
  6. <li>{upper($key)} is for {$value}</li> 
  7. {/foreach}  
  8. </ul> 
  9. </body> 
  10. </html> 

而配合這個(gè)模版,PHP的控制頁面中的關(guān)聯(lián)數(shù)組的寫法如下:

  1. $data = new Dwoo_Data();  
  2. $items = array(  
  3. 'a' => 'apple',  
  4. 'b' => 'ball',  
  5. 'c' => 'cat',  
  6. 'd' => 'dog' 
  7. );  
  8. $data->assign('items'$items); 

這樣輸出結(jié)果如下圖:

shuchujieguo

我們既然學(xué)會了loop,下面來嘗試下從數(shù)據(jù)庫中取出數(shù)據(jù)集,并通過Dwoo顯示出來,下面是模版文件的主要部分:

 

  1. <body> 
  2. <table> 
  3. <tr class="heading"> 
  4. <td>Author</td> 
  5. <td>Title</td> 
  6. </tr>   
  7. {loop $records}  
  8. <tr> 
  9. <td>{$author}</td> 
  10. <td>{$title}</td> 
  11. </tr>   
  12. {/loop}  
  13. </table> 
  14. </body> 

而PHP文件代碼如下,其中使用了PDO去訪問數(shù)據(jù)庫:

 

  1. <? php  
  2. include 'dwooAutoload.php';  
  3. // 連接數(shù)據(jù)庫  
  4. try {  
  5. $dbh = new PDO('mysql:dbname=library;host=localhost''user''pass');  
  6. } catch (PDOException $e) {  
  7. echo "Error: Could not connect. " . $e->getMessage();  
  8. }  
  9. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
  10. try {  
  11. $sql = "SELECT a.AuthorName AS author, t.TitleName AS title FROM author AS a, title AS t, author_title AS at WHERE a.AuthorID = at.AuthorID AND t.TitleID = at.TitleID ORDER BY author LIMIT 0,20";  
  12. $sth = $dbh->query($sql);  
  13. while ($row = $sth->fetchObject()) {  
  14. $records[] = array('author' => $row->author, 'title' => $row->title);  
  15. }  
  16. //關(guān)閉數(shù)據(jù)庫連接  
  17. unset($dbh);   
  18. $dwoo = new Dwoo();  
  19. $tpl = new Dwoo_Template_File('tmpl/books.tpl');  
  20. $data = new Dwoo_Data();  
  21. $data->assign('records'$records);  
  22. $dwoo->output($tpl$data);   
  23. } catch (PDOException $e) {  
  24. echo "Error: Could not execute query \"$sql\". " . $e->getMessage();   
  25. unset($dbh);  
  26. } catch (Exception $e) {  
  27. echo "Error: " . $e->getMessage();   
  28. }   
  29. ?> 

 

#p#

四、模版組合

在頁面設(shè)計(jì)中,常用的最佳實(shí)踐是把一個(gè)復(fù)雜的頁面劃分為不同的部分,同樣模版文件中也應(yīng)該指定不同的部分,最后再將其組合起來,比如下圖是常件的模版件結(jié)構(gòu):

mobanjianjiegou

可以看到有頭部,尾部和頁面的主體三部分組成,下面給出它們的模版文件header.tpl:

 

  1. <!-- BEGIN header.tpl --> 
  2. <html> 
  3. <head></head> 
  4. <body> 
  5. <table width="100%" border="1"> 
  6. <tr> 
  7. <td align="center"><a href="#">Home</a></td> 
  8. <td align="center"><a href="#">News</a></td> 
  9. <td align="center"><a href="#">Weather</a></td> 
  10. <td align="center"><a href="#">Hotels</a></td> 
  11. <td align="center"><a href="#">Dining</a></td> 
  12. </tr> 
  13. </table> 
  14. <p /> 
  15. <h2>{$title}</h2> 
  16. <p /> 
  17. <!-- END header.tpl --> 
  18. footer.tpl  
  19. <!-- BEGIN footer --> 
  20. <table width="100%" align="center"> 
  21. <tr> 
  22. <td align="center"><font size="-2">&copy; {$year}. All rights reserved.</font></td> 
  23. </tr> 
  24. </table> 
  25. </body> 
  26. </html> 

而Dwoo中,使用include可以將不同的模版包含到同一個(gè)模版中去,比如下面是框架主模版文件main.tpl:

 

  1. {include(file='header.tpl')}  
  2. <!-- BEGIN main.tpl --> 
  3. <table border="1"> 
  4. <tr> 
  5. <td valign="top"> 
  6. <strong>{$headline}</strong> 
  7. <p /> 
  8. {$content}  
  9. </td> 
  10. <td valign="top" align="center" width="25%"> 
  11. <strong>Special Feature</strong><br /> 
  12. {$feature}  
  13. </td> 
  14. </tr> 
  15. </table> 
  16. <!-- END main.tpl --> 
  17. {include(file='footer.tpl')} 

而框架文件的php文件如下,可以為主框架模版中的變量賦值:

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. try {  
  4. $dwoo = new Dwoo();  
  5. $tpl = new Dwoo_Template_File('tmpl/main.tpl');  
  6. $data = new Dwoo_Data();  
  7. $data->assign('title''Welcome to London!');  
  8. $data->assign('headline''Playing in the Park');  
  9. $data->assign('content''It\'s a warm summer day, and Simon finds the lake in St. James Park too inviting for words...');  
  10. $data->assign('feature''Tower Bridge - Snapshots from the Top');  
  11. $data->assign('year'date('Y'mktime()));  
  12. $dwoo->output($tpl$data);  
  13. } catch (Exception $e) {  
  14. echo "Error: " . $e->getMessage();   
  15. }  
  16. ?> 

可以得出如下結(jié)果:

jieguo

而另外的實(shí)現(xiàn)方法,是不使用include,而是在主框架模版中如下設(shè)置:

 

  1. {$header}  
  2. <!-- BEGIN main.tpl --> 
  3. <table border="1"> 
  4. <tr> 
  5. <td valign="top"> 
  6. <strong>{$headline}</strong> 
  7. <p /> 
  8. {$content}  
  9. </td> 
  10. <td valign="top" align="center" width="25%"> 
  11. <strong>Special Feature</strong><br /> 
  12. {$feature}  
  13. </td> 
  14. </tr> 
  15. </table> 
  16. <!-- END main.tpl --> 
  17. {$footer} 

而在PHP文件中,再動態(tài)設(shè)置header和footer的變量的值,

  1. $data->assign('header',$dwoo->get(new Dwoo_Template_File('tmpl/header.tpl'), $data));  
  2. $data->assign('footer',$dwoo->get(new Dwoo_Template_File('tmpl/footer.tpl'), $data)); 

這里使用了Dwoo中的get方法,將兩個(gè)模版文件中的內(nèi)容提取出來,設(shè)置到header和footer兩個(gè)變量中去。

#p#

五、Dwoo中的插件機(jī)制

在Dwoo中,為開發(fā)者提供了大量方便的插件,比如前文提到的escape過濾功能,也是Dwoo 的插件之一。下面再學(xué)習(xí)一個(gè)同樣功能的插件auto_esacpe,它其實(shí)實(shí)現(xiàn)的是跟escape一樣的功能,但它可以針對一整段的模版變量進(jìn)行格式化,比如,如下的模版:

 

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {auto_escape on}  
  5. {$html}  
  6. {/auto_escape}  
  7. </body> 
  8. </html> 

這里使用了{(lán)auto_escape on},表明在 {/auto_escape}前的輸出全部要進(jìn)行HTML格式化,考察如下的php腳本:

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. try {  
  4. $dwoo = new Dwoo();  
  5. $tpl = new Dwoo_Template_File('tmpl/out.tpl');  
  6. $data = array();  
  7. $data['html']= '<span id="ack">Welcome to Jack & Jill\'s humble abode.</span>';  
  8. $dwoo->output($tpl$data);  
  9. } catch (Exception $e) {  
  10. echo "Error: " . $e->getMessage();   
  11. }  
  12. ?> 

其輸出為如下圖:

shuchujieguo

同樣,Dwoo也有象php中的strip_tags方法,用來去掉HTML標(biāo)記,比如:

 

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {strip_tags($html)}  
  5. </body> 
  6. </html> 

對應(yīng)的PHP腳本為:

  1. $data = array();  
  2. $data['html'] = '<a href="http://www.google.com">Search</a>'

那么將會輸出如下結(jié)果:

shuchujieguo

下面再介紹Dwoo中關(guān)于日期格式化的處理,其中可以使用date_format這個(gè)插件,這個(gè)插件需要傳入兩個(gè)參數(shù),一個(gè)是要處理的日期,另外一個(gè)是指定用什么格式去格式化處理日期,舉個(gè)例子:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {date_format $date "%d.%m.%Y"}  
  5. <br/> 
  6. {date_format $date "%B %d, %Y %I:%M %p"}  
  7. </body> 
  8. </html> 

處理的PHP代碼的核心部分:

  1. $data = array();  
  2. $data['date'] = '14 July 2010 21:35'

下面為其輸出:

shuchujieguo

小結(jié)

在本文中,介紹了PHP模版引擎Dwoo的基本原理和用法,給讀者一個(gè)快速的入門,在下一篇中,將深入介紹挖掘Dwoo中的一些特色功能。

【編輯推薦】

  1. PHP開發(fā)必備 PHP持續(xù)集成工具介紹(上)
  2. PHP開發(fā)必備 PHP持續(xù)集成工具介紹(下)
  3. PHP正則表達(dá)式 PHP中的正則函數(shù)
  4. PHP中幾個(gè)常用的時(shí)間應(yīng)用方式
責(zé)任編輯:于鐵 來源: IT168
相關(guān)推薦

2011-05-19 11:03:02

PHPDwoo

2018-06-11 15:30:12

2017-01-19 21:08:33

iOS路由構(gòu)建

2019-03-05 14:09:27

Docker存儲容器

2019-07-09 15:23:22

Docker存儲驅(qū)動

2023-12-26 07:59:01

2018-12-24 10:04:06

Docker存儲驅(qū)動

2010-03-04 16:28:17

Android核心代碼

2016-11-02 18:54:01

javascript

2017-12-25 11:50:57

LinuxArch Linux

2011-05-10 10:28:55

2021-01-03 15:07:16

開發(fā)編程語言后端.

2024-08-30 08:30:29

CPU操作系統(tǒng)寄存器

2024-09-30 09:56:59

2018-07-13 15:36:52

2020-12-24 11:19:55

JavaMapHashMap

2015-07-27 16:06:16

VMware Thin虛擬化

2025-02-08 08:21:48

Java排序Spring

2009-12-17 16:36:23

無線路由設(shè)置密碼

2018-04-23 14:23:12

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號