PHP Template engine Technology simple implementation _php instance

Source: Internet
Author: User
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 will be indexed as an array, and if it is an array, the value of the loop will be assigned     * @param mixed $tpl _value when $tpl_ var is a string value, default is NULL */public    function assign ($tpl _var, $tpl _value=null) {      if (Is_array ($tpl _var) && count ($tpl _var) > 0) {        foreach ($tpl _var as $k + = $v) {          $this->tpl_vars[$k] = $v;        }      } ElseIf ($tpl _var) {        $this->tpl_vars[$tpl _var] = $tpl _value;      }    }

Fetch method implementation

     /**      * Generate compiled file      * @param string $tplFile template path      * @param string $comFile compilation Path      * @return String */    Priv Ate 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 *    /Private Function Tplreplace ($content {      //escape left and right delimiter regular expression character      $left = Preg_quote ($this->left_delimiter, '/');      $right = Preg_quote ($this->right_delimiter, '/');      Simple simulation of the compiler variable      $pattern = Array (          //For example {$test}          '/'. $left. ' \$ ([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) '. $right. ' /I '        );      $replace = Array (          ' <?php echo $this->tpl_vars[\ ' ${1}\ ';?> '        );      Regular processing      return preg_replace ($pattern, $replace, $content);    }

display = Fetch+echo

/**     * Output Content     * @param string $fileName template file name *    /Public Function display ($fileName) {      //template path      $ Tplfile = $this->template_dir. '/'. $fileName;      Determine if the template exists      if (!file_exists ($tplFile)) {        $this->errormessage = ' template file does not exist ';        return false;      }      The compiled file      $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 variable, store user assignment    private $tpl _vars = Array ();    Error message    private $errorMessage = ';    /**     * Modify the value of the Class property     * @param array $configs related properties and values that need to be modified     * @return BOOL */public    function 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

  
 
    Test_tpl_demo  {$name}:{$age}:{$message} run the file test_tpl.php<?php  require ' Tpl.class.php ';    $TPL = new TPL ();  $tplarr = Array (      ' name ' = ' waited ', ' age ' = ' + '    );  $tpl->assign ($tplarr);  $TPL->assign (' message ', ' This is a demo ');  $tpl->display (' testtpl.html ');? >

Output: Waited:100:this is a demo

Generate compilation file: 972fa4d270e295005c36c1dbc7e6a56c.php

The above is the whole content of this article, I hope that everyone's study has helped.

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