Code interpretation similar to Smart template engine principle

Source: Internet
Author: User
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-&GT;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-&gt ; 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.

  • 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.