Home >Backend Development >PHP Tutorial >smart flash recovery Getting started with Smarty for beginners learning PHP
PHP designers who have just started to come into contact with template engines will find it difficult when they hear Smarty. In fact, the author is no exception and dare not touch it. But later when I analyzed the program architecture of XOOPS, I began to find that Smarty is actually not difficult. As long as you master the basic skills of Smarty, it is quite sufficient for general applications. Of course, if you can lay the foundation well, you don’t have to worry about the advanced applications later.
The main purpose of this article is not to delve into the use of Smarty, which has been fully written in the official instructions for use. The author only writes down some of his own experience in using it, so that friends who want to understand Smarty but cannot get in can get some inspiration from it. Just because the content of this article is not very in-depth, friends who know how to use Smarty may find it a bit simple.
This article has been revised for the third time now. I wanted to add more information; however, due to time constraints, the author has not thoroughly studied many of Smarty’s advanced skills, so I dare not show them to you, but I believe this This article should be able to satisfy most beginners who want to learn Smarty. Of course, you are welcome to let us know if there are any fallacies in this article, and the author will correct them in the next revision.
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 can be roughly divided into two types according to the data presentation method: 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 directly assigned 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 insert the variables and use parse( ) This method is used to parse the template 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
< !-- END : Block name -->
Most of these blocks will use if or for, while to control their display status in PHP programs. Although the template looks much simpler, as soon as the display is changed, With different templates, 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, it may be a location 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 many files, including the libs folder. There should be 3 class.php files + 1 debug.tpl + 1 plugin folder + 1 core folder in libs. Then directly copy libs to your program's main folder, and then rename it to class. that's all? That’s right! This installation method is relatively simple and suitable for users who generally do not have their own host.
As for why the Smarty official manual introduces some more complicated installation methods? Basically, it is installed according to the official method. It can be installed only once on the host, and then provided to all designers under the host for direct reference when developing different programs, without repeatedly installing too many copies of Smarty. The method provided by the author is suitable for program developers who want to move programs here and there, so that they don't have to worry about whether Smarty is installed on the host.
Program folder settings
Take the author's installation of Appserv on Windows as an example. The main folder of the program is "d:appservwebdemo". After installing Smarty, we create a folder like this under the main folder:
Under Linux, please remember to change the permissions of templates_c to 777. Under Windows, cancel it as read-only.
The first small program written with Smarty
We first set the path to Smarty. Please name the following file main.php and place it in the main folder:
main.php:
include "class/Smarty.class.php";
define(@#__SITE_ROOT@#, @#d:/appserv/web/demo@#); // 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 = @#}>@#;
?>
Photo The purpose of the above setting is that if the program needs to be transplanted to other places, just change __SITE_ROOT. (Here is a reference to XOOPS)
After Smarty's template path is set, the program will follow this path to grab the relative positions of all templates (in the example, it is @#d:/appserv/web/demo/templates/@#). Then we use the display() Smarty method to display our template.
Next, we place a test.htm under the templates folder: (It doesn’t matter what the extension is, but it is convenient for visual designers to develop, so I mainly use .htm.)
templates/test.htm:
< html>
Use foreach to present array1
<{foreach item=item1 from=$array1}>
<{$item1}>
<{/foreach}>
Use section to present array1
<{section name=sec1 loop=$array1}>
<{$array1[sec1]}>
<{/section}>
Use foreach to present array2
< ;{foreach item=index2 from=$array2}>
<{foreach key=key2 item=item2 from=$index2}>
<{$key2}>: <{$item2}>
<{/foreach}>
<{/foreach}>
Use section to present array1
<{section name=sec2 loop=$array2}>
index1: <{$array2[sec2]. index1}>
index2: <{$array2[sec2].index2}>
index3: <{$array2[sec2].index3}>
<{/section}>
<{$forum[sec1].category_name} > | |
< ;/td> | <{$forum[sec1].topic[sec2].topic_name}> |