1. The origin of the template
Use PHP for development before the template technology is available Program , Usually PHP Code And HTML. For example, the news list is probably a newslist. PHP page with the following structure:
<?
// Read the news records to be displayed from the database
?>
<HTML>
<Head> ........
</Head>
<Body>
<?
While ($ news = mysql_fetch_array ($ result )){
?>
<! -- Output the news title -->
<?
}
?>
</Body>
</Html>
So what's the problem? First, it is not conducive to division of labor and cooperation. Generally, programmers write code to design the webpage by the artist. In this way, the programmer must wait for the artist to design the interface to start working. That is to say, the work of programmers and artists cannot be synchronized. Second, it is not conducive to maintenance and has poor maintainability. For example, to modify the interface after the program is fixed, the programmer must modify the interface and then add it again. Finally, the program structure is messy and the readability is poor. HTML and PHP are mixed together, and once there are more than one program, it will become very messy.
Understanding the template principle-using the PHP tag template Technology
The template technology emerged to solve these problems. To solve these problems, the first thing that emerged was the template technology using PHP labels.
First, we need to understand what the purpose of the template is. How many tasks do templates need to implement? First, the separation of art and program. More specifically, the separation of acquired data and displayed data. Second, division of labor. Good division of labor and cooperation.
For example, if you use a news list template, you can divide the operations on the news List into two parts:
1. getnews. php reads data from the database to the $ news array, regardless of how $ news is displayed.
2. shownews. php outputs the $ news array into an HTML page. It does not need to care where $ news comes from.
Well, we have achieved the separation of the artist and the program, and achieved our initial goal. But how can we combine these two pages and implement the listnews. php function?
This requires another page, listnews. php, to connect "artist (display data)" with "Programmer (get data. It should be said that this page is very simple.
Assume that the code for getnews. php is as follows:
<?
$ News = "News list"; // actually, it should be read from the database.
?>
The shownesw. PHP code is as follows:
<HTML>
<Head>
<Title> display news </title>
</Head>
<Body>
<? = $ News?>
</Body>
</Html>
Then, the code for listnews. php on this joint page is very simple.
<?
Include ('getnews. php'); // get data
Include ('shownesw. php'); // display data
?>
Summary
The PHP tag template system can effectively separate the artist and program, and facilitate the division of labor between the programmer and the engineer, for example, shownews in the preceding example. PHP is maintained by the artist, getnews. PHP is maintained by programmers. The listnews. php can be maintained by the system designer. Of course, some agreed documents need to be added in the middle.
In fact, this simple example also illustrates the most basic MVC model. M, model, is responsible for reading data, which is equivalent to our getnews. php. V is an attempt to display data, which corresponds to shownews. php. Finally, controller C corresponds to our listnews. php
Learning explanatory template Technology -- phplib
Phplib's template system has won a lot of phper's liking for its small, flexible, convenient and simple features, and plays a certain role in the template technology, both the template engine and fasttemplate engine belong to the explanatory template technology.
Before explaining how to use phplib, explain why the PHP tag template system should be improved. In fact, this is mainly because the PHP tag is inconvenient for the artist. They prefer to directly use such visualized labels, such as {Title }.
First, we will modify the previous example to use visualized labels for processing. Step 1: Change shownews. php to shownews. TPL, and change the PHP tag to a visualized tag. The shownews. TPL code is as follows:
<HTML>
<Head>
<Title> display news </title>
</Head>
<Body>
{Title}
</Body>
</Html>
Step 2: How to Implement the effect of listnews. php? In fact, it is very simple. I just need to replace {Title} with another one? Therefore, the modified listnews. PHP code is as follows:
<?
Include ('getnews. php'); // get data
Echo str_replace ('{Title}', $ news, file_get_contents ('shownews. tpl '))
?>
In fact, this is exactly how phplib works! For example, if we want to use phplib to implement the above functions, we only need to modify the code after listnews. php. is modified as follows:
<?
Include ('getnews. php'); // get data
Include ('template. Class. php ');
$ T = new template ();
$ T-> set_file ('shownesw, 'shownews. TPL ');
$ T-> set_var ('News', $ News );
$ T-> parse ('out', 'shownesw ');
$ T-> P ('out ');
?>
In addition, you can refer to the relevant manual for the phplib region.
Compile samrty.