PHP Regular expression ended today, thinking back a few years ago when the first contact, feel this thing really play not turn ah, and today, feel that there is no difficult to understand, indeed there is a lot of progress, especially for the Smarty template engine has a clearer understanding. Regular expression learning to the end, will always throw the topic of writing a cottage Smarty template engine to practice practiced hand, today under the guidance of the master, wrote such a cottage smarty, as the review of a regular period bar.
<?php class template{//Storage template engine source file directory private $templateDir;//The compiled file directory private $compileDir;// The left border of the border symbol private $leftTag = "{#";//the boundary symbol right boundary private $rightTag = "#}";//the template file name private is currently being compiled $currentTemp = ';//The HTML code in the current source file private $outputHtml;//variable Pool private $varPool =array ();//constructor Incoming template file directory Compile file directory public function __construct ($templateDir, $compileDir, $leftTag =null, $rightTag =null) {$ This->templatedir= $templateDir; $this->compiledir= $compileDir; if (!empty ($LEFTTAG)) $this->lefttag=$ Lefttag;if (!empty ($RIGHTTAG)) $this->righttag= $rightTag;} Write data to the variable pool public function assign ($tag, $var) {$this->varpool[$tag]= $var;} Method of extracting data from a pool of variables Public function getvar ($tag) {return $this->varpool[$tag];} Get the source file content Public function getsourcetemplate ($templateName, $ext = '. html ') {$this->currenttemp=$ templatename;//get the full path $sourcefilename= $this->templatedir. $templateName. $ext;//Get the HTML code from the source file $this-> Outputhtml=file_get_coNtents ($sourceFilename);} Create the compiled file Public function compiletemplate ($templateName =null, $ext = '. html ') {$templateName =empty ($ templatename)? $this->currenttemp: $templateName;//start regular match $pattern= '/'. Preg_quote ($this->lefttag); $pattern. = ' *\$ ([a-za-z]\w*) * '; $pattern. =preg_quote ($this->righttag). ' /'; $this->outputhtml=preg_replace ($pattern, ' <?php echo $this->getvar (\ ' $1\ ') ? > ', $this->outputhtml);//compile file full path $compilefilename= $this->compiledir.md5 ($templateName). $ext; file _put_contents ($compileFilename, $this->outputhtml);} Template Output Public function display ($templateName =null, $ext = '. html ') {$templateName =empty ($templateName)? $ This->currenttemp: $templateName;include_once $this->compiledir.md5 ($templateName). $ext;}} $baseDir =str_replace (' \ \ ', '/', dirname (__file__)); $temp =new template ($baseDir. ' /source/', $baseDir. ' /compiled/'); $temp->assign (' title ', ' Small ant learning php '); $temp->assign (' name ', ' Little Ant '); $teMp->getsourcetemplate (' index '); $temp->compiletemplate (); $temp->display (); ?>
Class Library is very simple, mainly to understand how the template engine works, by the way in the understanding of OOP programming ideas.
Preg_match_all () can not only get the total mode, but also can match the sub-pattern. The 0 key matches the result for the total pattern. 1~n is a sub-mode.
Preg_replace () and \\1 are the same.
<! DOCTYPE
The regular expression ends. Over.
Write a very simple and cottage-Smarty template engine