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

php中文网
发布: 2016-07-29 08:35:57
原创
1425人浏览过

引擎文件 

复制代码 代码如下:

/** 
* 默默基于Discuz的模板引擎开发的OOP类模板引擎,可支持模板缓存并生成hash的md5值。由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."
"; 
        } 
        /** 
         * 解析模板 
         * 
         * @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_-][a-zA-Z0-9_-]*)([[a-zA-Z0-9_-."'[]$-]+])*)"; 
                    $const_regexp = "([a-zA-Z_-][a-zA-Z0-9_-]*)"; 
                        $template = preg_replace("/([ ]+) +/s", "\1", $template); 
                        $template = preg_replace("//s", "{\1}", $template); 
                        $template = preg_replace("/{langs+(.+?)}/ies", "languagevar('\1')", $template); 
                        $template = str_replace("{LF}", "="\n"?>", $template); 
                        $template = preg_replace("/{(\$[a-zA-Z0-9_[]'"$.-]+)}/s", "=\1?>", $template); 
                        $template = preg_replace("/$var_regexp/es", "$this->addquote('=\1?>')", $template); 
                        $template = preg_replace("/\?>/es", "$this->addquote('=\1?>')", $template); 
                         
                        $template = preg_replace("/[ ]*{templates+([a-z0-9_]+)}[ ]*/is", "  include('".$this->cache_dir."\1".$this->cache_suffix."'); ?> ", $template); 
                        $template = preg_replace("/[ ]*{templates+(.+?)}[ ]*/is", "  include('".$this->cache_dir."\1".$$this->cache_suffix."'); ?> ", $template); 
                        $template = preg_replace("/[ ]*{evals+(.+?)}[ ]*/ies", "$this->stripvtags('  \1 ?> ','')", $template); 
                        $template = preg_replace("/[ ]*{echos+(.+?)}[ ]*/ies", "$this->stripvtags('  echo \1; ?> ','')", $template); 
                        $template = preg_replace("/[ ]*{elseifs+(.+?)}[ ]*/ies", "$this->stripvtags('  } elseif(\1) { ?> ','')", $template); 
                        $template = preg_replace("/[ ]*{else}[ ]*/is", "  } else { ?> ", $template); 
                        for($i = 0; $i nest; $i++) { 
                        $template = preg_replace("/[ ]*{loops+(S+)s+(S+)}[ ]*(.+?)[ ]*{/loop}[ ]*/ies", "$this->stripvtags('  if(is_array(\1)) { foreach(\1 as \2) { ?>',' \3  } } ?> ')", $template); 
                        $template = preg_replace("/[ ]*{loops+(S+)s+(S+)s+(S+)}[ ]*(.+?)[ ]*{/loop}[ ]*/ies", "$this->stripvtags('  if(is_array(\1)) { foreach(\1 as \2 => \3) { ?>',' \4  } } ?> ')", $template); 
                        $template = preg_replace("/[ ]*{ifs+(.+?)}[ ]*(.+?)[ ]*{/if}[ ]*/ies", "$this->stripvtags('  if(\1) { ?>',' \2  } ?> ')", $template); 
                        } 
                        $template = preg_replace("/{$const_regexp}/s", "=\1?>", $template); 
                        $template = preg_replace("/ ?>[ ]*                        $hash=$this->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){ 
                        $c
                }else{ 
                        $c
                } 
                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_-.-]+)]/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}

AiPPT模板广场
AiPPT模板广场

AiPPT模板广场-PPT模板-word文档模板-excel表格模板

AiPPT模板广场 147
查看详情 AiPPT模板广场
 
模板index1.html

复制代码 代码如下:

{template index} 
{if $name==1} 
你好 
{else} 
谢谢 
{/if}  


这个模板是默默今天下午写的,写的比较仓促,也许存在漏洞,这个版本只是测试版,以后我会逐渐的去完善,先发出来,当作一个前瞻.

以上就介绍了powerpoint 模板 默默简单的写了一个模板引擎,包括了powerpoint 模板方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号