Silently and simply wrote a template engine_PHP tutorial

WBOY
Release: 2016-07-21 15:57:53
Original
863 people have browsed it

Engine file

Copy code The code is as follows:
/**
* An OOP template engine developed by Momo based on Discuz template engine, which can support template caching and generate hash md5 values.由hash值来判断模板是否被修改,假如被修改则重新生成缓存文件,假如没有被修改,则直接调用缓存文件.
* 版本: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="./";

/**
* 允许循环嵌套的次数,默认为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("模板路径不存在"); 
                        } 
        } 

        /** 
         * 设置缓存目录 
         * 
         * @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."
"; 
        } 

        /**                                                                                      */ 

        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("/([nr]+)t+/s", "\1", $template); 
                        $template = preg_replace("//s", "{\1}", $template); 
                        $template = preg_replace("/{langs+(.+?)}/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("/[nrt]*{templates+([a-z0-9_]+)}[nrt]*/is", "ncache_dir."\1".$this->cache_suffix."'); ?>n", $template); 
                        $template = preg_replace("/[nrt]*{templates+(.+?)}[nrt]*/is", "ncache_dir."\1".$$this->cache_suffix."'); ?>n", $template); 
                        $template = preg_replace("/[nrt]*{evals+(.+?)}[nrt]*/ies", "$this->stripvtags('nn','')", $template); 
                        $template = preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/ies", "$this->stripvtags('nn','')", $template); 
                        $template = preg_replace("/[nrt]*{elseifs+(.+?)}[nrt]*/ies", "$this->stripvtags('nn','')", $template); 
                        $template = preg_replace("/[nrt]*{else}[nrt]*/is", "nn", $template); 

                        for($i = 0; $i < $this->nest; $i++) { 
                        $template = preg_replace("/[nrt]*{loops+(S+)s+(S+)}[nr]*(.+?)[nr]*{/loop}[nrt]*/ies", "$this->stripvtags('n','n\3nn')", $template); 
                        $template = preg_replace("/[nrt]*{loops+(S+)s+(S+)s+(S+)}[nrt]*(.+?)[nrt]*{/loop}[nrt]*/ies", "$this->stripvtags('n \3) { ?>','n\4nn')", $template); 
                        $template = preg_replace("/[nrt]*{ifs+(.+?)}[nr]*(.+?)[nr]*{/if}[nrt]*/ies", "$this->stripvtags('n','n\2nn')", $template); 
                        } 

                        $template = preg_replace("/{$const_regexp}/s", "", $template); 
                        $template = preg_replace("/ ?>[nr]*                        $hash=$this->file_hash($tp_path); 
                        $head_hash=""; 
                        $foot_time=""; 
                        $this->file_write($this->cache_dir.$file.".tpl",$head_hash.$template.$foot_time); 

                         

                } 
        } 

         
        /**
* Check if the file exists and has read permissions
* *
* @param unknown_type $path
*/ 

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

        /**                                                                         */ 
        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; 
                } 
        } 

         
        /**
*Write file content
*/ 
        function file_write($path,$puts){ 

                if(!$this->file_test($path,"w+")){ 
                $fp=@fopen($path,"w+"); 
                @fwrite($fp,$puts); 
                fclose($fp); 
                } 

        } 

         
        /**                                                                       */ 
        function file_hash($path){ 
                return md5_file($path); 
        } 

         
        /**                                                                                   */ 
        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 



复制代码
代码如下:

{$head}
 
模板index1.html
复制代码 代码如下:
{template index} 
{if $name==1} 
你好 
{else} 
谢谢  {/if}  

This template was written silently this afternoon. It was written in a hurry and may have loopholes. This version is only a beta version. I will gradually improve it in the future and send it out first as a preview.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317721.htmlTechArticleThe engine file copy code is as follows: ?php /** *OOP template developed silently based on Discuz template engine Engine that supports template caching and generates hash md5 values. Determine the template by hash value...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!