Separate art to avoid repetition

Source: Internet
Author: User
Tags html page variables php file php and php script php template repetition variable
Repeat if you are designing an interactive website, you will certainly focus on two major issues, namely art and programs. This is also a Web site in the construction of its content after the most critical elements. There are usually two ways to coordinate the relationship between art and programs:





1. First do the art page, then the programmer directly in the art page HTML file embedded asp, JSP, PHP and other program code.





2. Art and program at the same time, but because there is no page frame, the program can only make some key code, the two sides completed after the art page and program code embedded synthesis.





in the actual site construction process, due to people, progress and other environmental constraints, we usually mix the above two coordinated approach. However, both approaches have deficiencies:





1. Efficiency is not high. The coordination between the two is not likely to produce wait, repeat code debugging steps, and other phenomena;





2. debugging is not smooth. Because the program code eventually needs to embed in the HTML page, the code inserts, the debugging, the error correction all is quite cumbersome;





3. Maintenance inconvenience. Once the art design needs to be modified, such as the website revision, then all programs and HTML code mixed pages need to be rewritten;





If you are using PHP to build a Web site, then congratulate you, PHP template technology will be more satisfactory solution to the above problems.





So what is the template technology for PHP? The PHP template, the Phplib template technology, is one of the main modules in the Phplib Library, developed from the template of Perl. And Phplib is an extension in PHP, provides a lot of class library, can easily realize some basic functions such as user authentication, database encapsulation and so on. We can download the latest version of the phplib.netuse.de. To use a php template, just unpack the Template.inc file in the Phplib package and put it in the directory that our PHP program can call.





To peel away the veil of mystery, the core concept of template technology is simply a heartbeat: to designate your Art page as a template file, simply place the contents of the activity in the page, such as the database output, the user interaction, and so on as a variable in the form {variable} in the appropriate location in the template file, and when the user browses, The template file is opened by the PHP program file, and the variables defined in the template file are replaced, of course, replaced by the corresponding database output or user interaction dynamic generated content, examples are as follows:





definition template file: mytemplate.html




The contents of
mytemplte.html are:


<html>


  .....


<body>


  ...


  ...


</body>


</html>





we can see, in fact, template file is a common HTML file, it contains the layout you want, art and other elements, while the internal activities of the contents of the form of variables exist, and wait to be replaced. Obviously, the template file is directly browsed is meaningless, because it does not contain any PHP program, all the content is "dead", now we look at how to call the template, let it "activity" up.





Assuming that we use the above template in the test.php file, the test.php file processing process is as follows when the user browses the test.php file:





test.php


<?php


$mydate =date ("Y-year m-month D-Day"); Handle the value of a variable


include ("Template.inc"); Transfer the template program module


$mytemp = new Template ("/Your template file directory/");


Create a template instance mytemp, if the template is in the current directory, then use the "." We can
.

$mytemp->set_file ("Myfilehandle", "mytemplate.html");


//Set Myfilehandle file handle points to the template file that we want to call mytemplate.html


$mytemp->set_var ("Today", mydate);


//Call the Set_var method of the template, set the variable today value in the template to $mydate


$mytemp->parse ("Myoutput", "Myfilehandle");


//calls the template's Parse method, analyzes the template file that the Myfilehandle file handle points to, and replaces the corresponding variable


//And save the complete HTML file contents in the string variable myoutput in the replacement result


$mytemp->p ("Myoutput");


//PrintOut the value of the string variable myoutput


?>





is so simple, template technology is very easy to separate the site design into a clear art and program two aspects, and their results simply and conveniently combined. If the art design needs to be modified, then we just remember the original template variable names, insert them into the new template corresponding position on it. It's really easy! On the other hand, in the PHP program basically does not have the PHP script and the HTML syntax mixes the phenomenon, greatly enhances the PHP system the execution efficiency, moreover if is the database and so on some time-consuming operation, now may close the database in time, releases the resources, This also improves the efficiency of resource usage in PHP and related systems to some extent.





Well, the basic concept of PHP template technology is introduced here, the next time we introduce more high-level template application!





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.