PHP Template engine technology simple implementation, PHP template engine _php Tutorial

Source: Internet
Author: User
Tags php template

PHP Template engine technology simple implementation, PHP template engine


After using the SMARTY,TP, but also want to understand how its template technology is implemented, so write a simple template class, basically read the template file-and Replace template file content, save or static

tpl.class.php Main Analysis

Assign method implementation

        /* *          Template Assignment Operation          * @param mixed $TPL _var If it is a string, it is indexed as an array, and if it is an array, the value of the loop is assigned          * @param mixed $tpl _ Value when $tpl_var is a string, the default is null          *         /Public Function assign ($tpl _var, $tpl _value= NULL  {if0) {                foreachas $k = = $v) {                                 $ this->tpl_vars[$k] = $v;                }            } ElseIf ($tpl _var) {                $this->tpl_vars[$tpl _var] = $tpl _value;            }        } 

Fetch method implementation

         /* *           * Generate compiled Files           * @param string $tplFile template path           * @param string $comFile compilation Path           * @return string            *        /private  function Fetch ($tplFile, $comFile)            {//  Determine if the compiled file needs to be rebuilt (whether the compiled file exists or if the template file modification time is greater than the modified time of the compiled file)            if(!file_exists ($comFile) | | filemtime ($ Tplfile) > filemtime ($comFile)) {                // compile, can also be statically used here Ob_start ()                Content = $this-Tplreplace (file_get_contents ($tplFile));                File_put_contents ($comFile, $content);            }        }            

Simple Compilation method: regular substitution according to rules

        /** * Compile file * @param string $content What to compile * @return string*/        Privatefunction Tplreplace ($content) {//escape left and right delimiter regular expression characters$left = Preg_quote ($ This->left_delimiter,'/'); $right= Preg_quote ($ This->right_delimiter,'/'); //Simple simulation of compilation variables$pattern =Array (//For example{$test}                    '/'. $left.'\$ ([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*)'. $right.' /I'                ); $replace=Array (' 
    tpl_vars[\ ' ${1}\ '];?>
 '                ); //Regular Processing            returnpreg_replace ($pattern, $replace, $content); }

display = Fetch+echo

        /** * Output content * @param string $fileName template file name*/         Publicfunction Display ($fileName) {//Template Path$tplFile = $ This->template_dir.'/'. $fileName; //determine if a template exists            if(!file_exists ($tplFile)) {                $ This->errormessage ='template file does not exist'; return false; }            //post-compilation files$comFile = $ This->compile_dir.'/'. MD5 ($fileName).'. PHP'; $ This-fetch ($tplFile, $comFile);
Include $comFile; }

Other properties

        //template file storage location        Private$template _dir ='Templates'; //Compile file storage location        Private$compile _dir ='compiles'; //left delimiter        Private$left _delimiter ='{'; //Right delimiter        Private$right _delimiter ='}'; //internal temporary variables, storing user assignments        Private$tpl _vars =Array (); //error Message        Private$errorMessage =''; /** * Modify the value of the class Property * @param array $configs the related properties and values that need to be modified * @return BOOL*/         Publicfunction Setconfigs (array $configs) {if(Count ($configs) >0){                foreach($configs as$k =$v) {                    if(Isset ($ This-$k)) $ This$k =$v; }                return true; }            return false; }

Test

Template file testtpl.html

"en">    
 
   "UTF-8">    Test_tpl_demo    {$name}:{$age}:{$message}

Run File test_tpl.php

 
  PHP    'Tpl.class.php';         New Tpl ();     = Array (            'name'='waited ') ,             '  Age '=' + '        );    $TPL-Assign ($tplarr);    $tpl->assign ('message','This isa demo' );    $tpl->display ('testtpl.html');? >

Output: Waited:100:this is a demo

Generate compilation file: 972fa4d270e295005c36c1dbc7e6a56c.php

http://www.bkjia.com/PHPjc/1107665.html www.bkjia.com true http://www.bkjia.com/PHPjc/1107665.html techarticle PHP Template engine technology simple implementation, PHP template engine after SMARTY,TP, but also want to know how its template technology is implemented, so write a simple template class, basically read the module ...

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