Home Backend Development PHP Tutorial powerpoint template silently and simply wrote a template engine

powerpoint template silently and simply wrote a template engine

Jul 29, 2016 am 08:35 AM

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); 
                         
                } 
        }
                                                                                                                                                                                                $this->error ("The template file does not exist, or there is no operation permission");
                                                                                                                                                                        function file_read($path,$length=0){
                                                                                 ->file_test($path,"r+")){
                                                                                                           
                                                                                                                                                                                                                                                                                                    }                                                                                                              /**
                                                                                                                                           */
                                                                                                                                                                                                                                                                           path,"w+")){
                                                                                                                                                                                                                                     
         
        /**
             * Calculate the hash of the file                                                                                                          */ 
        function file_hash($path){ 
                return md5_file($path); 
        } 
         
        /**
             * Compare the hash values ​​of template files and cached files                                                                            */ 
        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}  

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


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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles