復(fù)制代碼 代碼如下:<?php /** * 默默基于Discuz的模板引擎開發(fā)的OOP類模板引擎,可支持模板緩存并生成hash的md5值。由hash值來判斷模板是否被修改,假如被修改則重新生成緩存文件,假如沒有被修改,則直接調(diào)用 " /> 97精品伊人久久大香线蕉app,性xxx在线观看,亚洲视频成人

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

默默簡單的寫了一個(gè)模板引擎

引擎文件 

復(fù)制代碼 代碼如下:<?php 
/** 
* 默默基于Discuz的模板引擎開發(fā)的OOP類模板引擎,可支持模板緩存并生成hash的md5值。由hash值來判斷模板是否被修改,假如被修改則重新生成緩存文件,假如沒有被修改,則直接調(diào)用緩存文件. 
* 版本:1.0.0.1 beta 測試版 
*/ 
class mmtp{ 

        var $left_tags="{"; 
        var $right_tags="}"; 
        var $tp_suffix=".html"; 
        var $cache_suffix=".tpl"; 
        var $tp_dir="./"; 
        var $cache_dir="./"; 

        /** 
         * 允許循環(huán)嵌套的次數(shù),默認(rèn)為5 
         * 
         * @var unknown_type 
         */ 
        var $nest = 5;     
    /** 
     * 模板路徑 
     * 
     * @param unknown_type $tp_dir 
     * @return mmtp 
     */ 

        function __setdir($tp_dir){ 
                        if(file_exists($tp_dir)){ 
                                $this->tp_dir=$tp_dir; 
                        }else{ 
                                $this->error("模板路徑不存在"); 
                        } 
        } 

        /** 
         * 設(shè)置緩存目錄 
         * 
         * @param unknown_type $cache_dir 
         */ 
        function __setcdir($cache_dir){ 
                                if(file_exists($cache_dir)){ 
                                $this->cache_dir=$cache_dir; 
                        }else{ 
                                $this->error("緩存路徑不存在"); 
                        } 
        } 

    /** 
     * 輸出錯(cuò)誤信息 
     * 
     * @param unknown_type $msg 
     */ 

        function error($msg){ 
                print "<div style=/"font-size:12px;color:red;/">".$msg."</div>"; 
        } 

        /** 
         * 解析模板 
         * 
         * @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", "http://1", $template); 
                        $template = preg_replace("http://</!/-/-/{(.+?)/}/-/-/>/s", "{//1}", $template); 
                        $template = preg_replace("http://{lang/s+(.+?)/}/ies", "languagevar('//1')", $template); 
                        $template = str_replace("{LF}", "<?=/"http://n/"?>", $template); 

                        $template = preg_replace("http://{(///$[a-zA-Z0-9_/[/]/'/"/$/./x7f-/xff]+)/}/s", "<?=//1?>", $template); 
                        $template = preg_replace("/$var_regexp/es", "/$this->addquote('<?=//1?>')", $template); 
                        $template = preg_replace("http://</?/=/</?/=$var_regexp/?/>/?/>/es", "/$this->addquote('<?=//1?>')", $template); 

                         
                        $template = preg_replace("/[/n/r/t]*/{template/s+([a-z0-9_]+)/}[/n/r/t]*/is", "/n<? include('".$this->cache_dir."http://1".$this->cache_suffix."'); ?>/n", $template); 
                        $template = preg_replace("/[/n/r/t]*/{template/s+(.+?)/}[/n/r/t]*/is", "/n<? include('".$this->cache_dir."http://1".$$this->cache_suffix."'); ?>/n", $template); 
                        $template = preg_replace("/[/n/r/t]*/{eval/s+(.+?)/}[/n/r/t]*/ies", "/$this->stripvtags('/n<? //1 ?>/n','')", $template); 
                        $template = preg_replace("/[/n/r/t]*/{echo/s+(.+?)/}[/n/r/t]*/ies", "/$this->stripvtags('/n<? echo //1; ?>/n','')", $template); 
                        $template = preg_replace("/[/n/r/t]*/{elseif/s+(.+?)/}[/n/r/t]*/ies", "/$this->stripvtags('/n<? } elseif(//1) { ?>/n','')", $template); 
                        $template = preg_replace("/[/n/r/t]*/{else/}[/n/r/t]*/is", "/n<? } else { ?>/n", $template); 

                        for($i = 0; $i < $this->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<? if(is_array(//1)) { foreach(//1 as //2) { ?>','/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<? if(is_array(//1)) { foreach(//1 as //2 => //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<? if(//1) { ?>','/n//2/n<? } ?>/n')", $template); 
                        } 

                        $template = preg_replace("http://{$const_regexp/}/s", "<?=//1?>", $template); 
                        $template = preg_replace("/ /?/>[/n/r]*/</? /s", " ", $template); 
                        $hash=$this->file_hash($tp_path); 
                        $head_hash="<!-- hash=".$hash." -->"; 
                        $foot_time="<!-- time=".(date("Y-m-d G:i:s"))." -->"; 
                        $this->file_write($this->cache_dir.$file.".tpl",$head_hash.$template.$foot_time); 

                         

                } 
        } 

         
        /** 
         * 檢查文件是否存在并且有讀取權(quán)限 
         * 
         * @param unknown_type $path 
         */ 

        function file_test($path,$method){ 
                if(!file_exists($path) || !fopen($path,$method)){ 
                        $this->error("模板文件不存在,或沒有操作權(quán)限"); 
                        return false; 
                } 
        } 

        /** 
         * 讀取文件內(nèi)容 
         * 
         * @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; 
                } 
        } 

         
        /** 
         * 寫入文件內(nèi)容 
         * 
         * @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); 
                } 

        } 

         
        /** 
         * 計(jì)算文件的hash 
         * 
         * @param unknown_type $path 
         * @return unknown 
         */ 
        function file_hash($path){ 
                return md5_file($path); 
        } 

         
        /** 
         * 對(duì)比模板文件與緩存文件的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("http:///"", "/"", preg_replace("http://[([a-zA-Z0-9_/-/./x7f-/xff]+)/]/s", "['//1']", $var)); 
        } 

        function transamp($str) { 
        $str = str_replace('&', '&', $str); 
        $str = str_replace('&amp;', '&', $str); 
        $str = str_replace('/"', '"', $str); 
        return $str; 


        function stripvtags($expr, $statement) { 
        $expr = str_replace("http:///"", "/"", preg_replace("http://</?/=(///$.+?)/?/>/s", "http://1", $expr)); 
        $statement = str_replace("http:///"", "/"", $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 
復(fù)制代碼 代碼如下:{$head} 
模板index1.html
復(fù)制代碼 代碼如下:{template index} 
{if $name==1} 
你好 
{else} 
謝謝 
{/if}  

這個(gè)模板是默默今天下午寫的,寫的比較倉促,也許存在漏洞,這個(gè)版本只是測試版,以后我會(huì)逐漸的去完善,先發(fā)出來,當(dāng)作一個(gè)前瞻.

php技術(shù)默默簡單的寫了一個(gè)模板引擎,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日韩亚洲欧美中文在线 | 亲嘴扒胸摸屁股视频免费网站 | 2017必看无码作品 | 国产AV国片精品无套内谢无码 | 精品久久久久久久高清 | 寂寞护士中文字幕 mp4 | 亚洲精品久久久久一区二区三 | 亚洲国产在线综合018 | 暖暖 视频 在线 观看 高清 | 久久精品成人免费网站 | 男女边吃奶边做边爱视频 | 动听968 | 中文字幕国产在线观看 | 亚洲精品乱码一区二区三区 | 天天夜夜草草久久亚洲香蕉 | 69国产精品成人无码视频 | 99精品免费久久久久久久久蜜桃 | 夜夜女人国产香蕉久久精品 | 曰本xxⅹ孕妇性xxx | 全黄H全肉禁乱公 | 哺乳溢出羽月希中文字幕 | 日韩免费一级毛片 | 4438成人情人网站 | 精品久久久亚洲精品中文字幕 | 久久婷婷丁香五月色综合啪免费 | 亚洲 综合 欧美在线视频 | 国产国语在线播放视频 | CHINA篮球体育飞机2022网站 | 中文字幕在线免费视频 | 亚洲午夜久久久精品电影院 | 无限资源在线完整高清观看1 | 国产午夜免费不卡精品理论片 | 国产毛片女人18水多 | 99久酒店在线精品2019 | 中文字幕一区中文亚洲 | 国内精品人妻无码久久久影院蜜桃 | 99在线观看精品 | 久久久无码精品一区二区三区 | 91se在线看片国产免费观看 | 2021国产精品久久久久精品免费网 | 青青草原在线免费 |