PowerPoint templates silently write a template engine

Source: Internet
Author: User
Engine files

Copy the Code code as follows:

/**
* The OOP class template engine developed silently based on the Discuz template engine can support template caching and generate hash MD5 values. By the hash value to determine whether the template is modified, if modified to regenerate the cache file, if not modified, directly call the cache file.
* Version: 1.0.0.1 beta test version
*/
Class mmtp{
var $left _tags= "{";
var $right _tags= "}";
var $tp _suffix= ". html";
var $cache _suffix= ". TPL";
var $tp _dir= "./";
var $cache _dir= "./";
/**
* The number of times a nested loop is allowed, default is 5
*
* @var Unknown_type
*/
var $nest = 5;
/**
* Template path
*
* @param unknown_type $TP _dir
* @return MMTP
*/
function __setdir ($tp _dir) {
if (file_exists ($TP _dir)) {
$this->tp_dir= $TP _dir;
}else{
$this->error ("template path does not exist");
}
}
/**
* Set Cache Directory
*
* @param unknown_type $cache _dir
*/
function __setcdir ($cache _dir) {
if (file_exists ($cache _dir)) {
$this->cache_dir= $cache _dir;
}else{
$this->error ("Cache path does not exist");
}
}
/**
* Output error message
*
* @param unknown_type $msg
*/
Function error ($MSG) {
Print "". $msg. "";
}
/**
* Parsing templates
*
* @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 ("/\<\?\=\<\?\= $var _regexp\?\>\?\>/es", "\ $this->addquote (' '), $template);

$template = Preg_replace ("/[\n\r\t]*\{template\s+ ([a-z0-9_]+) \}[\n\r\t]*/is", "\ n Cache_dir. " \\1 ". $this->cache_suffix. ');? >\n ", $template);
$template = Preg_replace ("/[\n\r\t]*\{template\s+ (. +?) \}[\n\r\t]*/is "," \ n Cache_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 < $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 ', ' \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]*\<\? /S "," ", $template);
$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 permission
*
* @param unknown_type $path
*/
function File_test ($path, $method) {
if (!file_exists ($path) | |!fopen ($PATH, $method)) {
$this->error ("template file does not exist, or does not have permission to operate");
return false;
}
}
/**
* Read File contents
*
* @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;
}
}

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

/**
* Calculate the hash of the file
*
* @param unknown_type $path
* @return Unknown
*/
function File_hash ($path) {
Return Md5_file ($path);
}

/**
* Compare the hash value of the template file with the cached file
*
* @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 = "Welcome to the Momo template engine";
Include ("./cache/index1.tpl");
?>


Template index.html

Copy the Code code as follows:

{$head}


Template index1.html

Copy the Code code as follows:

{Template Index}
{if $name ==1}
How are you doing
{Else}
Thank you
{/if}


This template is silently written this afternoon, write a relatively hasty, perhaps there is a loophole, this version is only beta, and I will gradually improve, first sent out, as a forward-looking.

The above describes the PowerPoint template silently wrote a template engine, including the content of the PowerPoint template, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.