PHP template engine Smarty

Source: Internet
Author: User
Tags php template

PHP template engine,Smarty,Unlike other template engines, such as FastTemplate or patTemplate, Smarty converts the Template into a PHP script, removes the additional code that appears in the parsing link for control and error check every time you use this template.

This feature enables Smarty to perform well on large applications and high-traffic websites. In addition to template compilation, Smarty also has many other outstanding features, which will be discussed in detail later.

First, let's reveal the details of template compilation.

What does "template compilation" mean? What kind of compilation is required for webpages? Isn't compilation required for C ++ and Java? Yes, but the compilation here will be slightly different. Smarty parses your template and converts it to a PHP script instead of a binary code, as it does in general programming. In this way, when your web page is browsed, Smarty directly reads the PHP script file instead of the template itself, thus saving the time for re-parsing.

Smarty is also intelligent in deciding when to compile. It will only re-Compile when you make actual changes to the web page, so you don't need to worry about manual compilation problems. The advantage of this is that you do not need to know any knowledge about PHP scripts and coding. These operations are performed the day after tomorrow, so if your template designer doesn't even need to know That Smarty is a "template compilation engine ".

Smarty is also famous for its automatic cache template output.

Smarty caches the output content of the template, which saves the system expenditure for retrieving data from the data source again. Data sources are usually external and slow, so they are often the bottleneck of your application, just like remote data sources. Smarty caches the output of your template along with the data obtained from the data source, so that you do not connect to the data source every time your webpage is accessed. If you have a slow-response data source server or multiple requests to the data source, this cache function will greatly improve the web page performance.

Of course, sometimes you do not want to cache the template output, for example, the securities quote machine or you constantly modify your data source and expect to immediately reflect these changes on the web page. No problem! Smarty also allows you to define what needs to be cached and what does not need to be. In fact, you can put the cache and the content that is not cached on a page, because Smarty can accurately define what can be cached (such as the permanent navigation bar) and what cannot be cached (such as the Securities machine at the bottom of the web page ). You can also set the cache content validity period so that the template content will only be cached for a period of time. In this way, you can find a balance between a large amount of information updated in real time and a faster website experience.

One thing to note is that this Cache function is completely different from Zend Cache or other PHP accelerators.

Cache tools similar to Zend Cache the bytecode generated after PHP script compilation, while Smarty caches the output of your template (actually a PHP script ). Therefore, Smarty can work with Zend Cache. The benchmark test has found that this is a perfect solution.

As the engine creator said: when used with the PHP accelerator, the performance of Smarty has indeed improved. Smarty also provides the variable modification function. You can uppercase, cut, search, and replace strings. The variable modifier simplifies the programming process and bypasses the professional syntax that programmers often use. In this way, the template designer will have better control over the template variable format, although he must first know which variable modifiers are available. This is a very useful function, because syntaxes become concise and can be used by people who are not programmers.

Smarty also provides many built-in and used functions in your template.

These functions are like the application programming interface of the template. They can be modified but cannot be used by built-in functions. These functions allow 4F60 to implement conditional output (using the if statement), dynamic loop iteration (using the foreach or section statement), and read the config file.

The filter Smarty can classify the filters so that you can run the filters before or after the template is compiled. The pre-filter is used before your template is run. The post-filter is used after the template is compiled. The output filter filters filter the output results of the template. Why filter? You may ask. The pre-filter can remove the content you do not want and ensure that the content you do not want will not be compiled. The post-filter can add additional information after the template is compiled, such as the creation time. The output filter allows you to modify the template output, for example, hiding all email addresses for spam.

The Config file is a configuration file that stores the global variables of the template. In this way, you can store some global variables that affect all templates in one place. A typical example is the color scheme of all templates. If the color scheme needs to be modified, the template designer only needs to change the value in the configuration file. In this way, they do not have to modify every template one by one.

The plug-in Smarty plug-in architecture has emerged since version 2.0, which makes Smarty more user-oriented. The pre-filters and post-filters discussed above are all available plug-in types. Other plug-in types include modifier, control module, compiler, etc. With the plug-in, you can create your own template functions, variable modifiers, and filters. You can also use the resource plug-in to change the format of reading data from Smarty (by default, it is a flattened file ).

With the resource plug-in, you can save your template in the database and retrieve data through certain interfaces. In conclusion, combining the template compilation function of Smarty with the inherent efficiency of PHP gives you an absolute speed advantage. Using a well-designed plug-in architecture, Smarty also has extended functions, including template functions and variable modifiers. The increase in speed and function does not affect the affinity of Smarty. In fact, Smarty is very easy to use. Smarty also provides online shared documents. You can download it from the Smarty Website.

Andrei Zmievski, one of the authors of Smarty, is also a member of the PHP development team. He will try to ensure that Smarty keeps up with PHP development. So you can rest assured that any PHP revision will be supported by Smarty.

We hope that the above content will help you.


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.