[Smarty] How the smarty template is executed

Source: Internet
Author: User
Tags php template smarty template
In order to separate the business logic of the program from the content presentation page and improve the development speed, php introduces the template engine concept. The most popular php template engine is smarty, smarty is recognized by PHP Web developers for its powerful functions and fast speed. This article will record how the smarty template engine works.

In order to separate the business logic of the program from the content presentation page and improve the development speed, php introduces the template engine concept. The most popular php template engine is smarty, smarty is recognized by php web developers for its powerful functions and fast speed. This article will record how the smarty template engine works.

In order to separate the business logic of the program from the content presentation page and improve the development speed, php introduces the template engine concept. The most popular php template engine is smarty, smarty is recognized by php web developers for its powerful functions 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.

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.

(Do not consider the cache process first)

The time stamp in the compilation file records the modification time of the template file. If the template has been modified, it can be detected and re-compiled.

(Compiling stores static content. dynamic content varies with input parameters)

Reading compiled files saves the time to read template files and replace strings, so it can be faster.

The first request for article. php is compiled to generate a 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 cache file is not enabled, determine the template file. If the cached file has expired, determine the template file.

Original article address: [smarty] implementation principle of the smarty template. Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.