这个类里面比较关键的$main , $vars , function output(),第一个记录了将要输出的模板(其实是一个php文件),第二个以数组方式保存所谓的“模板变量”,第三个是将模板内容输出到浏览器。在我的思路里面,所谓的“模板”不再是单纯的HTML和模板语言结合的文件,而是一个传统的PHP与HTML混合的文件,在模板类向浏览器输出内容的时候,把这个“模板”include进来就可以了,下面就是一个我项目里的“模板”文件:
CODE
<?php include($t.'include/header.php');?>
<?php if ( $_popup ) { ?>
<?=$_pop_window?>
<?php } ?>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tbody>
<tr>
 <td width="479" height="34" background="<?=$_tpl_path?>images/index_05.gif"> 
<?php if ( $_tpl_cfg_show_search ) { ?>
<?php include($t.'include/search.php');?>
<?php } ?> </td>
 <td width="301" background="<?=$_tpl_path?>images/index_05.gif"><IMG SRC="<?=$_tpl_path?>images/index_09.gif" WIDTH=301 HEIGHT=34 ALT=""></td>
</tr>
</tbody>
</table>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
</tbody>
<tr>
 <td width="213" height="404" valign="top" background="<?=$_tpl_path?>images/index_20.gif"> 
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td><IMG SRC="<?=$_tpl_path?>images/index_13.gif" WIDTH=237 HEIGHT=23 ALT=""></td>
   </tr>
  </table><?php if ( $_tpl_cfg_show_vote ) { ?>
  <table width="237" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="black-12-20">
      <tr> 
       <td width="16%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
       <td width="80%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_vote_module_disp_name?></strong></td>
       <td width="4%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
      </tr>
     </table></td>
   </tr>
   <tr>
    <td>
 <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr> 
       <td height="9"><img src="<?=$_tpl_path?>images/spacer.gif" width="1" height="1"></td>
      </tr>
     </table> 
 <?php include($t.$_vote_module_file);?>
 </td>
   </tr>
  </table>
<?php } ?><?php if ( $_tpl_cfg_show_link ) { ?>
  <table width="237" border="0" cellspacing="0" cellpadding="0">
   <tr> 
    <td>
 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="black-12-20">
      <tr> 
       <td width="16%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
       <td width="80%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_link_module_disp_name?></strong></td>
       <td width="4%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
      </tr>
     </table>
 </td>
   </tr>
   <tr> 
    <td ><?php include($t.$_link_module_file);?></td>
   </tr>  </table>
  <table width="33%" border="0" align="center" cellpadding="0" cellspacing="0">
   <tr>
    <td height="66">
 <?php if ( $_tpl_cfg_enable_service_module ) { ?>
<div align="center"><a href="javascript:" onClick="MM_openBrWindow('<?=$_common_files?>service/fuwutai.php','','width=531,height=373')"><img src="<?=$_tpl_path?>images/service.gif" width="177" height="49" border='0'></a></div>
<?php } ?>
</td>
   </tr>
  </table> 
 <?php } ?>
 </td>
 <td width="567" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td><IMG SRC="<?=$_tpl_path?>images/index_14.gif" WIDTH=543 HEIGHT=23 ALT=""></td>
   </tr>
  </table><?php if ( $_tpl_cfg_show_news ) { ?>
  <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
   <tr> 
    <td colspan="2">
 <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="black-12-20">
      <tr> 
       <td width="2%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
       <td width="93%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_news_module_disp_name?></strong></td>
       <td width="5%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
      </tr>
     </table>
 </td>
   </tr>
<tr>
 <td colspan='2' align='center'>
 <?php include($t.$_news_module_file);?>
 </td>
</tr>  </table> <?php } ?> <?php if ( $_tpl_cfg_show_prod ) { ?>
  <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
   <tr> 
    <td colspan="2"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="black-12-20">
      <tr> 
       <td width="2%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
       <td width="93%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_prod_module_disp_name?></strong></td>
       <td width="5%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
      </tr>
     </table></td>
   </tr>
   <tr> 
    <td width="100%" height="121" valign="top" colspan='2'> 
 <?php include($t.$_prod_module_file);?>
    </td>
     </table> <?php } ?> </td>
   </tr>
  </table>
 </td>
</tr>
</tbody>
</table>
<?php include($t.'include/foot.php');?>
</body>
</html>
我从这样的修改中获得了下列好处:
1、效率
根据我的调试,在用Smarty之前,项目首页在我的本本上基本上要消耗0.1x~0.2x秒之间,放弃Smarty以后,基本稳定在0.07x以下2、更加精简的PHP文件以上就是我通过我的项目对模板技术的反思和修改,请兄弟们PP^^

解决方案 »

  1.   

    还是网页和PHP代码分开比较好啊。不然又要编代码又要做网页,很累的。
      

  2.   

    难道楼上的都觉得美工看到PHP标签会比模板标签爽吗?我接触到的美工似乎觉得都差不多。
      

  3.   

    刚接触模板,小声问一下
    phplib是不是比Smarty小一些?不是“庞然大物”
      

  4.   

    你是不是没有理解编译性模板?简单的说就是把模板语言翻译成HTML跟PHP混合编排,不是你这么理解:“如果你去看看Smarty缓存文件,或许你会觉得,里面的代码象你刚学php的时候写的……”,缓存文件就是编译以后的根本不是给你看的,就好比你写个C语言程序,运行都是.exe文件,你看exe文件干什么?对于编译过的文件,Smarty是这么做的:搜索编译文件是否存在,存在则包含进来,不然重新编译生成新的,你觉得它这样做效率很低吗?只能说你没有理解Smarty,不要怀疑这么多程序员智慧的结晶,先怀疑一下自己。再说,你这种代码:
    <?php if ( $_tpl_cfg_show_search ) { ?>
    <?php include($t.'include/search.php');?>
    <?php } ?>
    肯定没有
    <IfConfigShowSearch>
    <Include Filename="include/search.php">
    </IfConfigShowSearch>
    这样的模板语言简练好看,还能定义成XML格式的,这就是Smarty的扩展性很好。
      

  5.   

    楼上的曲解了一部分我的意思,我说“如果你去看看Smarty缓存文件,或许你会觉得,里面的代码象你刚学php的时候写的……”,是指PHP和HTML混合的方式,在此并没有贬低Smarty的意思。不过我还是觉得,任何模板技术,都不及单纯的php与html混合效率高。
      

  6.   

    单纯从效率来说,当然是混合写高,但是从可读性,结构性,扩展性和维护性来说混合写都不是最佳的。Smarty之所以这么做也是经过考虑的,不是你觉得不好就能否认的。只能说明没有实践经验,没遇到复杂的情况,建议你可以看看许多优秀的PHP开源代码。举个例子给你看看:
    比如字符串的输出,可以有很多格式:
    如果需要全部大小输出:按照你的逻辑应该是:  <?= $var ?> 
    这样所有需要大写输出的地方你都得加: <?php $var = touppercase($var);?> 那什么时候需要输出什么时候不需要输出都是你规定的,美工怎么知道?
    而用Smarty这些都可以全部统一的:{$var upppercase="true"}
    还有日期格式的输出:{$var format="xxxx年xx月xx日"} {$var format="xxxx-xx-xx"}
    还有加密方式: {$var encode="base64" upppercase="true" attribute="password"}用你的模板技术能做到吗?因为你的设计思想里压根就没这一点。你最多规定一种格式,要另一种格式输出你又得改源码,还有各种组合格式,一来一去开发效率就不高,这时候你会觉得Smarty设计得很巧妙和实用。
      

  7.   

    看样子真的是我才疏学浅了,Smarty用的不好,这些好处我没有体会到。
    凡事都有它的适用范围,Smarty有它存在的理由,我想我只是换了一种思路来考虑我项目中的“模板”这个问题,我并没有发明一种我自己的所谓“模板”。更让我困惑的是,为什么非得要php文件使用一种语言(php本身),模板又要换一种语言,Smarty既然可以以那种统一的格式来处理变量,难道PHP语言本身就不可以?
      

  8.   

    又学了不少
    呵呵~~~~~~~~
    关于Smarty的东西
      

  9.   

    这是结构描述语言跟解释型、编译型语言的区别,你不能指望一种语言来统一它们。解释型、编译型语言能完全实现你的逻辑,模板语言能描述你的输出,但是模板语言不是独立存在,是要靠PHP或者其它CGI语言来分析的。一旦模板语言独立存在,就跟XSL描述XML一样,就缺乏了CGI程序的可交互性了。而且模板语言只能定义简单的逻辑,复杂的逻辑控制都是在PHP里做的,这也是为什么提出MVC的概念。你的想法只对于简单的小型网站适用。用统一的格式来处理变量是可以的,比如你把变量都放入$GLOBALS,然后用array_map映射一个函数来统一处理这些变量,但是你总得用个标志来表明哪些变量需要处理,哪些不需要处理吧?你的标志放哪?如果有10个标志,用PHP就要判断10次,描述就不会象{$var charset = "GB2312" prefix = "xxxx-" color="#FF0000" ...}这么简单了,而且美工设计也不用知道PHP怎么写的,只要知道几个属性参数就行了。让PHP识别{$var charset = "GB2312" prefix = "xxxx-" color="#FF0000" ...}这样的语法是很困难的,因为charset,prefix,color这种属性是自定义的,PHP要规定死了就缺乏灵活性了。
      

  10.   

    我接触的很多美工,对他们来说写{$var xxx=xxx}之类的语句和写PHP语句一样:都不懂。既然Smarty可以处理{$var charset = "GB2312" prefix = "xxxx-" color="#FF0000" ...}这样的变量,那么在Smarty内部,是不是也诚如所言 ,做了N次这样的判断呢?我想也应该是吧(我没太深刻的研究过Smarty的,所以不确定)
      

  11.   

    偶觉得还是HTML和PHP分开的好Smarty庞大有它的作用,虽然循环的嵌套等等...对于美工来说比接触php代码好不了多少...但是采用了Smarty以后,网站的改版,程序的变换等等都会很方便的
      

  12.   

    smarty的这种设计思路是很好的.只是要求美工人员也要了解她的这种模板语言.但是我想没有美工愿意学,所以遇到更换样式上的事情又要我们来改.使用了phplib和smarty之后,觉得smarty是为实现mvc而生的.比如一个新闻发布系统,具有import(重要)这样一个属性,逻辑尚只要把这个属性传递给模板变量就可以了,具体怎么显示是view决定,可以是加粗,
    可以是在标题之后加上一个有特殊标记的图片....总之由你决定.这一点是很爽的.好了.罗索了这么多,各位高人有什么感想说出来听听.
      

  13.   

    楼主应该还没很深入去应用模板技术吧?
    用SMARTY来做小型网站简直就是“杀鸡用牛刀”!
    其实最好根据你自己的项目来选择模板引擎,小型的可以使用PHPLIB,中大型的那就选择SMARTTEMPLATE或者SMARTY。
      

  14.   

    SMARTY体积大 功能强 
    我比较喜欢SMARTTEMPLATE的哈,小又快
      

  15.   

    我喜欢用PHPLIB~~~~~~~~~~~~的模板。
      

  16.   

    我觉得你可以尝试用Savant模板,简单,而且直接使用php语法,
    http://phpsavant.com/yawiki/index.php?area=Savant2&page=StartExample见下面的例子:
    Model Logic
    <?php
    require_once 'Savant2.php';
    $tpl =& new Savant2();// Create a title.
    $name = "Some Of My Favorite Books";// Generate an array of book authors and titles.
    $booklist = array(
        array(
            'author' => 'Hernando de Soto',
            'title' => 'The Mystery of Capitalism'
        ),
         array(
            'author' => 'Milton Friedman',
            'title' => 'Free to Choose'
        )
    );// Assign values to the Savant instance.
    $tpl->assign('title', $name);
    $tpl->assign('books', $booklist);// Display a template using the assigned values.
    $tpl->display('books.tpl.php');
    ?>View Logic        <?php if (is_array($this->books)): ?>
                
                <!-- A table of some books. -->
                <table>
                    <tr>
                        <th>Author</th>
                        <th>Title</th>
                    </tr>
                    
                    <?php foreach ($this->books as $key => $val): ?>
                        <tr>
                            <td><?php echo $val['author'] ?></td>
                            <td><?php echo $val['title'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                    
                </table>
                
            <?php else: ?>
                
                <p>There are no books to display.</p>
                
            <?php endif; ?>
    这个开发大型网站是没一点问题的,为什么呢?我已经实践过了
      

  17.   

    PHPLIB smarty smartytemplate都用过,感觉smarty的速度优势并没有多大,
    曾经开启过它的缓存功能,但用在session实时更新时,不得不关闭它,然后用
    它的cache_handler将它存在数据库中,但速度并没有改变多少。
    不知道是不是自己没有用好的缘故,大家觉得呢?