Paste the directory structure first:
lscompiled/ index.php source/ ss.class.PHPcompiled 模版编辑后的目录index.phpsource 模版源文件夹ss.clsss.php 山寨类smarty文件夹
ss.clsss.php
Templatedir= $templateDir; $this->compiledir= $compileDir; if (!empty ($LEFTTAG)) $this->lefttag= $leftTag; if (!empty ($RIGHTTAG)) $this->righttag= $rightTag; if (!empty ($templateExtName)) $this->templateextname= $templateExtName; }//Put the used variable into the variable pool public function assign ($tag, $var) {$this->varpool[$tag]= $var; }//Remove a variable for the following str_replace to replace the public function GetVar ($tag) {return $this->varpool[$tag]; }//Get template source file Public function getsourcetemplate ($templateName) {$this->currenttemp= $templa Tename; Stitching full file name $sourceFileName = $this->templatedir. $this->currenttemp. $this->templateextname; $this->outputhtml=file_get_contents ($sourceFileName); }//Compile Public Function compiletemplate ($templateName =null) {//Get the template that is currently required to compile $templateN Ame=empty ($templateName)? $this->currenttemp: $templateName; $pattern = '/\{# (\$[a-za-z_]\w+) #\}/';//php variable naming//preg_quote can appear in the user-defined left and right delimiters. \ + * ? [ ^ ] $ ( ) { } = ! < > | :-to escape $pattern = "/". Preg_quote ($this->lefttag); The front and back plus the above match any matches like{# $name #}
$pattern. = ' *\$ ([a-za-z_]\w*) * '; $pattern. =preg_quote ($this->righttag). " /"; $this->outputhtml=preg_replace ($pattern, '
GetVar (\ ' $1\ ');? > ', $this->outputhtml);//\\1 can//note not to use double quotes, parsing GetVar Template:: $getVar notice Error $this-& Gt;outputhtml=preg_replace ($pattern, "
GetVar (\\1);? > ", $this->outputhtml); $compileFileName = $this->compiledir.md5 ($templateName). $this->templateextname; File_put_contents ($compileFileName, $this->outputhtml); }//Output public function display ($templateName =null) {$templateName =empty ($templateName)? $this-> ; Currenttemp: $templateName; Include ($this->compiledir.md5 ($templateName). $this->templateextname); } }
The simple call is as follows: ( index.php
middle content)
assign('name','iamtb'); $test->assign('pageTitle','tbtbt'); $test->getSourceTemplate('index'); $test->compileTemplate('index'); $test->display('index');
The above describes the code to explain similar to the Smart template engine principles, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.