I don't know when it started, and someone started to feel less satisfied with the HTML embedded in the Server Script. However, whether it is Microsoft's ASP or open source PHP, is the embedded Server Script in the Web server-side language. Therefore, it is thought that if the application logic (or business application logic) and Web page rendering (Layout) logic separation, is it better?
In fact, this problem has long existed, from the beginning of the interactive Web page, whether it is the user of ASP or PHP is both a program developer and visual designer two identities. But usually these users are not program strong is the art of strong, if you want to both at the same time, that can die a lot of brain cells ...
So the template engine was born! The purpose of the template engine is to achieve the above mentioned logical separation function. It allows the programmer to focus on the control of the data or the function, while the visual designer can focus on the page layout and make the page look more professional! So the template engine is well suited to the company's website development team, so that everyone can play their expertise!
In terms of the author's contact with the template engine, according to the data presentation method is roughly divided into: need to match the program processing template engine and completely by the template itself decided by the template engine two forms.
In a template engine that needs to be processed with the program, the programmer must be responsible for the rendering logic of the variable, that is, he must handle the contents of the variable before outputting it to the template in order to do assign work. In other words, program developers have to write more programs to determine the appearance of variables. The template engine, which is entirely self-determined by the template itself, allows the variable to be assign directly into the template, allowing the visual designer to determine how the variable should be presented when designing the template. Therefore, it may have another set of its own template program syntax (such as Smarty), to facilitate the control of the rendering of variables. But in this way, visual designers also have to learn how to use the template language.
The working principle of the template engine, first we look at the following operation diagram:
The general template engine (such as Phplib) is to parse the template when the template object is created, and then put the variable into the parse () this method to parse the template, and finally the page output.
For Smarty users, there is no need to do any parse action in the program, these Smarty will help us do it automatically. and has compiled the Web page, if the template does not change, Smarty will automatically skip the compilation of the action, directly execute the compiled page, to save the compilation time.
Some concepts of using smarty
In the general template engine, we often see the concept of the region, so-called chunks are likely to grow like this:
<!--start:block name--
Area content
<!--end:block name--
Most of these chunks will be in the PHP program with if or for, and while to control their display state, although the template looks much more concise, but as long as a different display mode, PHP program is bound to change again!
In Smarty, everything is dominated by variables, and all rendering logic allows the template to control itself. Because Smarty will have its own template language, whether the block is to be displayed or repeated, it is presented with the Smarty template syntax (if, foreach, section) with variable content. It feels like a template gets a little complicated, but the advantage is that the PHP program does not have to change as long as it is properly planned.
From the above instructions, we can know that using smarty to master a principle: The application of the program logic and Web page rendering logic clearly separated. This means that there are not too many HTML codes in the PHP program. As long as you decide which of these variables to be in the template, let the template decide how to render the variables (even if they don't appear).
The foundation of Smarty
Installing Smarty
First, let's decide where the program should be placed.
There may be a location under Windows like this: "d:\appserv\web\demo\".
Linux may have a location like this: "/home/jaceju/public_html/".
Download the latest Smarty kit from Smarty's official website: http://smarty.php.net.
After unlocking Smarty 2.6.0, you will see a lot of files, including a Libs folder. There should be 3 class.php + 1 DEBUG.TPL + 1 plugin folders + 1 Core folders in Libs. Then copy the Libs directly to your program Master folder and rename it to class. That's it? That's right! This installation method is relatively simple, suitable for users who do not have their own host.
As for the Smarty Official Handbook, why do you want to introduce some of the more complex installation methods? Basically installed in the official way, can be installed only once on the host, and then provided to all the designers under the host to develop different programs directly referenced, and not repeatedly installed too many Smarty copies. And the way I provide is suitable to bring the program to move over the program developers to use, so as not to worry about the host installed Smarty.
Folder settings for the program
With the author in Windows installation Appserv as an example, the program's Master folder is "d:\appserv\web\demo\". After installing the smarty, we create the folder under the Master folder:
Under Linux, remember to change the permissions of Templates_c to 777. Windows will cancel it read-only.
The first small program written in Smarty
We first set the path of the Smarty, please name the following file as main.php, and put it under the Master folder:
main.php:
<?php
Include "class/smarty.class.php";
Define (@#[email protected]#, @ #d:/appserv/web/[email protected]#); No slash at the end.
$TPL = new Smarty ();
$tpl->template_dir = __site_root. "/templates/";
$tpl->compile_dir = __site_root. "/templates_c/";
$tpl->config_dir = __site_root. "/configs/";
$tpl->cache_dir = __site_root. "/cache/";
$tpl->left_delimiter = @#<{@#;
$tpl->right_delimiter = @#}>@#;
?>
PHP Template engine Smarty