收 藏 投 稿 繁 体 RSS 
站长吧-中国站长学习与交流的网站
首 页 运 营 学 院 建 站 论 坛
Web master8.net  
 网站运营  
  欢迎在本站发布信息,在线投递稿件请点这里。编辑QQ:4908220,欢迎联系交流。
业界动态 创业故事 推广研究 策划盈利 电子商务 企业平台
  站长工具
SEO查询 Whois查询 Pr查询 域名查询 IP查询 网页编辑器
 建站服务  
  如有建站意向,请尽快联系我们,以便安排时间... 建站服务 QQ4908220 QQ:4908220
作品展示 服务范围 服务流程 服务报价 联系方式 付款方式
文章正文  » 您的当前位置: 首页 >> 学院 >> 程序开发 >> PHP
A Sample Template Class For PHP
  来源:本站原创 作者:年华 | 时间:2006-05-23 | 浏览:   相关评论 | 报告错误 | 发布文章
【字号: | | 】 【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色)
/**
* Nianhua.L PHP Template Class
*
* @author 年华 Email/MSN nianhua.liu@gmail.com QQ:4908220
* @link master8.net/blog || master8.net
* @version 1.0
* @copyright 2006 Master8.NET
* @package Nianhua.L PHPTC
*/


class template {
   /**
    * 模板文件存放目录[Template file dir]
    *
    * @var string
    */  
 var $tpl_default_dir;
   /**
    * 模板刷新时间[Template refresh time]
    *
    * @var int
    */  
 var $tpl_refresh_time;

   /**
    * 返回编译后的模板文件[Return compiled file]
    *
    * @return string
    */
 function tpl($file){
   $tplfile=$this->tpl_default_dir."/".$file.".htm";
   $compiledtpldir=$this->tpl_default_dir.".tpl";//构造编译目录[Define compile dir]
   $compiledtplfile=$compiledtpldir."/".$file.".tpl.php";//构造编译文件[Define compile file]
   is_dir($compiledtpldir) or @mkdir($compiledtpldir,0777);
   if(!file_exists($compiledtplfile) || (time()-@filemtime($compiledtplfile) > $this->tpl_refresh_time))//文件不存在或者创建日期超出刷新时间
   {
     $this->tpl_compile($tplfile,$compiledtplfile);//编译模板[Compile template]
   }
   return $compiledtplfile;
 }
   /**
    * 编译模板文件[Compile template]
    *
    * @return boolean
    */
 function tpl_compile($tplfile,$compiledtplfile){
   $str=$this->tpl_read($tplfile);
   $str=$this->tpl_parse($str,$language);
   if($this->tpl_write($compiledtplfile,$str))
   {
     return true;
   }
   return false;      
 }
   /**
    * 解析模板文件[Parse template]
    *
    * @return string
    */
 function tpl_parse($str){
   $str=preg_replace("/([\n\r]+)\t+/s","\\1",$str);
   $str=preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str);
   $str=preg_replace("/\{template\s+(.+)\}/","\n\n",$str);
   $str=preg_replace("/\{include\s+(.+)\}/","\n\n",$str);
   $str=preg_replace("/\{if\s+(.+?)\}/","",$str);
   $str=preg_replace("/\{else\}/","",$str);
   $str=preg_replace("/\{elseif\s+(.+?)\}/","",$str);
   $str=preg_replace("/\{\/if\}/","",$str);
   $str=preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","",$str);
   $str=preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","\n \\3) { ?>",$str);
   $str=preg_replace("/\{\/loop\}/","\n\n",$str);
   $str=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\((.+)\))\}/","",$str);
   $str=preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\((.+)\))\}/","",$str);
   $str=preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","",$str);
   $str=preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/s", "",$str);
   $str=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "",$str);
   $str="\n".$str;//防止直接浏览模板编译文件
   return $str;
 }
   /**
    * 读取模板源文件[Read resource file]
    *
    * @return string
    */
 function tpl_read($tplfile){
   if($fp=@fopen($tplfile,"r") or die('Can not open tpl file'))
   {
     $str=fread($fp,filesize($tplfile));
     fclose($fp);
     return $str;  
   }
   return false;
 }
   /**
    * 写入模板编译文件[Write compiled file]
    *
    * @return boolean
    */
 function tpl_write($compiledtplfile,$str){
   if($fp=@fopen($compiledtplfile,"w") or die('Can not open tpl file'))
   {
     flock($fp, 3);
     if(@fwrite($fp,$str) or die('Can not write tpl file'))
     {
       fclose($fp);
       return true;
     }
     fclose($fp);
   }
   return false;
 }

}
?>


How to use?:http://master8.net/blog/attachment/how to use.txt

Demo:http://master8.net/blog/attachment/tpl.zip


master8
  • 上一篇:通过Select向Input赋值的几种方法
  • 下一篇:Skype出台海外赔偿方案

  • 我要投稿  打印本文  推荐本文  加入收藏  返回顶部  关闭窗口
    搜模板(www.somoban.com) 原创网站模板交易平台
    阿里妈妈再掀疯狂采购风,网站广告位严重告急,急召天下站长
    基于PHP+MySQL的整站、模块、插件开发等或者按需求实现相应功能;
基于各PHP主流建站系统CMS,BBS,BLOG等的模板定制,完全手写代码;
整站数据迁移或备份恢复;网页代码优化、重构;整站常规SEO优化;网站技术支持;
点击了解详情...
    站长论坛
    • 验证码: