由于是自己论坛使用的,所以只是想和大家讨论思路方面的问题. 
水平问题,让大家笑话了 
template.php 
代码: <?php 
/********************************************************************************* 
*                                                                 模板类(Template) 
*    最后修改时间:2004.4.07    本论坛使用    
*    


**********************************************************************************/ 
class Template {    //$this->$template,储存模板数据. 
   var $template = '';    //模板路径. 
   var $tpl_path = '';    //模板前缀(风格名称). 
   var $tpl_prefix = '';     //cache路径(编译后的路径). 
   var $cache_path = ''; 
    
   //css文件路径. 
   var $css_path = '';    //header文件路径. 
   var $header_path = '';    //footer文件路径 
    var $footer_path = '';    /** 
   * 初始化模板路径. 
   */ 
   function Template($root = 'default') 
   { 
      //模板前缀(风格名称). 
      $this->tpl_prefix = $root; 
      //模板文件路径. 
      $this->tpl_path = './templates/' . $root . '/'; 
      //生成的PHP文件存放路径. 
      $this->cache_path = './template_data/' .$this->tpl_prefix . '_'; 
      return true; 
   }    /** 
   * chk_cache,检查"编译"后的模板是否需要更新,判断依据:最后修改时间,"编译"文件是否存在. 
   */ 
   function chk_cache($tpl_index) 
    { 
      $tpl_file = $this->tpl_path . $tpl_index . '.html'; 
      $cache_file = $this->cache_path . $tpl_index . '.php'; 
      //判断是否需要更新. 
      if(!file_exists($cache_file)) 
        { 
         return true; 
      } 
        elseif(filemtime($tpl_file) > filemtime($cache_file)) 
        { 
         return true; 
      } 
   }    /** 
   * 输出模板文件. 
   */ 
   function parse_tpl($tpl_index,$message='') 
    { 
       return $this->cache_path . $tpl_index . '.php'; 
    }    /** 
   * 加载模板文件. 
   */ 
   function load_tpl($tpl_index) 
    { 
      $tpl_file = $this->tpl_path . $tpl_index . '.html'; 
      $fp = fopen($tpl_file, 'r'); 
      $this->template = fread($fp, filesize($tpl_file)); 
      fclose($fp); 
   }    /** 
   * 替换变量,并且"编译"模板. 
   */ 
   function write_cache($tpl_index) 
    { 
  
      $cache_file = $this->cache_path . $tpl_index . '.php';       //变量显示. 
      $this->template = preg_replace("/(\{=)(.+?)(\})/is", "<?=\\2?>", $this->template);       //界面语言替换. 
      $this->template = preg_replace("/\{lang +(.+?)\}/ies", "\$lang['main']['\\1']", $this->template);         $fp = fopen($cache_file, 'w'); 
        flock($fp, 3); 
        fwrite($fp, $this->template); 
        fclose($fp); 
    }    /** 
   * 替换block. 
   */ 
   function assign_block($search,$replace) 
    { 
      $this->template = str_replace($search,$replace,$this->template); 
   } 

?> 
 

解决方案 »

  1.   

    嗯,你这样速度可能会很快。但用
         $tpl->assign_block("{block_cat}","<?foreach(\$cats as \$cat) {?>"); 
    总有点不大习惯。str_replace改成preg_replace会快n多。
      

  2.   

    ccterran:
    str_replace改成preg_replace会快n多。
    ------------------------------------------
    应该是str_replace快吧
      

  3.   

    preg_replace需要进行正则判断,效率应该比str_replace差点吧
      

  4.   

    我一直用 str_replace效率还是很不错