Principle Definition and Usage Example of PHP Template Engine

  • 2021-12-04 09:46:02
  • OfStack

This paper describes the principle, definition and usage of PHP template engine with examples. Share it for your reference, as follows:

template Stores Template Source Files

Template compilation tool class

Compline.class.php


<?php
/**
*  Template compiler class 
*/
class Compile
{
  private $template;     // With compiled file 
  private $content;      // Text to be replaced 
  private $comfile;      // Compiled file 
  private $left = '{';     // Left delimiter 
  private $right = '}';    // Right delimiter 
  private $value = array();   // Value stack 
  private $php_turn;
  private $T_P = array();
  private $T_R = array();
  public function __construct($template, $compileFile, $config)
  {
   $this->template = $template;
   $this->comfile = $compileFile;
   $this->content = file_get_contents($template);
   if($config['php_turn'] === false)
   {
     $this->T_P[] = "/<\?(=|php|)(.+?)\?>/is";
     $this->T_R[] = "&lt;? \\1\\2? &gt";
   }
   //{$var}
   $this->T_P[] = "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/";
   //{foreach $b} Or {loop $b}
   $this->T_P[] = "/\{(loop|foreach) \\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/i";
   //{[K|V]}
   $this->T_P[] = "/\{([K|V])\}/";
   //{/foreach} Or {\loop} Or {\if}
   $this->T_P[] = "/\{\/(loop|foreach|if)}/i";
   //{if (condition)}
   $this->T_P[] = "/\{if (.* ?)\}/i";
   //{(else if | elseif)}
   $this->T_P[] = "/\{(else if|elseif) (.* ?)\}/i";
   //{else}
   $this->T_P[] = "/\{else\}/i";
   //{#...#  Or  *...# , comments }
   $this->T_P[] = "/\{(\#|\*)(.* ?)(\#|\*)\}/";
   $this->T_R[] = "<?php echo \$this->value['\\1']; ?>";
   $this->T_R[] = "<?php foreach ((array)\$this->value['\\2'] as \$K => \$V) { ?>";
   $this->T_R[] = "<?php echo \$\\1; ?>";
   $this->T_R[] = "<?php } ?>";
   $this->T_R[] = "<?php if (\\1) { ?>";
   $this->T_R[] = "<?php }else if (\\2) { ?>";
   $this->T_R[] = "<?php }else{ ?>";
   $this->T_R[] = "";
  }
  public function compile()
  {
   $this->c_all();
   $this->c_staticFile();
   file_put_contents($this->comfile, $this->content);
  }
  public function c_all()
  {
   $this->content = preg_replace($this->T_P, $this->T_R, $this->content);
  }
  /**
  *  Join pair JavaScript File parsing 
  * @return [type] [description]
  */
  public function c_staticFile()
  {
   $this->content = preg_replace('/\{\!(.* ?)\!\}/', '<script src=\\1'.'?t='.time().'></script>', $this->content);
  }
  public function __set($name, $value)
  {
   $this->$name = $value;
  }
  public function __get($name)
  {
   if(isset($this->$name))
   {
     return $this->$name;
   }
   else
   {
     return null;
   }
  }
}

Template.class.php


<?php
/**
* Template
*/
class Template
{
  private $arrayConfig = array(
   'suffix'   => '.tpl',  // Suffix of template 
   'templateDir' => 'template/', // The folder where the template is located 
   'compileDir'  => 'cache/',  // Directory to store after compilation 
   'cache_html'  => true,   // Do you need to compile to static html Documents 
   'suffix_cache' => '.html',  // Set the suffix of the compiled file 
   'cache_time'  => 7200,   // How long to set automatic updates 
   'php_turn'  => true,   // Set whether support php Native code 
   'debug'     => false,
   );
  public $file;         // Template file name , Without path 
  public $debug = array();     // Debugging information 
  private $value = array();    // Value stack 
  private $compileTool;      // Compiler 
  private $controlData = array();
  static private $instance = null;  // Template class object 
  public function __construct($arrayConfig = array())
  {
   $this->debug['begin'] = microtime(true);
   $this->arrayConfig = array_merge($this->arrayConfig, $arrayConfig);
   $this->getPath();
   if(!is_dir($this->arrayConfig['templateDir']))
   {
     exit("template dir isn't found!");
   }
   if(!is_dir($this->arrayConfig['compileDir']))
   {
     if(strtoupper(substr(PHP_OS,0,3)) === 'WIN')
     {
      mkdir($this->arrayConfig['compileDir']);
     }
     else
     {
      mkdir($this->arrayConfig['compileDir'], 0770, true);
     }
   }
   include('Compile.class.php');
  }
  public function getPath()
  {
   $this->arrayConfig['templateDir'] = strstr(realpath($this->arrayConfig['templateDir']), '\\', '/').'/';
   $this->arrayConfig['compileDir'] = strstr(realpath($this->arrayConfig['compileDir'])), '\\', '/').'/';
  }
  /**
  *  Gets an instance of the template engine 
  */
  public static function getInstance()
  {
   if(is_null(self::$instance))
   {
     self::$instance = new Template();
   }
   return self::$instance;
  }
  /**
  *  Setting engine parameters separately 
  *  Also support 1 Sequential setting of multiple parameters 
  */
  public function setConfig($key, $value = null)
  {
   if(is_array($key))
   {
     $this->arrayConfig = $key + $this->arrayConfig;
   }
   else
   {
     $this->arrayConfig[$key] = $value;
   }
  }
  /**
  *  Gets the current template engine configuration for debugging purposes only 
  */
  public function getConfig($key = null)
  {
   if($key && array_key_exists($key, $this->arrayConfig))
   {
     return $this->arrayConfig[$key];
   }
   else
   {
     return $this->arrayConfig;
   }
  }
  /**
  *  Inject a single variable 
  */
  public function assign($key, $value)
  {
   $this->value[$key] = $value;
  }
  /**
  *  Inject array variables 
  */
  public function assignArray($array)
  {
   if(is_array($array))
   {
     foreach ($array as $k => $v) {
      $this->value[$k] = $v;
     }
   }
  }
  /**
  *  Gets the location of the template 
  * @return [type] [description]
  */
  public function path()
  {
   return $this->arrayConfig['templateDir'].$this->file.$this->arrayConfig['suffix'];
  }
  /**
  *  Determine whether the configuration file requires caching 
  */
  public function needCache()
  {
   return $this->arrayConfig['cache_html'];
  }
  /**
  *  Determine whether caching is needed 
  */
  public function reCache($file)
  {
   $flag = false;
   $cacheFile = $this->arrayConfig['compileDir'].md5($file).$this->arrayConfig['suffix_cache'];
   if($this->arrayConfig['cache_html'] === true)
   {
     // Cache required 
     $timeFlag = (time() - @filemtime($cacheFile)) < $this->arrayConfig['cache_time'] ? true : false;
     if(is_file($cacheFile) && filesize($cacheFile) > 1 && $timeFlag)
     {
      // The cache exists and has not expired 
      $flag = true;
     }
     else
     {
      $flag = false;
     }
   }
   return $flag;
  }
  /**
  *  Presentation template 
  */
  public function show($file)
  {
   $this->file = $file;
   if(!is_file($this->path()))
   {
     exit(' The corresponding template could not be found ');
   }
   $compileFile = $this->arrayConfig['compileDir'].md5($file).'.php';
   $cacheFile = $this->arrayConfig['compileDir'].md5($file).$this->arrayConfig['suffix_cache'];
   if($this->reCache($file) === false)
   {
     // If caching is required 
     $this->debug['cached'] = 'false';
     $this->compileTool = new Compile($this->path(), $compileFile, $this->arrayConfig);
     if($this->needCache())
     {
      ob_start();
     }
     extract($this->value, EXTR_OVERWRITE);
     if(!is_file($compileFile) || fileatime($compileFile) < filemtime($this->path()))
     {
      $this->compileTool->value = $this->value;
      $this->compileTool->compile();
      include $compileFile;
     }
     else
     {
      include $compileFile;
     }
     if($this->needCache())
     {
      $message = ob_get_contents();
      file_put_contents($cacheFile, $message);
     }
   }
   else
   {
     readfile($cacheFile);
     $this->debug['cached'] = 'true';
   }
   $this->debug['spend'] = microtime(true) - $this->debug['begin'];
   $this->debug['count'] = count($this->value);
   $this->debug_info();
  }
  public function debug_info()
  {
   if($this->arrayConfig['debug'] === true)
   {
     echo "<br/>", '-------------------- debug_info--------------', "<br/>";
     echo ' Program run date :', date("Y-m-d h:i:s"), "<br/>";
     echo ' Template parsing takes time :', $this->debug['spend'], ' Seconds ', "<br/>";
     echo ' Number of tags contained in template :', $this->debug['count'], "<br/>";
     echo ' Whether to use static caching :', $this->debug['cached'], "<br/>";
     echo ' Template engine instance parameters :', var_dump($this->getConfig());
   }
  }
  /**
  *  Clearly cached html Documents 
  * @return [type] [description]
  */
  public function clean()
  {
   if($path === null)
   {
     $path = $this->arrayConfig['compileDir'];
     $path = glob($path.'* '.$this->arrayConfig['suffix_cache']);
   }
   else
   {
     $path = $this->arrayConfig['compileDir'].md5($path).$this->arrayConfig['suffix_cache'];
   }
   foreach ((array)$path as $v) {
     unlink($v);
   }
  }
}

test.php


<?php
include 'Template.class.php';
$tpl = new Template(array('debug' => true));
$tpl->assign('data', 'hello world');
$tpl->assign('person', 'htGod');
$tpl->assign('data1', 3);
$arr = array(1,2,3,4,'5',6);
$tpl->assign('b', $arr);
$tpl->show('member');

For more readers interested in PHP, please check the topic of this site: PHP Template Technology Summary, PHP Database Operation Skills Summary Based on pdo, PHP Operation and Operator Usage Summary, PHP Network Programming Skills Summary, PHP Basic Syntax Introduction Tutorial, php Object-Oriented Programming Introduction Tutorial, php String (string) Usage Summary, php+mysql Database Operation Introduction Tutorial and php Common Database Operation Skills Summary

I hope this article is helpful to everyone's PHP programming.


Related articles: