Deep analysis of PHP template technology principle "one" _php template

Source: Internet
Author: User
Tags mixed php code php template
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. News listings, for example, are likely to be a newslist.php page, structured as follows:

?
read out the news record you want to display from the database
?>
<body>
?
while ($news = Mysql_fetch_array ($result)) {
?>
<!--output News headlines-->
?
}
?>
</body>

So what's wrong with that? First, it is not conducive to Division of division. In general, programmers will write code, designers design the page. In this way, the programmer has to wait for the artist to design the interface so that it can start work. In other words, the programmer and the art work is not synchronized. Second, is not conducive to maintenance, poor maintenance. For example, after the program is fixed, to modify the interface, then must be modified by the artist, the programmer to add again. Finally, the program structure is confusing and the readability is poor. HTML and PHP mixed together, once the program more than one, it will become very messy.

Understanding the template principle--template technology using PHP tags
Template technology is to solve these problems arise, in order to solve these problems, the first occurrence of the use of PHP tags template technology.
First, we need to understand what the purpose of the template is. How many tasks does a template have to accomplish? First, the separation of art and procedures. More specifically, the separation of data and display data is obtained. Second, the Division of labor. Good division of teamwork.
For example, a news list, if you use a template, we can divide the operation of the news list into two parts:
1, getnews.php is responsible for reading data from the database to the array $news, do not care about how $news is displayed.
2, shownews.php is responsible for the $news array output into an HTML page. And it doesn't have to ignore where the $news came from.
Well, in this way, we realized the art and program separation, 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 "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";//The actual should be read from the database.
?>

The code for shownesw.php is as follows:
<title> Show News </title>
<body>
<?= $news?>
</body>

So, this joint page listnews.php code is very simple.
?
Include (' getnews.php ');//Get Data
Include (' shownesw.php ');//Display data
?>

Summarize
The use of PHP tag template system, can be very good to achieve the separation of art and program, while facilitating the work of programmers and art staff, such as in the above example shownews.php by the art staff to maintain, getnews.php by the program personnel to maintain. And listnews.php can be maintained by the System Designer. Of course, there is a need to add some of the agreed documents.
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 data, it corresponds to the shownews.php. Finally, the controller C, corresponding to our listnews.php

Learning Interpretive Template Technology--phplib
Phplib template system to small flexible, convenient and simple features to win a lot of phper like, in the template technology occupies a certain position, it and fasttemplate such as template engines are interpretative template technology.
Before you explain the use of phplib, explain why the template system using PHP tags continues to improve. In fact, this is mainly because the PHP tag is not convenient for the artist staff. They prefer to use this visual label directly, such as {title}.
First of all, we have to modify our example before to use the visual label to deal with. The first step, change the shownews.php to Shownews.tpl, the inside of the PHP tag replaced by a visual label, SHOWNEWS.TPL code is as follows:
<title> Show News </title>
<body>
{title}
</body>
The second step, how to achieve the effect of listnews.php? In fact, it is very simple, I just have to replace the {title} not on it? So the code for the modified listnews.php is as follows:
?
Include (' getnews.php ');//Get Data
Echo Str_replace (' {title} ', $news, file_get_contents (' Shownews.tpl '))
?>

Actually the principle of phplib is just like this! For example, we want to use Phplib to achieve the above function, we only need to modify listnews.php. The modified code is 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 to the Phplib area, you can consult the corresponding manual.

Compile-type Samrty, next time you continue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.