We're on the
1, the origin of the template
In the absence of template technology, the use of PHP development programs, usually PHP code and HTML mixed together. For example, a news list, which is probably a newslist.php page, is structured as follows:
- < ?
- read out the news record to be displayed from the database
- ?>
- < html>
- < head>...
- < /head >
- < body>
- < ?
- While ($news = mysql
_fetch_array($result)) {
- ?>
- < !--Output News headlines-- >
- < ?
- }
- ?>
- < /body >
- < /html >
So what's the problem with that? First, it is not conducive to division of labor. In general, programmers write code, and artists design pages. In this way, the programmer must wait for the artist to design the interface so that it can start working. This means that the programmer and the artist's work is not synchronized. Second, not conducive to maintenance, maintainability is poor. For example, after the program is fixed, to modify the interface, it must be modified by the artist, the programmer re-add. Finally, the program structure is chaotic and the readability is poor. HTML and PHP mixed together, once the program more, it will become very messy.
Understanding template principles--Using PHP tag templates
Template technology is just to solve these problems, in order to solve these problems, the first is to use the PHP tag template.
First, we need to understand what the purpose of the template is. How many tasks does the template need to achieve?
First, the separation of art and program. More specifically, the separation of data and display data is obtained.
Second, the Division of labor. Good division of labor.
For example, a news list, if you use a PHP tag template, we can divide the action of the news list into two parts:
1, getnews.php is responsible for reading data from the database into the array $news, do not care about how $news is displayed.
2. shownews.php is responsible for outputting the $news array into an HTML page. And it doesn't need to bother about where $news came from.
OK, so, we have achieved the separation of art and program, to achieve our initial goal, but how to combine the two pages, and realize the function of listnews.php?
This requires another page listnews.php, which is responsible for connecting the "art (display Data)" and "Programmer (Get Data)". It should be said that this page is very simple.
Suppose the code for getnews.php is as follows:
- < ?
- $ News = "News list";
- In practice, it should be read from the database.
- ?>
- The code for shownesw.php is as follows:
- < html>
- < head>
- < title> Show News < /title>
- < /head >
- < body>
- < ? = $news ?>
- < /body >
- < /html >
So, the code for this federated page listnews.php is simple.
- < ?
- Include (' getnews.php ');
- Get Data
- Include (' shownesw.php ');
- Show data
- ?>
Summarize
Using the PHP tag template system, can be very good for the separation of art and program, while facilitating the division of labor between programmers and artists, such as in the above example shownews.php by the artist to maintain, getnews.php by the program personnel to maintain. And listnews.php can be maintained by the System Designer. Of course, some of the agreed documents need to be added in the middle.
In fact, this simple example also illustrates the most basic MVC model. where m, the model, which is responsible for reading the data, is equivalent to our getnews.php. V, is to try to display the data, also corresponds to the shownews.php. And finally the controller C, corresponding to our listnews.php
http://www.bkjia.com/PHPjc/446060.html www.bkjia.com true http://www.bkjia.com/PHPjc/446060.html techarticle we are in the 1, the origin of the template before the template technology, using the PHP development program, usually PHP code and HTML mixed together. For example, a news list, is probably a ne ...