找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1227|回复: 0
打印 上一主题 下一主题

默默简单的写了一个模板引擎

[复制链接]

2647

主题

2647

帖子

7881

积分

论坛元老

Rank: 8Rank: 8

积分
7881
跳转到指定楼层
楼主
发表于 2018-2-14 09:26:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            引擎文件
[U]复制代码[/U] 代码如下:tp_dir=$tp_dir;  
                        }else{  
                                $this->error("模板路径不存在");  
                        }  
        }  
        /**  
         * 设置缓存目录  
         *  
         * @param unknown_type $cache_dir  
         */  
        function __setcdir($cache_dir){  
                                if(file_exists($cache_dir)){  
                                $this->cache_dir=$cache_dir;  
                        }else{  
                                $this->error("缓存路径不存在");  
                        }  
        }  
    /**  
     * 输出错误信息  
     *  
     * @param unknown_type $msg  
     */  
        function error($msg){  
                print "".$msg."";  
        }  
        /**  
         * 解析模板  
         *  
         * @param unknown_type $file  
         */  
        function tp($file){  
                $tp_path=$this->tp_dir.$file.$this->tp_suffix;  
                $fp=fopen($tp_path,"rb");  
                if(!$this->file_test($tp_path,"r") && !$this->match_hash($file)){  
                        $template=$this->file_read($tp_path);  
                    $var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";  
                    $const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";  
                        $template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);  
                        $template = preg_replace("/\/s", "{\\1}", $template);  
                        $template = preg_replace("/\{lang\s+(.+?)\}/ies", "languagevar('\\1')", $template);  
                        $template = str_replace("{LF}", "", $template);  
                        $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "", $template);  
                        $template = preg_replace("/$var_regexp/es", "\$this->addquote('')", $template);  
                        $template = preg_replace("/\\?\>/es", "\$this->addquote('')", $template);  
                          
                        $template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is", "\ncache_dir."\\1".$this->cache_suffix."'); ?>\n", $template);  
                        $template = preg_replace("/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is", "\ncache_dir."\\1".$$this->cache_suffix."'); ?>\n", $template);  
                        $template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtags('\n\n','')", $template);  
                        $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtags('\n\n','')", $template);  
                        $template = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtags('\n\n','')", $template);  
                        $template = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n\n", $template);  
                        for($i = 0; $i nest; $i++) {  
                        $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies", "\$this->stripvtags('\n','\n\\3\n\n')", $template);  
                        $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies", "\$this->stripvtags('\n \\3) { ?>','\n\\4\n\n')", $template);  
                        $template = preg_replace("/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies", "\$this->stripvtags('\n','\n\\2\n\n')", $template);  
                        }  
                        $template = preg_replace("/\{$const_regexp\}/s", "", $template);  
                        $template = preg_replace("/ \?\>[\n\r]*\file_hash($tp_path);  
                        $head_hash="";  
                        $foot_time="";  
                        $this->file_write($this->cache_dir.$file.".tpl",$head_hash.$template.$foot_time);  
                          
                }  
        }  
         
        /**  
         * 检查文件是否存在并且有读取权限  
         *  
         * @param unknown_type $path  
         */  
        function file_test($path,$method){  
                if(!file_exists($path) || !fopen($path,$method)){  
                        $this->error("模板文件不存在,或没有操作权限");  
                        return false;  
                }  
        }  
        /**  
         * 读取文件内容  
         *  
         * @param unknown_type $path  
         * @return unknown  
         */  
        function file_read($path,$length=0){  
                if(!$this->file_test($path,"r+")){  
                $fp=@fopen($path,"r+");  
                if($length==0){  
                        $contents=@fread($fp,filesize($path));  
                }else{  
                        $contents=@fread($fp,$length);  
                }  
                fclose($fp);  
                return $contents;  
                }  
        }  
         
        /**  
         * 写入文件内容  
         *  
         * @param unknown_type $path  
         * @param unknown_type $puts  
         */  
        function file_write($path,$puts){  
                if(!$this->file_test($path,"w+")){  
                $fp=@fopen($path,"w+");  
                @fwrite($fp,$puts);  
                fclose($fp);  
                }  
        }  
         
        /**  
         * 计算文件的hash  
         *  
         * @param unknown_type $path  
         * @return unknown  
         */  
        function file_hash($path){  
                return md5_file($path);  
        }  
         
        /**  
         * 对比模板文件与缓存文件的hash值  
         *  
         * @param unknown_type $file  
         * @return unknown  
         */  
        function match_hash($file){  
                $read_hash=$this->file_read($this->cache_dir.$file.$this->cache_suffix,46);  
                $html_hash=$this->file_hash($this->tp_dir.$file.$this->tp_suffix);  
                        if(preg_match("/".$html_hash."/i",$read_hash)){  
                        return true;  
                }  
        }  
        function addquote($var) {  
        return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));  
        }  
        function transamp($str) {  
        $str = str_replace('&', '&', $str);  
        $str = str_replace('&', '&', $str);  
        $str = str_replace('\"', '"', $str);  
        return $str;  
}  
        function stripvtags($expr, $statement) {  
        $expr = str_replace("\\\"", "\"", preg_replace("/\/s", "\\1", $expr));  
        $statement = str_replace("\\\"", "\"", $statement);  
        return $expr.$statement;  
}  
}  
$tp=new mmtp();  
$tp->__setdir("./");  
$tp->__setcdir("./cache/");  
$tp->tp("index1");  
$_GET[it]=sdhkadajksdhajdhkajsdhjkasdjkasdhasjdhkjsadhk;  
$name=2;  
$head="欢迎使用MoMo模板引擎";  
include("./cache/index1.tpl");  
?>  
模板index.html
[U]复制代码[/U] 代码如下:{$head}
模板index1.html
[U]复制代码[/U] 代码如下:{template index}  
{if $name==1}  
你好  
{else}  
谢谢  
{/if}  
这个模板是默默今天下午写的,写的比较仓促,也许存在漏洞,这个版本只是测试版,以后我会逐渐的去完善,先发出来,当作一个前瞻.
            
            
        
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户反馈
客户端