php超越模板引擎

2005年5月8日 | 分类: PHP | 标签:

总体来说,模板引擎是一个"好东西"

作为一个PHP/Perl的程序员,许多模板引擎(fastTemplate, Smarty, Perl的 HTML::Template)的用户,以及我自己的(bTemplate [1] 的作者),我讲这句话很多次了。

然而,在同事进行了长时间的讨论之后,我确信了大量的模板引擎(包括我自己写的)根本是错误的。 我想唯一的例外是Smarty [2],虽然我认为它太庞大了,并且考虑到这篇文章的其余部分相当的没有观点。然而,就你为什么选择Smarty(或者类似的解决方案)有几个理由,这些将在文章后面探究。

这篇文章讨论模板的理论。我们将看到为什么大部分"模板引擎"是过于肥大,并且最终我们将回过头来看一个轻量级的,小巧快速的另类选择。

下载和授权

模板类和所有在本文中使用的例子能够在这里下载:template.zip [3]。你可以根据发布 [4] OSI [5] 的 MIT Open Source License使用这些文件中的代码。

一些关于模板引擎的背景知识

让我们首先研究一下模板引擎的背景知识。模板引擎被设计出来用于把商业逻辑(例如从数据库中获取数据或者计算贸易耗费)从数据的表现分离开来。模板引擎解决了两个主要问题:

 

  1. 如何实现这种分离
  2. 如何从HTML中分离"复杂"的php代码

 

这从理论上使得没有PHP经验的HTML设计者能够不看任何PHP代码的条件下修改站点的外观。

然而,模板系统也引入了一些复杂性。首先,我们现在有一个从多个文件得来的"页面"。典型的,你可能有一个主PHP页负责业务逻辑,一个外面的"布局"模板把整个站点的整体布局进行渲染,一个内部的内容特定的模板,一个数据库抽象层,以及模板引擎本身(这些可能是也可能不是由多个文件组成)。也有可能,一些人仅仅简单地在每个PHP页面的首尾处包含"头部"和"尾部"文件。

这产生的单个页面的文件数量是很可观的。然而,因为PHP解析器非常快,用到的文件数量可能不是那么重要除非你的站点流量很大。
然而,要记住模板系统引入了另外一个处理的层次。模板文件不仅仅是必须被包含,他们还必须被解析(取决于模板系统,这个行为有很多种方式来完成 —— 使用正则表达式,字符串替换,编译,词法分析,等等)。这就是为什么对模板进行测速变得流行起来:因为模板引擎使用各种方法来解析数据,它们中的一些比另外一些要快(而且,一些模板引擎提供了比其他引擎更加丰富的功能)。

模板引擎基础知识

简单地说,模板引擎利用了用C写的脚本语言(PHP)。在这些嵌入的脚本语言中,你有另外一个伪脚本语言(无论你的模板引擎支持何种标签)。某些提供了简单的变量改写和循环。另外一些呢,则提供了条件和嵌套循环。而再其他的呢(至少有Smarty)提供了一个PHP的比较大的子集的接口,以及一个缓冲层。

为什么我认为Smarty最接近于正确的方向?因为Smarty的目标是"把业务逻辑从表现中分离出来"而不是"PHP代码和HTML代码的分离"。这看上去区别不大,但是它正是要点所在。任何模板引擎的最终目标不应该是从HTML移除所有的逻辑。它应该是把表现逻辑从业务逻辑中分离出来。

有很多你仅仅需要逻辑来正确显示你的数据的例子。例如,你的业务逻辑是从你的数据库中获取一个用户列表。你的表现逻辑可能是把用户列表用3列显示。可能修改用户列表函数使得它返回3个数组是很笨的办法。毕竟函数不应该关心数据接下来要怎么处理这样的事情。然而,在你的模板文件中缺少一些逻辑,那些正是你要做的事情。

在这点上Smarty是正确的(使得你利用PHP的很多东西),但是仍然有许多问题。基本上,它仅仅提供了一个以新语法访问PHP的接口。以那开始,它看上去不那么聪明了。是不是事实上写 <font face="新宋体">{foreach --args}</font><font face="新宋体">&lt;? foreach --args ?&gt;</font> 更加简单?如果你认为这样简单一些,问问你自己是不是在包含一个巨大的模板库来到成这种分离时能够看到真正的意义要更加简单一些。诚然,Smarty提供了许多其他很好的特性,但是看上去这些益处能够在不用承担包含Smarty类库的情况下也能获得。

别样的解决方案

我主要要鼓吹的一个解决方案是一个使用PHP代码作为它的原生脚本语言的"模板引擎"。我知道这以前有人做过。而且当我第一次看到的时候,我想,"为什么要这样做?",然而我在考虑过我同事的论据之后,并且实现了一个直接使用PHP代码仍然实现了把业务逻辑和表现逻辑分离的最终目标的模板系统时(只用了大约25行代码,不包括注释),我意识到了好处所在。

这个系统给像我们这样的开发者提供了对PHP核心函数的访问权利,我们能够使用他们来格式化输出——像日期格式化这样的任务应该在模板中处理。而且,因为模板是普通的PHP文件,像Zend Performance Suite [6]PHP Accelerator [7] 这样的字节码缓存程序,能够自动缓存模板(因而,它们不需要在每次被访问时都被重新解释执行)。只要你记得把你的模板文件命名为程序能够辨认出是PHP文件的名字(通常,你仅仅需要确保它们有一个.php的后缀),这确实是一个好处。

当我认为这种方法比经典的模板引擎要高明得多时,肯定还有一些要商榷的问题。最明显的反面意见是,PHP代码太复杂了,而且设计者不应该强迫去学习PHP。事实上,PHP代码和像Smarty这样的高级模板引擎的语法差不多简单(如果不是更简单的话)。而且,设计者能够使用像<font face="新宋体">&lt;?=$var;?&gt;</font>这样的简写PHP。这要比<font face="新宋体">{$var}</font>复杂很多?当然,这要长一些,但是如果你习惯了,你能够获得了PHP的威力而且不用承受解析模板文件带来的负担。

第二,而且可能更重要的,在基于PHP的模板中没有固有的安全。Smarty提供了选项在模板文件中彻底禁用PHP代码。它使得开发者能够约束模板能够访问的函数和变量。如果你没有不怀好意的设计者,这不会是什么问题。然而,如果你允许外部的用户上传或者修改模板,我在此展示的基于PHP的解决方案绝对没有任何安全可言!任何代码都能放入模板中并且得到运行。是的,甚至是一个<font face="新宋体">print_r($GLOBALS)</font>(这将改有恶意的用户访问脚本中任何变量的权利)。

但是,我个人或者工作上写过的项目中,绝大多数不允许最终的用户修改或者上传模板。如果是这样,问题就不存在了。因此现在让我们来看看代码吧。

例子

这是一个简单的用户列表页面的例子。

<font face="新宋体">&lt;?php &nbsp;<br />require_once('template.php'); &nbsp;<br /><br />/** &nbsp;<br />* This variable holds the file system path to all our template files. &nbsp;<br />*/ &nbsp;<br />$path = './templates/'; &nbsp;<br /><br />/** &nbsp;<br />* Cr&#101;ate a template object for the outer template and set its variables. &nbsp;<br />*/ &nbsp;<br />$tpl = &amp; new Template($path); &nbsp;<br />$tpl-&gt;set('title', 'User List'); &nbsp;<br /><br />/** &nbsp;<br />* Cr&#101;ate a template object for the inner template and set its variables. &nbsp;The &nbsp;<br />* fetch_user_list() function simply returns an array of users. &nbsp;<br />*/ &nbsp;<br />$body = &amp; new Template($path); &nbsp;<br />$body-&gt;set('user_list', fetch_user_list()); &nbsp;<br /><br />/** &nbsp;<br />* Set the fetched template of the inner template to the 'body' variable in &nbsp;<br />* the outer template. &nbsp;<br />*/ &nbsp;<br />$tpl-&gt;set('body', $body-&gt;fetch('user_list.tpl.php')); &nbsp;<br /><br />/** &nbsp;<br />* Echo the results. &nbsp;<br />*/ &nbsp;<br />echo $tpl-&gt;fetch('index.tpl.php'); &nbsp;<br />?&gt; </font>

其中有两个值得注意的重要的概念。第一个就是内部和外部模板的概念。外部模板包含定义站点主要外观的HTML代码。而内部模板包含定义站点内容区域的HTML代码。当然,你能够在任意数目的层上有任意数目的模板。因为通常我们给每个区域使用不同的模板对象,所以没有名字空间的问题。例如,我能在内部和外部模板中都有变量叫"title",而不用害怕有什么冲突。

这是一个用来显示用户列表的模板的简单例子。注意特殊的foreach和endforeach;语法在PHP手册中有说明 [8]。它完全是可选择的。

而且,你可能奇怪我为什么要用.php的后缀来命名我的模板文件。呵呵,许多PHP字节码缓存解决方案(比如 phpAccelerator)如果要被认成PHP文件,需要文件有一个.php后缀。因为这些模板是PHP文件,为什么不去获得这些好处?

&lt;table&gt; &nbsp;<br />&nbsp; &nbsp;&lt;tr&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;th&gt;Id&lt;/th&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;th&gt;Name&lt;/th&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;th&gt;Email&lt;/th&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;th&gt;Banned&lt;/th&gt; &nbsp;<br />&nbsp; &nbsp;&lt;/tr&gt; &nbsp;<br />&lt;? foreach($user_list as $user): ?&gt; &nbsp;<br />&nbsp; &nbsp;&lt;tr&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;&lt;?=$user['id'];?&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;&lt;?=$user['name'];?&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;&lt;a href=&quot;mailto:&lt;?=$user['email'];?&gt;&quot;&gt;&lt;?=$user['email'];?&gt;&lt;/a&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;&lt;?=($user['banned'] ? 'X' : '&nbsp;');?&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp;&lt;/tr&gt; &nbsp;<br />&lt;? endforeach; ?&gt; &nbsp;<br />&lt;/table&gt;

这个layout.tpl.php是一个简单的例子(定义了整个页面看上去是什么样子的模板文件)

&lt;html&gt; <br />&nbsp; &nbsp;&lt;head&gt; <br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;title&gt;&lt;?=$title;?&gt;&lt;/title&gt; <br />&nbsp; &nbsp;&lt;/head&gt; <br /><br />&nbsp; &nbsp;&lt;body&gt; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;h2&gt;&lt;?=$title;?&gt;&lt;/h2&gt; <br /><br />&lt;?=$body;?&gt; <br /><br />&nbsp; &nbsp;&lt;/body&gt; <br />&lt;/html&gt;

而这是解析后的输出。

<font face="新宋体">&lt;html&gt; <br />&nbsp;&lt;head&gt; <br />&nbsp; &nbsp;&lt;title&gt;User List&lt;/title&gt; <br />&nbsp;&lt;/head&gt; <br /><br />&nbsp;&lt;body&gt; <br /><br />&nbsp; &nbsp;&lt;h2&gt;User List&lt;/h2&gt; <br /><br />&lt;table&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;th&gt;Id&lt;/th&gt; <br />&nbsp; &nbsp;&lt;th&gt;Name&lt;/th&gt; <br />&nbsp; &nbsp;&lt;th&gt;Email&lt;/th&gt; <br />&nbsp; &nbsp;&lt;th&gt;Banned&lt;/th&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;1&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;bob&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;&lt;a href=&quot;mailto:bob@mozilla.org&quot;&gt;bob@mozilla.org&lt;/a&gt;&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;&nbsp;&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;2&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;judy&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;&lt;a href=&quot;mailto:judy@php.net&quot;&gt;judy@php.net&lt;/a&gt;&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;&nbsp;&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;3&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;joe&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;&lt;a href=&quot;mailto:joe@opera.com&quot;&gt;joe@opera.com&lt;/a&gt;&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;&nbsp;&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;4&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;billy&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;&lt;a href=&quot;mailto:billy@wakeside.com&quot;&gt;billy@wakeside.com&lt;/a&gt;&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;X&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;5&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;eileen&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;&lt;a href=&quot;mailto:eileen@slashdot.org&quot;&gt;eileen@slashdot.org&lt;/a&gt;&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td align=&quot;center&quot;&gt;&nbsp;&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&lt;/table&gt; <br />&nbsp;&lt;/body&gt; <br />&lt;/html&gt;</font>

缓存

因为解决方案简单如斯,实现模板缓存成为了一个非常简单的任务。为了实现缓存,我们有一个二级类,它扩展了原来的模板类。CachedTemplate类事实上使用和原来的模板类相同的API。不同点是我们必须传递缓存的设置给构造函数,并且调用<font face="新宋体">fetch_cache()</font>而不是<font face="新宋体">fetch()</font>

缓存的概念是简单的。简单的说,我们设置一个缓存时间来调表输出应该被保存的时长(以秒为单位)。在产生一个页面的所有工作开展之前,我们必须首先测试页面是否已经被缓存了,而且缓存是否仍然没有过期。如果缓存在这那,我们不需要在去麻烦数据库和业务逻辑来产生页面——我们可以简单地输出原先缓存地内容。

这种方法需要解决唯一地标识缓存文件的问题。如果一个站点是被一个显示基于GET变量的中心脚本所控制,对每个PHP文件只有一个缓存不会有什么帮助。例如,如果<font face="新宋体">index.php?page=about_us</font>和用户调用<font face="新宋体">index.php?page=contact_us</font>得到的显示完全不同。

问题是通过给每个页面产生一个唯一的<font face="新宋体">cache_id</font>来解决的。为了做到这个目的,我们把事实上被请求的文件变成<font face="新宋体">REQUEST_URI</font>(基本上就是整个URL:<font face="新宋体">index.php?foo=bar&amp;bar=foo</font>)。当然,这个转换过程是受到CachedTemplate类控制的,但是要记住的重要的事情是你绝对要在创建<font face="新宋体">CachedTemplate</font>对象时传递一个唯一的<font face="新宋体">cache_id</font>。当然下面有例子来说明。

使用缓存包括以下步骤。

 

  1. <font face="新宋体">include()</font> 模板源文件

     

     

  2. 创建一个新的<font face="新宋体">CachedTemplate</font>对象(并且传递路径,唯一的<font face="新宋体">cache_id</font>和缓存过期时间给模板)

     

     

  3. 测试内容是否已经被缓存了

     

     

  4. 如果还促拿了,显示文件并且结束脚本

     

     

  5. 否则,进行所有的处理并且<font face="新宋体">fetch()</font>模板

     

     

  6. <font face="新宋体">fetch_cache()</font>的调用将自动产生一个新的缓存文件

 

这个脚本假定你的缓存文件将放到<font face="新宋体">./cache/</font>中,因此你必须创建那个目录并且改变它的目录权限(<font face="新宋体">chmod</font>)使得Web服务器能够写入文件。而且还要注意如果你在编写脚本的过程中发现了错误,错误也会被缓存!因而在你开发的过程中禁用缓存是一个好主意。最好的办法是给cache的生存周期传递0——这样,缓存总是立即就失效了。

这是一个实际的缓存的例子。

<font face="新宋体">&lt;?php <br />/** <br />* Example of cached template usage. &nbsp;Doesn't provide any speed increase since <br />* we're not getting information from multiple files o&#114; a database, but it <br />* introduces how the is_cached() method works. <br />*/ <br /><br />/** <br />* First, include the template class. <br />*/ <br />require_once('template.php'); <br /><br />/** <br />* Here is the path to the templates. <br />*/ <br />$path = './templates/'; <br /><br />/** <br />* Define the template file we will be using for this page. <br />*/ <br />$file = 'list.tpl.php'; <br /><br />/** <br />* Pass a unique string for the template we want to cache. &nbsp;The template <br />* file name + the server REQUEST_URI is a good choice because: <br />* &nbsp; &nbsp;1. If you pass just the file name, re-used templates will all <br />* &nbsp; &nbsp; &nbsp; get the same cache. &nbsp;This is not the desired behavior. <br />* &nbsp; &nbsp;2. If you just pass the REQUEST_URI, and if you are using multiple <br />* &nbsp; &nbsp; &nbsp; templates per page, the templates, even though they are completely <br />* &nbsp; &nbsp; &nbsp; different, will share a cache file (the cache file names are based <br />* &nbsp; &nbsp; &nbsp; on the passed-in cache_id. <br />*/ <br />$cache_id = $file . $_SERVER['REQUEST_URI']; <br />$tpl = &amp; new CachedTemplate($path, $cache_id, 900); <br /><br />/** <br />* Test to see if the template has been cached. &nbsp;If it has, we don't <br />* need to do any processing. &nbsp;Thus, if you put a lot of db calls in <br />* here (or file reads, o&#114; anything processor/disk/db intensive), you <br />* will significantly cut the amount of time it takes for a page to <br />* process. <br />* <br />* This should be read aloud as &quot;If NOT Is_Cached&quot; <br />*/ <br />if(!($tpl-&gt;is_cached())) { <br />&nbsp; &nbsp;$tpl-&gt;set('title', 'My Title'); <br />&nbsp; &nbsp;$tpl-&gt;set('intro', 'The intro paragraph.'); <br />&nbsp; &nbsp;$tpl-&gt;set('list', array('cat', 'dog', 'mouse')); <br />} <br /><br />/** <br />* Fetch the cached template. &nbsp;It doesn't matter if is_cached() succeeds <br />* o&#114; fails - fetch_cache() will fetch a cache if it exists, but if not, <br />* it will parse and return the template as usual (and make a cache for <br />* next time). <br />*/ <br />echo $tpl-&gt;fetch_cache($file); <br />?&gt;</font>

设置多个变量

我们如何能够同时设置多个变量?这又一个使用由Ricardo Garcia贡献的函数的例子。

<font face="新宋体">&lt;?php &nbsp;<br />require_once('template.php'); &nbsp;<br /><br />$tpl = &amp; new Template('./templates/'); &nbsp;<br />$tpl-&gt;set('title', 'User Profile'); &nbsp;<br /><br />$profile = array( &nbsp;<br />&nbsp; &nbsp;'name' =&gt; 'Frank', &nbsp;<br />&nbsp; &nbsp;'email' =&gt; 'frank@bob.com', &nbsp;<br />&nbsp; &nbsp;'password' =&gt; 'ultra_secret' &nbsp;<br />); &nbsp;<br /><br />$tpl-&gt;set_vars($profile); &nbsp;<br /><br />echo $tpl-&gt;fetch('profile.tpl.php'); &nbsp;<br />?&gt; </font>

相关的模板是这样的:

<font face="新宋体">&lt;table cellpadding=&quot;3&quot; border=&quot;0&quot; cellspacing=&quot;1&quot;&gt; &nbsp;<br />&nbsp; &nbsp;&lt;tr&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;Name&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;&lt;?=$name;?&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp;&lt;/tr&gt; &nbsp;<br />&nbsp; &nbsp;&lt;tr&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;Email&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;&lt;?=$email;?&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp;&lt;/tr&gt; &nbsp;<br />&nbsp; &nbsp;&lt;tr&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;Password&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&lt;td&gt;&lt;?=$password;?&gt;&lt;/td&gt; &nbsp;<br />&nbsp; &nbsp;&lt;/tr&gt; &nbsp;<br />&lt;/table&gt; </font>

而且解析后的输出是这样的:

<font face="新宋体">&lt;table cellpadding=&quot;3&quot; border=&quot;0&quot; cellspacing=&quot;1&quot;&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td&gt;Name&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;Frank&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td&gt;Email&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;frank@bob.com&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&nbsp;&lt;tr&gt; <br />&nbsp; &nbsp;&lt;td&gt;Password&lt;/td&gt; <br />&nbsp; &nbsp;&lt;td&gt;ultra_secret&lt;/td&gt; <br />&nbsp;&lt;/tr&gt; <br />&lt;/table&gt;</font>

特别感谢Ricardo Garcia和Harry Fuecks他们的对这篇文章的贡献。

相关的链接

这儿是一个总体上探究模板引擎的好去处的列表。

 

 

模板类源代码

以及最后出场的,模板类。

<font face="新宋体">&lt;?php <br />/** <br />* Copyright &copy; 2003 Brian E. Lozier (brian@massassi.net) <br />* <br />* set_vars() method contributed by Ricardo Garcia (Thanks!) <br />* <br />* Permission is hereby granted, free of charge, to any person obtaining a copy <br />* of this software and associated documentation files (the &quot;Software&quot;), to <br />* deal in the Software without restriction, including without limitation the <br />* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or <br />* sell copies of the Software, and to permit persons to whom the Software is <br />* furnished to do so, subject to the following conditions: <br />* <br />* The above copyright notice and this permission notice shall be included in <br />* all copies o&#114; substantial portions of the Software. <br />* <br />* THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS o&#114; <br />* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, <br />* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE <br />* AUTHORS o&#114; COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES o&#114; OTHER <br />* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT o&#114; OTHERWISE, ARISING <br />* FROM, OUT OF o&#114; IN CONNECTION WITH THE SOFTWARE o&#114; THE USE o&#114; OTHER DEALINGS <br />* IN THE SOFTWARE. <br />*/ <br /><br />class Template { <br />&nbsp; &nbsp;var $vars; /// Holds all the template variables <br />&nbsp; &nbsp;var $path; /// Path to the templates <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Constructor <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param string $path the path to the templates <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return void <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function Template($path = null) { <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;path = $path; <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;vars = array(); <br />&nbsp; &nbsp;} <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Set the path to the template files. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param string $path path to template files <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return void <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function set_path($path) { <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;path = $path; <br />&nbsp; &nbsp;} <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Set a template variable. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param string $name name of the variable to set <br />&nbsp; &nbsp; * @param mixed $value the value of the variable <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return void <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function set($name, $value) { <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;vars[$name] = $value; <br />&nbsp; &nbsp;} <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Set a bunch of variables at once using an associative array. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param array $vars array of vars to set <br />&nbsp; &nbsp; * @param bool $clear whether to completely overwrite the existing vars <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return void <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function set_vars($vars, $clear = false) { <br />&nbsp; &nbsp; &nbsp; &nbsp;if($clear) { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;vars = $vars; <br />&nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp; &nbsp; &nbsp;else { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(is_array($vars)) $this-&gt;vars = array_merge($this-&gt;vars, $vars); <br />&nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp;} <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Open, parse, and return the template file. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param string string the template file name <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return string <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function fetch($file) { <br />&nbsp; &nbsp; &nbsp; &nbsp;extract($this-&gt;vars); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Extract the vars to local namespace <br />&nbsp; &nbsp; &nbsp; &nbsp;ob_start(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Start output buffering <br />&nbsp; &nbsp; &nbsp; &nbsp;include($this-&gt;path . $file); &nbsp;// Include the file <br />&nbsp; &nbsp; &nbsp; &nbsp;$contents = ob_get_contents(); // Get the contents of the buffer <br />&nbsp; &nbsp; &nbsp; &nbsp;ob_end_clean(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// End buffering and discard <br />&nbsp; &nbsp; &nbsp; &nbsp;return $contents; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Return the contents <br />&nbsp; &nbsp;} <br />} <br /><br />/** <br />* An extension to Template that provides automatic caching of <br />* template contents. <br />*/ <br />class CachedTemplate extends Template { <br />&nbsp; &nbsp;var $cache_id; <br />&nbsp; &nbsp;var $expire; <br />&nbsp; &nbsp;var $cached; <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Constructor. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param string $path path to template files <br />&nbsp; &nbsp; * @param string $cache_id unique cache identifier <br />&nbsp; &nbsp; * @param int $expire number of seconds the cache will live <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return void <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function CachedTemplate($path, $cache_id = null, $expire = 900) { <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;Template($path); <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;cache_id = $cache_id ? 'cache/' . md5($cache_id) : $cache_id; <br />&nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;expire &nbsp; = $expire; <br />&nbsp; &nbsp;} <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * Test to see whether the currently loaded cache_id has a valid <br />&nbsp; &nbsp; * corrosponding cache file. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return bool <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function is_cached() { <br />&nbsp; &nbsp; &nbsp; &nbsp;if($this-&gt;cached) return true; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp;// Passed a cache_id? <br />&nbsp; &nbsp; &nbsp; &nbsp;if(!$this-&gt;cache_id) return false; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp;// Cache file exists? <br />&nbsp; &nbsp; &nbsp; &nbsp;if(!file_exists($this-&gt;cache_id)) return false; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp;// Can get the time of the file? <br />&nbsp; &nbsp; &nbsp; &nbsp;if(!($mtime = filemtime($this-&gt;cache_id))) return false; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp;// Cache expired? <br />&nbsp; &nbsp; &nbsp; &nbsp;if(($mtime + $this-&gt;expire) &lt; time()) { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@unlink($this-&gt;cache_id); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false; <br />&nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp; &nbsp; &nbsp;else { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/** <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Cache the results of this is_cached() call. &nbsp;Why? &nbsp;So <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * we don't have to double the overhead for each template. <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If we didn't cache, it would be hitting the file system <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * twice as much (file_exists() &amp; filemtime() [twice each]). <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */ <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;cached = true; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true; <br />&nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp;} <br /><br />&nbsp; &nbsp;/** <br />&nbsp; &nbsp; * This function returns a cached copy of a template (if it exists), <br />&nbsp; &nbsp; * otherwise, it parses it as normal and caches the content. <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @param $file string the template file <br />&nbsp; &nbsp; * <br />&nbsp; &nbsp; * @return string <br />&nbsp; &nbsp; */ <br />&nbsp; &nbsp;function fetch_cache($file) { <br />&nbsp; &nbsp; &nbsp; &nbsp;if($this-&gt;is_cached()) { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$fp = @fopen($this-&gt;cache_id, 'r'); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$contents = fread($fp, filesize($this-&gt;cac
he_id)); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fclose($fp); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $contents; <br />&nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp; &nbsp; &nbsp;else { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$contents = $this-&gt;fetch($file); <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Write the cache <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($fp = @fopen($this-&gt;cache_id, 'w')) { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fwrite($fp, $contents); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fclose($fp); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else { <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;die('Unable to write cache.'); <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $contents; <br />&nbsp; &nbsp; &nbsp; &nbsp;} <br />&nbsp; &nbsp;} <br />} <br />?&gt;</font>

另外一个值得注意的重要的事情是这里展示的解决办法是我们传递模板的文件名给<font face="新宋体">fetch()</font>函数。如果你需要重用模板对象而不去<font face="新宋体">re-set()</font>所有的变量,这将比较有用。

并且记住:模板引擎的要点是把你的业务逻辑从你的表现逻辑中分离出来,而不是把你的PHP代码从HTML代码中分离出来。

本文附件下载:template.zip

[1] http://www.massassi.com/bTemplate/
[2] http://smarty.php.net/
[3] http://www.sitepoint.com/examples/tempeng/template.zip
[4] http://opensource.org/licenses/mit-license.html
[5] http://www.opensource.org/
[6] http://zend.com/store/products/zend-performance-suite.php
[7] http://www.php-accelerator.co.uk/
[8] http://www.php.net/manual/en/control-structures.alternative-syntax.php
[9] http://wact.sourceforge.net/index.php/TemplateView
[10] http://www.phppatterns.com/index.php/article/articleview/11/
[11] http://simplet.sourceforge.net/
[12] http://phppatterns.com/index.php/article/articleview/4/1/1/
[13] http://smarty.php.net/

本文英文原版地址:http://www.sitepoint.com/article/1218/

目前还没有任何评论.
您必须在 登录 后才能发布评论.