Transferred from: http://www.cnblogs.com/RightDear/archive/2012/11/06/2756218.html
Smarty (template engine, template technology)
The use of Smarty is mainly to realize the separation of logic and external content;
Characteristics:
1, fast (because the second execution of the use of the first execution of the generated compilation file)
2, caching technology (it is because of caching technology, so that smarty template technology is not suitable for those who need more real-time update requirements, such as stock information)
3. Plug-in technology
4. Caching Technology
5, Statement free static page technology is actually a space-time technology:
Http://localhost/news.php?id=8 such as this is a news page of a portal site,
Many people visit, every time to go to the database query, you can first visit the time to generate an HTML page, the future visitors are to visit this static page.
Database---> Static pages
Analysis Template technology: The process of using PHP language to read template files to complete a string replacement
The detailed code is as follows:
HTML page: <body><font color = ' Red ' >{id}</font><br><font color = ' green ' >{name}</font ><br><font color = ' bule ' >{age}</font><br></body> background logic section//$id =8; $name = ' Zhangsan '; $age = n; $str = file_get_contents (' demo1.html ');//All contents of the template file $str = Str_replace (' {ID} ', $id, $str);//replace id$str = str_ Replace (' {name} ', $name, $str), $str = Str_replace (' {age} ', $age, $str); Echo $str;? >
Object-oriented technology for encapsulation of template function
1 <?php 2 class Template 3 {4 //attribute 5 public $vars; Save the contents of the tag and data to be replaced 6 public $left _delimiter = ' {* '; Left delimiter 7 public $right _delimiter = ' *} '; Right delimiter 8 //Method 9 public function Assign ($key, $value) Ten {one $this->vars[$key] = $value; 13 }14 public function display ($file) //file represents a template name of { $str = file_get_contents ($file); Read more content from the template and put the content into $str ($this->vars as $key + $value)//$key Key name (template tag) $value value $str = Str_replace ($this->left_delimiter. $key. $this->right_delimiter, $value, $str); }21 echo $ str;22 //file_put_contents (' bak.html ', $str); }24}25?>
Note: The Assign (' name ', ' Zhangsan ') is not actually replaced by the data, but the data is stored in vars[], and the data is replaced when display
The process of Smarty:
1, smarty PHP source files, first compiled into intermediate files
2. If caching is enabled, then the cache file is generated according to the compiled file
3, after each visit will access to compile files
If the cache file is enabled and the cache file is cached and the cache file is not expired, the cache file is accessed directly
(The process of caching is not considered first)
Compile file time stamp record template file modification time, if the template has been modified can be detected, and then recompile
(compilation is to save static content, dynamic content varies according to the parameters passed in)
Reading the compiled file eliminates the time to read the template file, and the string substitution, so it can be faster
The first time the request demo.php compile, generate the compilation file, in the compilation file
When the second request demo.php, determine whether the template file changes, if the template file has changed, then to read the template file, and then compile, if not changed, then read the compiled file, compile the final output of the file;
The cache is closed by default, and the cache is completely cached in the cache file until the cache file expires, so Smarty is not particularly suitable in some websites where real-time is particularly strong;
Consider caching:
In the Smarty program, determine whether to open the cache file, and the cache file is not expired, to find the cache file
If you do not open the cache file, you can judge the template file, if the cache file has expired, but also to judge the template file.
Smarty (Principle Overview)