Separate artist to prevent duplication

Source: Internet
Author: User
If you are designing an interactive website, you will pay attention to two main issues: Art engineers and programs. This is also the most critical factor for a website to get rid of its content during construction. There are usually two ways to coordinate the relationship between the artist and the program: 1. first, prepare the art page, and then the programmer will directly embed ASP, JSP, PHP, and other programs in the HTML file of the art page. if you are designing an interactive website, you will pay attention to two main issues: Art and program. This is also the most critical factor for a website to get rid of its content during construction. There are usually two ways to coordinate the relationship between the artist and the program:

1. complete the art page first, and then the programmer directly embeds ASP, JSP, PHP, and other program code in the HTML file of the art page.

2. the artist and the program are both executed at the same time. However, because there is no page framework, the program can only make some key code. after both parties have completed the process, the engineer page and program code are embedded and merged again.

In the actual website construction process, due to personnel, progress, and other environmental restrictions, we usually use the above two coordination methods in a hybrid manner. However, both methods have shortcomings:

1. low efficiency. Poor coordination between the two may lead to waiting, repeated code debugging steps, and other phenomena;

2. poor debugging. Because the program code needs to be embedded in the HTML page, the Code embedding, debugging, and error correction are cumbersome;

3. inconvenient maintenance. Once the artist's design needs to be modified, such as the website revision, all the mixed pages of programs and HTML code must be rewritten;

If you are using a PHP program to build a website, congratulations, the PHP template technology will solve the above problems more satisfactorily.

So what is PHP template technology? PHP Template is the Template technology of PHPlib. it is one of the main modules in the PHPLIB Library and is developed from the Perl Template. PHPLIB is an extension of PHP. It provides many class libraries to easily implement basic functions such as user authentication and database encapsulation. We can download the latest phplib.netuse.de. To use the PHP template, you only need to unbind the template. inc file from the PHPLIB package and put it in the directory that our PHP program can call.

With the mysterious veil removed, the core concept of template technology is simple and heartbeat: to specify your art page as a template file, you only need to output the activity content on the page, such as the database, user interaction and other variables defined as {variable} are placed in the corresponding position in the template file. when the user browses, the PHP program file opens the template file, replace the variables defined in the template file with the dynamically generated content such as the corresponding database output or user interaction. for example:

Define the Template File: Mytemplate.html

The content of Mytemplte.html is:

  
.....
  
...
...
  
  


As we can see, in fact, a template file is a common HTML file, which contains the layout, art, and other elements you want, and the internal activity content exists in the form of variables, and wait for replacement. Obviously, it is meaningless to directly browse the template file because it does not contain any PHP program and all the content is "dead". now let's take a look at how to call the template, make it "active.

Assume that the preceding template is used in the test. php file. when you browse the test. php file, the process of processing the test. php file is as follows:

Test. php

   $ Mydate = date ("Y, m, d"); // process the variable value
Include ("template. inc"); // call the template program module
$ Mytemp = new Template ("/directory of your Template file /");
// Create a template instance mytemp. if the template is in the current directory, use ".".
$ Mytemp-> set_file ("MyFileHandle", "MyTemplate.html ");
// Set the myfilehandlefile to the template file mytemplate.html that we want to call.
$ Mytemp-> set_var ("today", mydate );
// Call the set_var method of the template and set the today value of the variable in the template to $ mydate.
$ Mytemp-> parse ("MyOutput", "MyFileHandle ");
// Call the parse method of the template to analyze the template file pointed to by the MyFileHandle file handle and replace the corresponding variable
// Save the replacement result, that is, the complete HTML file content, in the string variable MyOutput.
$ Mytemp-> p ("MyOutput ");
// Print the value of the output string variable MyOutput
?>


It is so simple that the template technology easily separates the website design into two aspects: clear art and program, and simply and conveniently combines their results. If the artist design needs to be modified, we only need to remember the variable names in the original template and insert them to the corresponding positions in the new template. It's really easy! On the other hand, the PHP program does not have a mix of PHP scripts and HTML syntax, which greatly improves the execution efficiency of the PHP System. if it is a database or other time-consuming operations, now, you can close the database and release resources in a timely manner, which also improves the resource usage efficiency of PHP and related systems to a certain extent.

Now, we will introduce the basic concepts of PHP template technology. next time we will introduce more advanced template applications!

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.