Detailed Introduction to PHP template engine smarty_php Tutorial

Source: Internet
Author: User
Tags php template smarty template
PHP template engine, Smarty,Unlike other template engines, such as Fasttemplate or pattemplate, the main reason is that Smarty will convert the template into a PHP script and eliminate the additional code that will appear in the parsing process for control and error checking each time the template is used.

This feature enables Smarty to excel in large-scale applications and high-traffic websites. Smarty In addition to template compilation There are many other outstanding features, we will discuss in detail later.

First, let's uncover the template compilation.

What does "compilation of templates" mean? What kind of compilation does a webpage need? is compiling not C + + and Java to do? Yes, but the compilation here will be slightly different. Smarty parses your template and then translates it into a PHP script, not a binary code, as it does in general programming. Thus, when your page is browsed, Smarty directly reads the PHP script file instead of the template itself, thus saving time for parsing again.

Smarty is smart when deciding when to compile, and he only compiles when you have actual changes to the page, so you don't need to worry about manual compilation. The good thing about this is that you don't need to know anything about PHP scripts and coding. These are all operating in the day after tomorrow, so if your template designer doesn't even need to know that Smarty is a "template-compiling engine".

Smarty is also known for its auto-cached template output.

Smarty caches the output of the template, which saves the expense of the system when retrieving data from the data source. Data sources are often external and slow, so it's often a bottleneck for your application, just like a remote data source. 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 page is accessed. If you have a slow-reacting data source server or have multiple requests for data sources, this caching feature can greatly improve the performance of your Web pages.

Of course, sometimes you don't want the output of the template to be cached, like a securities ticker or you're constantly modifying your data sources and expect to react to them immediately on the web. None of this is a problem! Smarty also allows you to define which ones need to be cached and which do not. In fact, you can put the cache and no cache content on a page, because Smarty can accurately define what can be cached (such as perpetual navigation bar) and what can not be cached (such as the security machine at the bottom of the page). You can also set the expiration date of the cached content so that the contents of the template will only be cached for a certain period of time. This allows you to find the balance in the vast amount of information and faster Web experiences that are updated in real time.

One point to note is that this caching function is completely different from the Zend cache or other PHP accelerators.

Caching tools like Zend cache are caches of bytecode generated by PHP scripts, and Smarty caches the output of your template (which is actually a PHP script). As a result, smarty can fight alongside the Zend cache. The benchmark has been found to be a perfect solution.

As the engine creator said: When used in conjunction with the PHP Accelerator, the performance of smarty really improved. Smarty also provides variable modification functionality. You can capitalize, cut, find, and replace strings. The variable modifier simplifies the programming process, bypassing the professional syntax that programmers often use. This allows the template designer to have better control over the format of the template variable, although he first has to know which variable modifiers are available. This is a useful feature because syntax becomes concise, and it can be used by people who are not programmers.

Smarty also provides a number of built-in and custom-used functions in your template.

These functions are like the application programming interface of the template, except that the custom function can be modified and the built-in function cannot. These functions allow the 4F60 to implement conditional output (using the IF statement), dynamically iterate iterations (using foreach or section statements), read config files, and so on.

Filter Smarty filters can be categorized so that you can run the filter before or after the template is compiled. The pre-filter is used before your template is run, and the post filter is used after the template is compiled, and the output filter filters the output of the template. Why use filters? You might be asking. Pre-filters can get rid of what you don't want and make sure that what you don't want is not compiled. The post filter can then be compiled with additional information, such as creation time. The output filter allows you to modify the output of the template, such as hiding all e-mail addresses for spam harassment.

The config file is a configuration file that can store the global variables of the template. This allows you to store a set of global variables that affect all templates in one place. A typical example is the color scheme for all templates. If the color scheme needs to be modified, the template designer needs to change the value in the configuration file only. So that they don't have to modify each template one at a.

The plugin Smarty plugin architecture has emerged since version 2.0, which allows Smarty to be more user-defined. The pre-filter, post-filter, etc. discussed earlier are all available plug-in types. Other plug-in types include modifiers, control modules, compilers, and so on. Using plugins, you can make your own template functions, variable modifiers and filters. You can also use the resource plug-in to change the Smarty read data format (the default is flatten files).

Through the resource plug-in, you can save your template in the database, through a certain interface to retrieve data. Conclusion combining the Smarty template compilation with the efficiency inherent in PHP, you have an absolute speed advantage. Using the well-designed plug-in architecture, Smarty also has extended function functions, including template functions and variable modifiers. The increase in speed and function does not affect smarty affinity, in fact, Smarty is very easy to get started. The Smarty is also equipped with online shared documents. You can download it from smarty website.

One of Smarty's authors, Andrei Zmievski, is also a member of the PHP development team, who will try to ensure that Smarty follows PHP's development. So you can rest assured: any revision of PHP will be supported by Smarty.

Hope that through the above content of the introduction, can bring you help.


http://www.bkjia.com/PHPjc/445769.html www.bkjia.com true http://www.bkjia.com/PHPjc/445769.html techarticle php template engine, Smarty, it is different from other template engine, such as Fasttemplate or pattemplate, the main reason is that Smarty will convert the template into PHP script, and eliminate each time ...

  • 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.