PHP3 之 FastTemplate
--------------------------------------------------------------------------------
 
作者:LinuxBoy & NTBoy 来源:网易虚拟社区 
当你在建立一个站点的时候,可能不得不面对以下现实: 
站点需要一名程序员来设计程序和一名网站设计者来组织页面。那么,是否有使两者很好的结合起来的方法呢? 有的,就是使用模板(FastTemplate:这个程序在本站的“程序和代码”中可以找到),这将会使你的工作更加轻松。 下面解释一下使用模板的好处: 1、在很短的时间内可以替换整个站点的外观 
2、使程序员可以抽象编程,而无须接触HTML代码 
3、速度非常之快 
4、可以重复利用以前的模板 模板的来历:FastTemplate来自于同名的Perl软件包(在CPAN上可以找到)。 它被移植到了PHP3平台上。你只需要一个基础类文件class.FastTemplate.php3 先解释一下使用模板和使用echo或print命令建立html页面的区别,echo和 print在编写短小的脚本时显得非常实用,但是做出来的脚本的组织性和可定制性不好,修改起来颇废工夫,模板在编写支持多国语种的站点时的效率则要高的多,如用echo和print可以想象工作量之大。 不用担心,熟练使用FastTemplate会占去你一部分时间,但是这些时间会在你 以后的工作中弥补回来,尤其是大工程时。 那么,怎么使用FastTemplate呢? 第一步,只需使用函数<?php $tpl = new FastTemplate( "path"); ?>,其中path指向template目录所在的路径。这句函数建立了一个$tpl对象,以后可以对它指定参数,进行处理或用来建立各种页面等等。 FastTemplate是建立在这么一种理论基础之上的,即假定一个web页是由许多小的部分组成的。举例来说,如WEB页细分为TITLE、PAGE、FOOT等等。整个页被赋予一个变量名,每一个小部分又被赋予一个变量名,最小的不可分割的部分通常就是字符串了,它也被赋予一个变量名。具体处理的时候,是一层层的包含关系。被包含部分在上一层中以宏{NAME}的形式出现。最后经过一层层的向上输出,得到完整的页面。 那么最底层的向字符串赋值的函数是什么呢,就是: <?php $tpl->assign(NAME, "text"); ?> 通过这个功能,FastTemplate将字符串text赋予了变量NAME,以后上一层就可替换宏{NAME}的内容为text。 例如: $tpl->assign(NAME, "me"); 这就将变量NAME赋值了字符串"me"。 第二步,$tpl需要知道其所调用的所有模板文件,即各个小部分。这个功能由define一个数组实现: <?php $tpl->define(); ?> 例如: <?php $tpl->define(array(foo => "foo.tpl", bar => "bar.tpl")); ?> 这说明总共包括了两个模板文件:foo.tpl和bar.tpl,并给它们指定了名字foo和bar。 有了第一节的知识以后,你现在是否想试一试将模板文件中所包含的宏 
{MACROS}部分按照自己定义的变量替换呢?使用以下的命令即可实现: <?php $tpl->parse(PAGECONTENT, "foo"); ?> 这条命令的具体意义是: 我们首先已经用assign定义了FOO模板中包含的若干宏变量,然后根据这些变量对模板文件FOO进行替换处理,并将替换后的模板文件赋予另一个变量名PAGECONTENT。 完整如下: <?php $tpl->assign(NAME, "me"); 
$tpl->parse(PAGECONTENT, "foo"); ?> 当然,我们还没有作完,因为bar模板文件才是WEB的主输出部分,BAR模板包含着FOO模板,BAR中还包含着宏变量{PAGETITLE}和{PAGECONTENT}等待处理,PAGECONTENT经过对FOO的处理已经得到,PAGETITLE还未指定,所以还要还要指定PAGETITLE,并调用函数 <?php $tpl->parse(MAIN, "bar"); ?> 进行处理,并将处理后的结果赋予变量MAIN。 如下: <?php $tpl->assign(PAGETITLE, "FooBar test"); 
$tpl->parse(MAIN, "bar"); ?> 很简单吧,最后我们只需输出页面即可: <?php $tpl->FastPrint(MAIN); ?> 下面就是foo.tpl,bar.tpl与最终的demo.php3文件。 
请大家仔细琢磨琢磨: ------------------------------------------------------------- 
foo.tpl <!-- foo.tpl --> 
This does not do anything obvious. Please look at {NAME}. ------------------------------------------------------------- 
bar.tpl <!-- bar.tpl --> 
<HTML> 
<HEAD><TITLE>Feature world - {PAGETITLE}</TITLE></HEAD> 
<BODY BGCOLOR=BLACK TEXT=WHITE> 
<H1>{PAGETITLE}</H1> 
{PAGECONTENT} 
</BODY> 
</HTML> ------------------------------------------------------------ 
demo.php3 <?php include "class.FastTemplate.php3"; $tpl = new FastTemplate( "."); 
$tpl->define(array(foo => "foo.tpl", bar => "bar.tpl")); $tpl->assign(NAME, "me"); 
$tpl->parse(PAGECONTENT, "foo"); $tpl->assign(PAGETITLE, "Welcome!"); 
$tpl->parse(MAIN, "bar"); $tpl->FastPrint(MAIN); ?> 
------------------------------------------------------------ 编制一个表格的例子: 经过以上的阐述,大家是否已经明白一点了。 
下面是一个处理表格的例子,首先我们再学一些新知识。 当我们当处理完foo模板后,并赋予了变量TPL1,我们可以将bar模板的内容处理后追加进TPL1中,这样就不必定义过多变量,也易于理解,例处理完页面的title后将content部分追加进去,最后将foot追加,生成完整一个页面再输出。这条命令就是: 
<?php $tpl->parse(TPL1, ".bar"); ?> 
其中的.表示追加。 如下: <?php # 处理模板foo,并赋予变量TPL1 
$tpl->parse(TPL1, "foo"); # 处理模板bar,并追加进变量TPL1 
$tpl->parse(TPL1, ".bar"); ?> 下面是一个完整的表格例子,大家好好揣摸揣摸 page.tpl <HTML> 
<HEAD><TITLE>Feature world - {PAGE_TITLE}</TITLE></HEAD> 
<BODY BGCOLOR=BLACK TEXT=WHITE> 
<H1>{PAGE_TITLE}</H1> 
{PAGE_CONTENT} 
</BODY> 
</HTML> table.tpl <TABLE> 
<TR> <TH>name</TH> <TH>size</TH> </TR> 
{TABLE_ROWS} 
</TABLE> table_row.tpl <TR> 
<TD>{FILENAME}</TD> 
<TD>{FILESIZE}</TD> 
</TR> yad.php3 <?php include "class.FastTemplate.php3"; function InitializeTemplates() { 
global $tpl; $tpl = new FastTemplate( "."); 
$tpl->define( 
array( 
page => "page.tpl", 
table => "table.tpl", 
table_row => "table_row.tpl" 

); 
} function ReadCurrentDirectory() { 
global $tpl; $handle = opendir( "."); 
while($filename = readdir($handle)) { 
$tpl->assign(FILENAME, $filename); 
$tpl->assign(FILESIZE, filesize($filename)); 
$tpl->parse(TABLE_ROWS, ".table_row"); 

closedir($handle); 
$tpl->parse(PAGE_CONTENT, "table"); 
} function PrintPage($title) { 
global $tpl; $tpl->assign(PAGE_TITLE, $title); 
$tpl->parse(FINAL, "page"); 
$tpl->FastPrint(FINAL); 
} InitializeTemplates(); 
ReadCurrentDirectory(); 
Printpage( "Yet Another Demo"); ?> 关于速度的最后一点讨论: 看完以上的例子,你会说“太棒了!漂亮,但是,速度怎么样呢?” 没问题,你的站点会变得很快。简单的说:因为你是一个程序员,你应该关注于程序代码的设计,代码应该更加有效率,应容易修改和容易理解。使用FastTemplate可以可以帮助你做到这一点,所以它使你的工作更加轻松一些。 如果你想替代一个已经建好的Web站点,我们建议使用regex(替换表达式)来替换,实际上FastTemplate使用的就是regex 来替换模板中的宏。 

解决方案 »

  1.   

    实例学习PHP之FastTemplate模板篇
    --------------------------------------------------------------------------------
     
    作者:地藏 来源:ASP酷 
       如果你从来没有接触过PHP,那么还是先看看这个吧,当然即使是你已经对PHP有所了解,但一本PHP4的的使用手册也还是需要的,:)。此外一本HTML语法手册当然也是不可缺少的啦。 
       
         在网站开发过程中你是不是经常面对改版的苦恼?几百几千个文件因为版式上的一点小变化就需要全部重新处理,是不是让你头痛无比?唉,如果能够把内容和表现形式分开就好了,这可是我们一直翘首等待的。但可惜用于处理这个问题的XML还未完全的成熟。难道除此之外就没有办法了吗?正所谓东西是死的,人却是活的,我们今天要学习的这个php库,就可以帮助我们在一定程度上处理这个问题。:)) 
       
         FastTemplate是什么?从PHP语言上来讲它是一个PHP库;从它的起源来说它源于一个同样名称的Perl软件包;从用途上来讲是一个可以让你在几秒内改变整个站点外观的实用工具。用最通俗的语言来说,它就是一个模板,一个类似DreamWaver中的模板。现在FastTemplate在你心里是一个问号?还是一个感叹号?又或是一个句号?(编辑:靠,在这骗稿费呀,我扁!)算了,不管那么多,你只要知道他是好东西就成,:) 
       
         首先在我们使用这个库以前当然要先下载它,大家可以在下面这个网址http://www.thewebmasters.net/php/下载它(本站下载地址为: http://www.phpe.net/downloads/1.shtml)。下载之后呢把它解压缩到你的web服务器的一个目录上,下面是解压缩后的目录结构 
       
      FastTemplate-1.1.0/ 
      FastTemplate-1.1.0/README < - 这个文件就不用说了吧? 
      FastTemplate-1.1.0/class.FastTemplate.php3 < - 这个文件最重要,它就库文件,核心耶! 
      FastTemplate-1.1.0/example_1.phtml < - 一个例子 
      FastTemplate-1.1.0/example_2.phtml < - 第二个例子 
      FastTemplate-1.1.0/example_3.phtml < - 第三个例子 
      FastTemplate-1.1.0/dynamic_example.phtml < - 第四个例子 
       
      FastTemplate-1.1.0/docs/ < - 文档目录 
      FastTemplate-1.1.0/docs/FastTemplate.3 < - Unix man page 
      FastTemplate-1.1.0/docs/FastTemplate.html < - HTML documentation 
       
      FastTemplate-1.1.0/templates/ < - 模板例子的目录 
      FastTemplate-1.1.0/templates/begin.tpl 
      FastTemplate-1.1.0/templates/header.tpl 
      FastTemplate-1.1.0/templates/main.tpl 
      FastTemplate-1.1.0/templates/row.tpl 
      FastTemplate-1.1.0/templates/test.tpl 
      FastTemplate-1.1.0/templates/footer.tpl 
      FastTemplate-1.1.0/templates/htaccess.tpl 
      FastTemplate-1.1.0/templates/middle.tpl 
      FastTemplate-1.1.0/templates/table.tpl 
         注意哟,这个目录一定要是php程序可以访问的目录哟,换句话说就是php.ini里那个include目录。然后呢,使用php4编程的朋友注意了,你现在还不能直接使用这个库,还需要人工做些修改!等下注意看下面。php3的读者就不用管那么多,现在就可以试一下它带的那几个例子了,(嘿嘿,不过呢,它那些例子的后缀名全是phtml,如果你没办法设置web的话。你可以尝试把后缀名改改看,应该也可以用,地藏没有试过不敢打包票。)那!接着呢,php4的兄弟可千万要注意哟,如果不你不按下面的修改的话,这个库可是没有办法用的哟!! 
       
      --- class.FastTemplate.php3 Sun Jun 27 13:44:47 1999 
      +++ php4.FastTemplate.php3 Tue Jul 20 10:49:25 1999 
      @@ -196,8 +196,10 @@ 
       settype($val,"string"); 
       } 
       
      - $template = ereg_replace("{$key}","$val","$template"); 
      - //$template = str_replace("{$key}","$val","$template"); 
      + // php4 doesn't like '{$' combinations. 
      + $key = '{'."$key".'}'; 
      + $template = ereg_replace("$key","$val","$template"); 
      + //$template = str_replace("$key","$val","$template"); 
       } 
       } 
       
      @@ -410,7 +412,7 @@ 
       } 
       if($end) 
       { 
      - $newParent .= "{$MacroName} 
      "; 
      + $newParent .= '{'."$MacroName} 
      "; 
       } 
       // Next line please 
       if($end) { $end = false; } 
       
         大家用文本编辑器打开class.FastTemplate.php3文件,找到上面的部分。'-'减号代表消除这行,'+'加号代表加入这行。另外windows系统下的朋友注意哟!你们还需要改点小东西,把下面的那个$win32变量的值改成true,不改的话,用不了可别骂我,;)。 
       
         var $WIN32 = true; // Set to true if this is a WIN32 server 
       
         改好后,大家就可以试试他的那几个例子了。 如何?成功了吧。嘿嘿嘿嘿~~~,地藏不会骗大家的啦。 
       
         OK,现在这个库已经可以用啦,我们准备进行下面的学习吧,:) 
       
         如何使用这个库?简单哟,首先要把这个库包含进来。也就是说呢,要 include "class.FastTemplate.php3"; 然后?呵呵…… 然后我也不知道啦!(编辑:"嘿!找扁啊,你!")啊,啊…!Sorry,我想起来了(在地藏被众编辑海扁一顿之后)。然后是学习这个库的使用原理。在这里呢我们先做个假设。 
       
         我们假设一个页面是由很多的小的部分组成(比如:每个网站都有分栏目导航等等),而且每个小的部分都有一个唯一的标识符(比如:我们把分栏每个小项定义为一个名字) 
       
         我们首先以这个库自带的example_1为例,让大家可以尽快的学会这个库的基本用法,;),不过大家要注意哟,这几个文件在服务器上的位置要按解压时的相对路径来存,千万不要忘记,就象不要忘记你女朋友的生日一样。呵呵~~~~ 
       
         < ? 
       
         // Example FastTemplate Demo #1 - The example from the man page 
       
         Header("Content-type: text/plain"); 
       
         include("class.FastTemplate.php3"); 
      $tpl = new FastTemplate("./templates"); 
       
         $tpl- >define( 
      array( 
      main = > "main.tpl", 
      table = > "table.tpl", 
      row = > "row.tpl" 
      ) 
      ); 
       
         $tpl- >assign( array( TITLE = > "FastTemplate Test") ); 
       
         for ($n=1; $n < = 3; $n++) 
      { 
      $Number = $n; 
      $BigNum = $n*10; 
      $tpl- >assign( 
      array( 
      NUMBER = > $Number, 
      BIG_NUMBER = > $BigNum 
      ) 
      ); 
      $tpl- >parse(ROWS,".row"); 
      } 
       
         $tpl- >parse(MAIN, array("table","main")); 
       
         $tpl- >FastPrint(); 
       
         exit; 
       
         ? > 
       
         < !-- NAME: main.tpl -- > 
      < html > 
      < head >< title > {TITLE} < /title > 
      < /head > 
      < body > 
      {MAIN} 
      < /body > 
      < /html > 
      < !-- END: main.tpl -- > 
       
         < !-- NAME: table.tpl -- > 
      < table border='1' > 
      {ROWS} 
      < /table > 
      < !-- END: table.tpl -- > 
       
         < !-- NAME: row.tpl -- > 
      < tr > 
      < td >{NUMBER}< /td > 
      < td >{BIG_NUMBER}< /td > 
      < /tr > 
      < !-- END: row.tpl -- > 
       
      

  2.   

    使用这个库首先要如前面所说的 include "class.FastTemplate.php3"; 然后是定义模板所在目录$tpl = new FastTemplate("./templates");注意哟!这里因为我是在windows系统运行,所以用的是"./templates",在Linux或UNIX中可能不一样,大家根据具体情况来设定。然后呢,我们对应不同的模板,定义下面这个矩阵数组。$tpl- >define( array( main = > "main.tpl", table = > "table.tpl", row = > "row.tpl" ) ); define是这个类中的一个函数。下面是它的语法:define( array( key,value pairs) ) ,define()函数映射一个名字到模板文件上,这个新的名字将是你用来代表模板的唯一名字,因为除此之外再不会有模板文件名出现。而且大家注意,这是使用类时决不能缺少的步骤,不能少的哟,如果少了那就和你不带生日礼物去你女朋友的生日Party有异曲同工之妙,嘿嘿嘿~~~~ 
       
         现在!关键的,最有个性的东西来了!assign( (key,value pair) 或 assign ( array(key value pairs) ) 这个函数将把你在模板中定义的标识符定义为你网页上真正想要的东西。比如就象是上面$tpl- >assign( array( TITLE = > "FastTemplate Test") ); 这句,将模板main.tpl里的{TITLE}替换为 FastTemplate Test 看不懂想不明的朋友多看看上面的那些代码。接着呢!接着是另外一个很有特色的东东。parse(RETURN, FileHandle(s) ) 将一个已定义模板插入的定义到另外一个模板文件中。大家仔细研究下面的代码,相信会比较容易了解。 
       
         for ($n=1; $n < = 3; $n++) 
      { 
      $Number = $n; 
      $BigNum = $n*10; 
      $tpl- >assign( 
      array( 
      NUMBER = > $Number, 
      BIG_NUMBER = > $BigNum 
      ) 
      ); 
      $tpl- >parse(ROWS,".row"); 
      } 
       
         parse这个函数有三种用法 
       
         $tpl- >parse(MAIN, "main"); // 标准 
      $tpl- >parse(MAIN, array ( "table", "main") ); // 简洁 
      $tpl- >parse(MAIN, ".row"); // 增加 
       
         其中以第三种最有意思,它表示的是在原来已经有的基础上再加上一个新数据。呵呵,这么说大家可能无法明白,我们还是看看再多研究一下上面的源代码吧,毕竟编程这东西有很多是只可意会不可言传的!(下面附上英文的说明,不是地藏想偷懒不翻译,实在是有些东西看原味的比翻译的更加容易理解呀, 
       
         In the regular version, the template named ``main'' is loaded if it hasn't been already, all the variables are interpolated, and the result is then stored in FastTemplate as the value MAIN. If the variable '{MAIN}' shows up in a later template, it will be interpolated to be the value of the parsed ``main'' template. This allows you to easily nest templates, which brings us to the compound style. 
       
         The compound style is designed to make it easier to nest templates. 
       
         The following are equivalent: 
       
         $tpl- >parse(MAIN, "table"); 
       
         $tpl- >parse(MAIN, ".main"); 
       
         // is the same as: 
       
         $tpl- >parse(MAIN, array("table", "main")); 
       
         // this form saves function calls and makes your code cleaner 
       
         It is important to note that when you are using the compound form, each template after the first, must contain the variable that you are parsing the results into. In the above example, 'main' must contain the variable '{MAIN}', as that is where the parsed results of 'table' is stored. If 'main' does not contain the variable '{MAIN}' then the parsed results of 'table' will be lost. The append style allows you to append the parsed results to the target variable. Placing a leading dot . before a defined file handle tells FastTemplate to append the parsed results of this template to the returned results. This is most useful when building tables that have an dynamic number of rows - such as data from a database query. ) 
       
         最后在完成上面的过程后,就可以进行输出了,FastTemplate库使用使用FastPrint(HANDLE) 来进行输出。 
       
         $tpl- >FastPrint(); 
       
         在经过上面的这些步骤后,大家对使用FastTemplate库的过程是否清楚明白呢?地藏试试把上面的过程总结一下,看看对各位朋友是否有帮助: 
       
         首先:将库用include加进来 
       
         其次:定义一个类变量,在上例中是$tpl 
       
         再次:定义各种最小元素,比如上面的$tpl = new FastTemplate("./templates"); 
       
         然后:利用parse函数将各种最小元素进行组合起来 
       
         最后:用FastPrint()进行输出