PHP开发必备 一步步学PHP模版引擎Dwoo

开发 后端
PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创新的语法。它可以比 CGI或者Perl更快速的执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成HTML标记的CGI要高许多。本文介绍了一款PHP的模版引擎Dwoo,一起来看。

PHP目前是使用最广泛的脚本解析动态语言之一。在PHP的开发当中,开发者都很关心的一个问题是,如何最大程度地将页面和商业逻辑分离。而目前的很多PHP的开发框架,在这方面都有很好的解决方案,比如Zend,Agavi,CakePHP和CodeIgniter。然而,假如你的项目不是太大而没使用这些框架时,则可以选用一些开源的PHP模版引擎来实现页面和逻辑的分离,目前比较著名的有Smarty。本文将介绍另一款新兴的PHP模版引擎Dwoo,它同样有很多优点,值得读者去学习。

一、安装Dwoo

首先到Dwoo的官方网站下载(http://www.dwoo.org)最新的版本1.1.7。在下载后,解压dwoo,将解压目录命名为dwoo,当然,你也可以用pear的安装方法安装,方法为:

pear channel-discover pearhub.org

pear install pearhub/Dwoo

二、Dwoo模版简介

在Dwoo中,跟象Smarty等模版引擎差不多的是,它允许用户用普通的HTML编辑工具编辑表现层的页面,然后在需要产生动态内容的地方用占位符表示。模版引擎在解析的时候,会把如数据库中的或者业余逻辑计算结果填充到这些占位符中。下面先看一个简单的例子。

我们先建立一个模版文件,Dwoo的模版文件默认是tpl,当然你也可以改为其他文件后缀。模版文件名为knock.tpl,把它保存在template文件夹中,内容为:

 

  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中,模版文件中,把需要动态更替的内容用{$ }这样的形式包裹起来,作为占位符,占位符当中的内容到时会被自动更替为实际的内容。接下来看如何使用Dwoo,代码如下:

 

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. // 创建dwoo实例  
  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. // 将实际内容输出到模版  
  12. $dwoo->output($tpl$data);  
  13. ?> 

下面是使用Dwoo的几个步骤:

1、首先要包含Dwoo自动装载类dwooAutoload.php,这个类是自动加载了Dwoo模版所需要的其他依赖的类和工具类;

2、创建Dwoo类的实例;

3、通过new Dwoo_Template_File的方法加载模版,其中的参数为模版文件所在的路径;

4、设置要向模版文件中输出的替换内容,在Dwoo中,只需要通过定义一个关联数组的方法即可,数组中每个元素的名称跟模版文件中的占位符一一对应,数组中的每个值,就是要替换模版中的实际内容;

5、通过调用output方法,将数据向模版中输出,传入的参数为输出的数组内容和模版路径。

下图为输出结果:

shuchujieguo

#p#

三、Dwoo语法讲解

下面以实例的形式讲解下Dwoo的语法,先来看最常用的if语句。

1) if 语句

下面是一个模版的例子:

  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语句其实跟普通的if语句没什么区别。接下来我们看下控制这个模版的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();这个Dwoo_Data()方法的优势在于,它比较容易存放大量的数据,比用数组的方法去存储数据方便多了,而且它本身提供了很多不同的方法去获得,清理和删除模版变量。这个例子中,用随机数的方法产生了auth变量,结果可能为如下图:

xiatu

当然,可以使用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循环语句

在Dwoo中,可以使用{loop}进行循环,动态产生数据,下面是例子:

  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> 

下面是产生数据的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. ?> 

这里,我们生成了数组items,然后在模版文件中,通过{loop $items}即可循环输出内容。结果如下图:

jieguo

注意,这里使用了{escape($item)}的方法输出每一行的内容,其中eascape是dwoo中使用的插件,是将所有内容在输出前使用HTML编码格式过滤,这可以防止XSS攻击,是个很好的实践。

而在Dwoo中,可以同样使用{foreach}而达到同样的效果,代码如下:

 

  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> 

而配合这个模版,PHP的控制页面中的关联数组的写法如下:

  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); 

这样输出结果如下图:

shuchujieguo

我们既然学会了loop,下面来尝试下从数据库中取出数据集,并通过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去访问数据库:

 

  1. <? php  
  2. include 'dwooAutoload.php';  
  3. // 连接数据库  
  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. //关闭数据库连接  
  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#

四、模版组合

在页面设计中,常用的最佳实践是把一个复杂的页面划分为不同的部分,同样模版文件中也应该指定不同的部分,最后再将其组合起来,比如下图是常件的模版件结构:

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可以将不同的模版包含到同一个模版中去,比如下面是框架主模版文件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. ?> 

可以得出如下结果:

jieguo

而另外的实现方法,是不使用include,而是在主框架模版中如下设置:

 

  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文件中,再动态设置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方法,将两个模版文件中的内容提取出来,设置到header和footer两个变量中去。

#p#

五、Dwoo中的插件机制

在Dwoo中,为开发者提供了大量方便的插件,比如前文提到的escape过滤功能,也是Dwoo 的插件之一。下面再学习一个同样功能的插件auto_esacpe,它其实实现的是跟escape一样的功能,但它可以针对一整段的模版变量进行格式化,比如,如下的模版:

 

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

这里使用了{auto_escape on},表明在 {/auto_escape}前的输出全部要进行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标记,比如:

 

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

对应的PHP脚本为:

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

那么将会输出如下结果:

shuchujieguo

下面再介绍Dwoo中关于日期格式化的处理,其中可以使用date_format这个插件,这个插件需要传入两个参数,一个是要处理的日期,另外一个是指定用什么格式去格式化处理日期,举个例子:

  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

小结

在本文中,介绍了PHP模版引擎Dwoo的基本原理和用法,给读者一个快速的入门,在下一篇中,将深入介绍挖掘Dwoo中的一些特色功能。

【编辑推荐】

  1. PHP开发必备 PHP持续集成工具介绍(上)
  2. PHP开发必备 PHP持续集成工具介绍(下)
  3. PHP正则表达式 PHP中的正则函数
  4. PHP中几个常用的时间应用方式
责任编辑:于铁 来源: IT168
相关推荐

2011-05-19 11:03:02

PHPDwoo

2018-06-11 15:30:12

2017-01-19 21:08:33

iOS路由构建

2018-12-24 10:04:06

Docker存储驱动

2019-03-05 14:09:27

Docker存储容器

2019-07-09 15:23:22

Docker存储驱动

2023-12-26 07:59:01

2016-11-02 18:54:01

javascript

2017-12-25 11:50:57

LinuxArch Linux

2010-03-04 16:28:17

Android核心代码

2021-01-03 15:07:16

开发编程语言后端.

2011-05-10 10:28:55

2020-12-24 11:19:55

JavaMapHashMap

2018-04-23 14:23:12

2017-01-06 15:13:25

LinuxVim源代码

2009-12-17 16:36:23

无线路由设置密码

2019-04-01 10:15:02

2018-07-13 15:36:52

2015-07-27 16:06:16

VMware Thin虚拟化

2009-08-14 11:35:01

Scala Actor
点赞
收藏

51CTO技术栈公众号