In order to separate the business logic of the program from the content presentation page and improve the development speed, PHP introduced the concept of template engine. The most popular PHP template engine can be said to be smarty. Smarty is powerful and powerful. It is fast and recognized by the majority of PHP web developers. This article will record the working execution principle of smarty template engine to deepen our understanding.
In fact, the working principle of all template engines is similar. It is nothing more than using regular matching in the PHP program to replace the tags in the template with PHP code to mix the two into a PHP mixed file, and then execute this mixed file. Compile documents. That's basically what happened. Let’s take smarty as an example to describe this process.
For example, the article page of this website: http://www.phpernote.com/article.php?id=795
The general process is as follows:
Part of the html template page code (article.html):
<body> <div>{subject}</div> <div>{content}</div> </body>
PHP page logic part code:
$subject='smarty视频教程分享'; $content='smarty视频教程分享,下面是具体的下载地址,有需要的朋友可以看看,对smarty模板讲解的非常详细,作者粗略看了一下目录,真是详细到细枝末节该......'; $str=file_get_contents('article.html'); $str=str_replace('{subject}',$subject,$str); $str=str_replace('{content}',$content,$str); echo $str;
The encapsulation code using object-oriented technology to implement the template function is as follows:
<?php class Template{ //属性 public $vars; //保存要替换的标记和数据的内容 public $left_delimiter='{*'; //左分隔符 public $right_delimiter='*}'; //右分隔符 //方法 public function assign($key,$value){ $this->vars[$key]=$value; } public function display($file){//file表示模板名 $str=file_get_contents($file);//从模板中读取多有内容,并将内容放入$str中 foreach ($this->vars as $key => $value){ //$key 键名(模板标记) $value 值 $str=str_replace($this->left_delimiter.$key.$this->right_delimiter, $value, $str); } echo $str; //file_put_contents('bak.html', $str); } }
Note: assign('name','zhangsan'); in this sentence, the data has not been replaced yet, but the incoming data is saved in vars[], and is only performed when displaying Data replacement.
smarty processing process:
1. Smarty first compiles the php source file into an intermediate file
2. If caching is enabled, the cache file will be generated based on the compiled file
3. Each subsequent access will access the compiled file
If the cache file is enabled and there is a cache file and the cache file has not expired, access the cache file directly (ignoring the caching process first) In the compiled file The timestamp records the modification time of the template file. If the template has been modified, it can be detected and recompiled.
(Compilation is to save static content, and dynamic content varies according to the parameters passed in)
Reading compiled files saves the time of reading template files and string replacement, so it can be faster.
Compile when article.php is requested for the first time, and a compiled file is generated, which is in the compiled file.
When requesting article.php for the second time, determine whether the template file has changed. If the template file has changed, then read the template file and then compile it. If it has not changed, read the compiled file. The final output of the compiled file;
Caching is turned off by default; caching is to store data completely in the cache file, and it will not be cached again until the cache file expires; therefore, smarty is not particularly suitable for some websites that are particularly real-time;
The above text can be understood abstractly as the picture below. Readers can experience it for themselves!
Consider caching:
In the smarty program, to determine whether the cache file is enabled and the cache file has not expired, go to the cache file. If the cache file is not enabled, go to the template file. If The cache file has expired, and the template file is also judged.