Home >Backend Development >PHP Tutorial >A simple template class (PHP)_PHP tutorial

A simple template class (PHP)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:48927browse

With a data manipulation class, the project can only simply manipulate data, but to be able to display beautiful pages together with the artist, a better template engine is needed. Compared with a relatively large template engine like SMARTY, I think the following one is really much smaller.

I saw this template class on the Internet before, and it was well written, so I quoted it. I don’t know who the author is yet, so I will talk about the principle of this class first.

First of all, this class has only a simple regular parser. But basically it can be used. If it can be expanded on this basis, I believe this little thing will have great development. Comrades with the same hobbies are invited to join in strengthening it. I'll drop the bricks right now.

template.class.php

[html]
class template{
       
//Array stored in variables
Private $vars = array();
//Template directory
Public $template_dir = './template/';
//Cache directory
Public $cache_dir = './cache/';
//Compile directory
Public $template_c_dir = './template_c/';
//Template file
Public $template_file = '';
//Left connector
Public $left_delimiter = '<{';
//Right connector
Public $right_delimiter = '}>';
//Compile file
Private $template_c_file = '';
//Cache files
Private $cache_file = '';
//Cache time
Public $cache_time = 0;
       
//Built-in parser
Private $preg_temp = array(
          '~<{($[a-z0-9_]+)}>~i'
=> '', // <{$name}>
                                                                  '~<{($[a-z0-9_]+).([a-z0-9_]+)}>~i'
=> '', // <{$arr.key}>
                                                                 '~<{($[a-z0-9_]+).([a-z0-9_]+).([a-z0-9_]+)}>~i'
=> '', // <{$arr.key.key2}>
                                                                  '~~i'
=> '_include($2); ?>', // <?php include('inc/top.php'); ?>
                                                        '~<{:(.+?)}>~' => '', // <{:strip_tags($a)}>
                                                        '~<{~(.+?)}>~' => '', // <{~var_dump($a)}>
                                                        '~ ' );
       
/**
*Constructor
​​*/
Public function __construct(){
If(defined('TMP_PATH')){
                 $this->template_c_dir = TMP_PATH . 'template_c/';
$ This-& gt; cache_dir = tmp_path. 'Cache/';
         } 
}  
       
/**
*Variable assignment
*@param $key mixed key name
*@param $value mixed value
​​*/
Public function assign($key, $value = ''){
If(is_array($key)){
$this->vars=array_merge($key,$this->vars);
         } 
        else{
$this->vars[$key]=$value;
         } 
}  
       
/**
*Show page
*@param $file string template file name
​​*/
Public function display($file){
echo $this->fetch($file);
}  
       
/**
*Return to cache content
*@param $file string template file name
*@return $content string cache content
​​*/
Public function fetch($file){
           $this->template_file = $file;
          $desc_template_file = $this->template_dir .$file;
          $desc_content = $this->readfile($desc_template_file);
                                   
          $template_c_file_time= filemtime($desc_template_file);
//If the cache time is exceeded, compile
If($this->cache_time < time()-$template_c_file_time){
$this->complie($this->token($desc_content));
         } 
//Get the contents of the cache area below
         ob_start();
                                   
@extract($this->vars , EXTR_OVERWRITE);
include ($this->template_c_dir . $this->template_c_file);
                                   
         $content = ob_get_contents();
         ob_end_clean();
                                   
//$this->store_buff($content);
         return $content;
}  
       
/*
*Replace the delimiter, and replace the content of the parser
*@param $content string read content
*@return $token_content string replaced content
*/
Public function token($content){
         $token_content = $content;
If($left_delimiter != '<{'){
               $token_content = str_replace($left_delimiter, '<{', $token_content);
                $token_content = str_replace($right_delimiter, '}>' , $token_content);
         } 
          $token_content = preg_replace(array_keys($this->preg_temp), $this->preg_temp, $token_content);
         return $token_content;
}  
       
/*
*Generate storage
*@param $content string read content
* *
         
Public function store_buff($content){
$this->cache_file = md5($this->template_file) . $this->template_file . '.html';
$tempfile = $this->cache_dir . $this->cache_file;
           $fp = fopen($tempfile, 'w');
          fputs($fp,$content);
          fclose($fp);
          unset($fp);
}  
*/
       
/*
*Compile and save
*@param $content string read content
* *
*/
Public function complie($content){
$this->template_c_file = md5($this->template_file) . $this->template_file . '.php';
$tempfile = $this->template_c_dir . $this->template_c_file;
           $fp = fopen($tempfile, 'w');
          fputs($fp,$content);
          fclose($fp);
          unset($fp);
}  
       
/*
*Read the contents of the file
*@param $file string file name
*@return $content string file content
*/
Public function readfile($file){
If(file_exists($file)){
                 $fp = fopen($file, 'r');
               $content ='';
             while(!feof($fp)){
                       $content .= fgets($fp,4096);
                                                                                                                                                       fclose($fp);
               unset($fp);
                    return $content;                                        } 
        else{
exit($file . ' not exist!');
         } 
}  
       
/*
*Template nesting
*@param $file string file name
*@return string The absolute address of the file
*/
Public function _include($file){
If(file_exists($this->template_dir . $file)){
                return ($this->template_dir . $file);
         } 
        else{
echo "Template file does not exist";
exit;
         } 
}   www.2cto.com
}
?>

With this template, you can forget about those large templates. After all, your artist will not write so many SMARTY marks in his slice file, and as long as he cuts it, you can use it directly without modifying the images. , css, js address.

Author: tomyjohn


http://www.bkjia.com/PHPjc/478157.html

truehttp: //www.bkjia.com/PHPjc/478157.htmlTechArticleWith a data operation class, the project can only simply operate the data, but it must be able to be displayed together with the artist A beautiful page requires a better template engine. With SMA...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn