php template function regular implementation code

  • 2020-05-26 08:02:18
  • OfStack

I've seen the source code for phpcms and discuz, so maybe there's a lack of innovation, but the principles are pretty much the same, but the details are a little different.
To get to the point, let's talk about the implementation process.
1. The first step is to decide where to put the template files. Where are the converted php files? What else do you call it? Direct access to the source code:
 
function template($tpl = 'index',$dir = 'hello') 
{ 
if(!file_exists($pd = TPL_PATH.$dir.'/'))@mkdir($pd,0777) or die("$pd Directory creation failed ");// Such as cache/tpl/hello/ 
if(!file_exists($td = TPL.$dir.'/'))@mkdir($td,0777) or die("$td Directory creation failed ");// Such as data/tpl/hello/ 

$t2p = $pd.$tpl.'.php';// Template file formed after regular conversion php file , Such as cache/tpl/hello/index.php 
$t2h = $td.$tpl.'.html';//html Template files, such as data/tpl/hello/index.html 

2. When do you need a regular conversion? It can be the php file after the regular does not exist, or the html file before the regular changes. The filemtime(string $path) function is used here, which returns the last time the file was modified.
 
if(!file_exists($t2p) || @filemtime($t2p) < @filemtime($t2h) )// The template file changes after the regular php File updated accordingly  
{ 
template_go($t2p,$t2h);// Template conversion start  
} 
return $t2p;// Returns after the regex php File, can be called like this: such as include template('header','hello'); 
} 

3. To start the template conversion, read it from the html file, replace it regularly, and then write it to the php file.
 
function template_go($t2p,$t2h) 
{ 
$str = @file_get_contents($t2h);// read  
if($str === false) exit(" Template file missing, please check! "); 
$str = template_do($str);// Regular replacement  
@chmod($t2p,0777); 
return $str = file_put_contents($t2p, $str);// write  
} 

4. Regular rules, a few brief regular substitution grammars.
 
function template_do($str) 
{ 
$str = preg_replace('/([\n\r+])\t+/s', '\\1', $str);// To get rid of TAB Tabs. modifier /s It's not ignoring the newline  
$str = preg_replace('/\{\$(.*)\}/Us', '<?php echo $\\1; ?>', $str);/*{$xx} Switch to <?php echo $xx;?>  Note that the modifier must be added /U, Can match 1 Times. Laziness can also be matched */ 
$str = preg_replace('/\{php (.+)\}/', '<?php \\1 ?>', $str);/*{php xxxx} Switch to <?php xxxx ?>  Note that you cannot add a modifier /s , consider wrapping the regular multiple times */ 
$str = preg_replace('/\{template(.*)\}/Us', '<?php include template\\1; ?>', $str); 
/*{template(xx,yy)} Switch to <?php include template(xx,yy); ?> */ 
$str = preg_replace('/\{include (.*)\}/Us', '<?php include "\\1"; ?>', $str);/*{include xx.php} Switch to <?php include xx.php ?> */ 
$str = "<?php defined('IN_PH') or die('Access Denied');?>".$str; 
//$str = preg_replace('/\s+/', ' ', $str);// Look at the web source code  
return $str; 
} 

Of course, this function is still relatively crude, but I hope to improve it.
ps: this is my first time to write a blog. I originally thought that I would write a technical blog if I had time to talk about my experience. When I learned from the experience, I also learned from the big guys.
Still have is, the blog is better to save, convenient save trouble, ha ha.

Related articles: