How the smarty template is executed. To enable separation of the business logic and content presentation page of the program, php introduces the template engine concept, the most popular php template engine is smarty, which introduces the template engine concept in php to improve the development speed by separating the program's business logic and content presentation pages, the most popular php template engine is smarty, which is recognized by the majority of php web developers for its powerful functionality and fast speed. This article will record how the smarty template engine works.
In fact, the working principle of all template engines is similar, in the php program, replace the tags in the template with the php code using regular expression matching to combine the two into a php mixed file and then execute the mixed file. This is basically the case. The following uses smarty as an example to describe this process.
For example, this site article page: http://www.phpernote.com/article.php? Id = 795
The general process is as follows:
HTML template page watermark code (article.html ):
{subject}
{content}
Php page logic code:
Comment '); $ str = str_replace (' {subject} ', $ subject, $ str); $ str = str_replace (' {content} ', $ content, $ str ); echo $ str;
The encapsulation code for implementing the template function using object-oriented technology is as follows:
Vars [$ key] = $ value;} public function display ($ file) {// file indicates the template name $ str = file_get_contents ($ file ); // Read more content from the template and put the content into $ str foreach ($ this-> vars as $ key => $ value) {// $ key name (Template tag) $ value $ str = str_replace ($ this-> left_delimiter. $ key. $ this-> right_delimiter, $ value, $ str);} echo $ str; // file_put_contents('bak.html ', $ str );}}
Note: assign ('name', 'hangsan '); in this sentence, the data has not been replaced, but the imported data is saved in vars, data is replaced only when display is used.
Process of smarty:
1. smarty first compiles the php source file into an intermediate file
2. if cache is enabled, cache files are generated based on the compiled files.
3. compile files will be accessed every time you access the website.
If cached files are enabled and cached files are not expired, you can directly access the cached files (regardless of the cache process). The time stamp in the compiled files records the template file modification time, if the template has been modified, it can be detected and re-compiled.
(Compiling stores static content. dynamic content varies with input parameters)
Reading the compiled file saves the time for reading the template file and replacing the string, so it can be faster.
The first request to compile article. php generates a compilation file, which is in the compilation file.
The second request article. in php, determine whether the template file has changed. if the template file has changed, read the template file and compile it. if the template file has not changed, read the compiled file, compile the final output of the file;
By default, the cache is disabled. the cache completely stores data in the cached file and will not be cached again until the cached file expires; therefore, smarty is not particularly suitable for websites with high real-time performance;
The above text can be abstracted and understood as the following figure. you can understand it yourself!
Cache considerations:
In the smarty program, you can find the cached file to determine whether the cached file has been enabled and the cached file has not expired. if the cached file has not been enabled, you can judge the template file, if the cached file has expired, the template file is also determined.
Articles you may be interested in
- The cycle table in the smarty template contains incomplete td
- Extension of the for loop in the smarty Template
- How to generate a random number in the smarty Template
- Method for judging null array in the smarty Template
- How to use constants defined by define in a program in a smarty Template
- Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
- Add the latest tag to the information in the smarty Template
- Smarty template retention variable summary
Ghost introduces the concept of the template engine. the most popular php template engine is smarty...