What task is implemented using the PHP tag template?

Source: Internet
Author: User

We are

1. The origin of the template

Before using the template technology, PHP development programs are usually compiled together with php code and html. For example, the news list is probably a newslist. php page with the following structure:

  1. <?
  2. // Read the news records to be displayed from the database
  3. ?>
  4. < Html>
  5. < Head>........
  6. </Head>
  7. < Body>
  8. <?
  9. While ($ news = mysql
    _ Fetch_array ($ result )){
  10. ?>
  11. <! -- Output the news title -->
  12. <?
  13. }
  14. ?>
  15. </Body>
  16. </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 template principles-using PHP tag templates

The template technology emerged to solve these problems. To solve these problems, the first thing that emerged was the use of PHP tag templates.
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 the PHP tag template for the News list, 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:

 
 
  1. <?
  2. $ News = "news list ";
  3. // Actually, it should be read from the database.
  4. ?>
  5. The Shownesw. php code is as follows:
  6. <Html>
  7. <Head>
  8. <Title> display news </title>
  9. </Head>
  10. <Body>
  11. <? = $ News?>
  12. </Body>
  13. </Html>

Then, the code for listnews. php on this joint page is very simple.
 

 
 
  1. <?
  2. Include ('getnews. php ');
  3. // Obtain data
  4. Include ('shownesw. php ');
  5. // Display data
  6. ?>

Summary

Using the PHP tag template system can effectively separate the artist and program, and facilitate the division of labor between the programmer and the artist, for example, shownews in the preceding example. php is maintained by US workers, 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


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.