Write a very simple and cottage-Smarty template engine

Source: Internet
Author: User
Tags php regular expression

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

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.