Home >
Article > Backend Development > [PHP] A simple and in-depth introduction to the template engine Smarty_PHP tutorial
[PHP] A simple and in-depth introduction to the template engine Smarty_PHP tutorial
WBOYOriginal
2016-07-21 15:59:31848browse
Introduction to Smarty
What is a template engine
I don’t know since when, some people started to feel dissatisfied with embedding Server Script in HTML. However, whether it is Microsoft's ASP or open source PHP, they are all web server-side languages with embedded Server Script. Therefore, some people think that it would be better if the program application logic (or business application logic) and the web page presentation (Layout) logic can be separated?
In fact, this problem has existed for a long time. When interactive web pages became popular, users of both ASP and PHP were both program developers and visual designers. But usually these users are either good at programming or good at art. If they want to take care of both at the same time, they will lose a lot of brain cells...
So the template engine came into being! The purpose of the template engine is to achieve the function of logical separation mentioned above. It allows program developers to focus on data control or function realization; while visual designers can focus on web page layout, making the web page look more professional! Therefore, the template engine is suitable for use by the company's website development team, allowing everyone to use their expertise!
As for the template engines that the author has come into contact with, they are roughly divided into two types according to the way the data is presented: template engines that need to be processed by programs and template engines that are completely determined by the template itself.
In a template engine that needs to be processed by a program, the program developer must be responsible for the presentation logic of the variables, which means that he must process the contents of the variables before outputting them to the template before he can do the assignment work. . In other words, program developers still have to write more programs to determine the appearance of variables. The template engine, which is completely determined by the template itself, allows variables to be assigned directly to the template, allowing the visual designer to decide how the variables are presented when designing the template. Therefore, it may have another set of its own template program syntax (such as Smarty) to facilitate the presentation of control variables. But in this way, visual designers also have to learn how to use template language.
The operating principle of the template engine. First, let's take a look at the following operation diagram:
General template engines (such as PHPLib) obtain the template to be parsed when creating a template object, and then After inserting the variables, parse the template through the parse() method, and finally output the web page.
For Smarty users, there is no need to do any parse actions in the program, Smarty will automatically do it for us. Moreover, if the template of a compiled web page has not changed, Smarty will automatically skip the compilation action and directly execute the compiled web page to save compilation time.
Using some concepts of Smarty
In general template engines, we often see the concept of regions. The so-called blocks will probably look like this:
Region content
Most of these blocks will be used in PHP programs with if or for, while To control their display status, although the template looks much simpler, as soon as the template is changed to a different display mode, the PHP program must be changed again!
In Smarty, everything is based on variables, and all presentation logic is controlled by the template. Because Smarty has its own template language, whether a block needs to be displayed or repeated, it is presented using Smarty's template syntax (if, foreach, section) and variable content. In this way, it feels like the template has become a bit complicated, but the advantage is that as long as you plan it properly, you don't have to change a single line of the PHP program.
From the above description, we can know that when using Smarty, we need to master one principle: clearly separate the program application logic and the web page rendering logic. That is to say, there should not be too much HTML code in the PHP program. In the program, you only need to decide which variables should be inserted into the template, and let the template decide how to present these variables (or even not appear at all).
Basics of Smarty
Installing Smarty
First, we first decide where to place the program.
Under Windows, there may be a location similar to this: "d:appservwebdemo".
Under Linux, the location may be similar to this: "/home/jaceju/public_html/".
Go to Smarty’s official website to download the latest Smarty package: http://smarty.php.net.
After unlocking Smarty 2.6.0, you will see a lot of files, including the libs folder.在 libs 中应该会有 3 个 class.php 檔 + 1 个 debug.tpl + 1 个 plugin 资料夹 + 1 个 core 资料夹。然后直接将 libs 复制到您的程序主资料夹下,再更名为 class 就可以了。就这样?没错!这种安装法比较简单,适合一般没有自己主机的使用者。
Let’s dive into the test. Let’s first look at the syntax of the two template files: the left side of the blue bar is the PHPLIB template, and the right side belongs to SMARTY. Personal preferences vary, so I won’t comment here. Focus on comparing the processing statements in the script, first look at the PHPLIB template:
SMARTY only There are two variables, tags and row, while PHPLIB template has a handler for the template file and an inexplicable out. To be honest, I didn't know why this out existed when I first learned it. It still seems awkward now. Why does SMARTY have so few processing statements? The answer is that the work is done by the engine. If you like to delve into the source program, you can find that there is a function called _compile_tag() in Smarty_compiler.class.php, which is responsible for converting the section tag into a PHP statement. This is not an ordinary label. It has parameters and data, which saves the workload of script programming. The workload on the template label is not much different. It can be judged that SMARTY is higher in ease of use.
Now it’s our turn to focus on speed. After all, for a skilled web developer, it is only a matter of time to master the most difficult tool, not to mention the template engine, a technology with a flat learning curve. Speed is the life of a web application, especially when the template engine is used on a site with a large number of concurrent visits, which is even more important. Before the test started, I thought PHPLIB template would win in this aspect because it has been upgraded many times and has basically no bugs. Moreover, SMARTY's engine is too big, unlike its opponent which only has two files.
Sure enough, the test results are as shown below, PHPLIB template has a 25% speed advantage:
But it won’t always be like this. I pressed refresh again and got a different result this time:
PHPLIB is basically unchanged, but SMARTY has increased the speed by 25%. Continue to refresh, and you will get results similar to the second time: SMARTY is nearly 10% faster than PHPLIB template. I think this is why the compiled version is faster than the interpreted version. The SMARTY engine itself is very large, and the template needs to be compiled into a php file, so the speed is certainly not as fast as the compact PHPLIB template. But this is only the case the first time. When receiving the request for the second time, SMARTY found that the template had already been compiled, so the most time-consuming step was skipped, and the opponent had to perform search and replacement step by step. This is a classic example of "exchanging space for time" mentioned in the principles of compilation.
5. Conclusion
The conclusion is that if you have fallen in love with SMARTY, then what are you waiting for? Of course, this does not mean that it is omnipotent. Just like when I use the MVC model to write my personal website, not only does it not reduce the workload, but I always have to worry about the coupling between different levels.
What is SMARTY not suitable for? Take a classic example from the manual: the weather forecast website. Another thing comes to mind: the stock market. Using SMARTY on this kind of website will be inefficient due to frequent recompilation, so PHPLIB template is more suitable.
This article is not to compare the two engines, but to illustrate the advantages of SMARTY. The most meaningful thing about using it is that it is part of the new PHP system. As an independent force, in addition to the two major systems of .NET and JAVA ONE, there are other options for large and medium-sized web development. For the GNU project, its significance is no different than Liu and Deng's army leaping thousands of miles into the Dabie Mountains.
http://www.bkjia.com/PHPjc/317353.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317353.htmlTechArticleSmarty introduces what is a template engine. I don’t know since when, some people began to feel dissatisfied with embedding ServerScript in HTML. . However, whether it is Microsoft ASP or open source PHP,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn