Implement HTML and PHP code separation using the IntegratedTemplate class in PHP4
People who use PHP programming will encounter such a problem: When the PHP code is together, see the PHP code is very hard, and the entire file cannot be edited by Dreamweaver, which is called this for PHP programmers and the art. The file is like a nightmare. Template technology in PHP is to solve this problem. There are a lot of PHP templates, and FastTemplate and phplib are more common, because the appearance of the PHP programming world is very large. PHP programmer does not know these two classes, just like the VB programmer does not know the MSGBOX function, is an incredible thing. We need to download the PHP template class before, now PHP4 has its own template IntegratedTemplate and IntegratedTemplateExtension, features and phplib. These two classes are the relationship between subclass and parent class. Generally we can use IntegratedTemplateExtension. What is incredible is that IntegratedTemplate is not inherited from the PEAR class and cannot use the PEAR class's debug function. The following example shows their usage, assuming the IntegratedTemplate class and the IntegratedTemplateExtension class are in c: / php4/pear/html/itx.php and c: / php4/pear/html/itx.php. The code we wrote is placed in c: /testphp/php4/welcome.htm and c: /testphp/html/welcome.php. Set C: / TestphP / PHP4 to virtual directory / testphp on Web Server and give the script permissions, confirm that c: /tephp/html/welcome.htm cannot be accessed via remote browser. In PHP.INI, set include_path = ".; C: / PHP4 / PEAR" Example 1: We placed the tag of the variable in the HTML file, set the value of the variable with the PHP code, then replace the tag in the HTML, final output Go to the customer browser. The following is Welcome.htm code, we put three PHP TAG (variable tags): {Welcome}, {WelcomeMessage}
{WelcomeMessage} p> body> html>
The following is Welcome.php code
phpRequire_once "html / itx.php"; // The following is assigned to the variable, which may get the data from Database in the actual code and then assign $ WelcomeTitle = "Welcome to the web paradise"; $ username = "Pip Pipilu" $ WelcomeMessage = "Your arrival is profound!"; // Generally, this global variable is placed in a separate file, easy to maintain $ html_code_file_root = "../html/"; $TPL = New IntegratedTemplateExtension ($ HTML_CODE_FILE_ROOT); / / Specify HTML file to replace TAG $ TPL-> loadTemplateFile ("Welcome.htm"); // Replace TAG $ TPL-> setvariable in the HTML file (Array "=> $ WelcomeTitle, "Username" => $ usrname, "WelcomeMessage" => $ WelcomeMessage)); // Output HTML $ TPL-> show ();?> Welcome.htm can still use web editing For the editing of Dreamweaver, FrontPage, Welcome.php is pure PHP code, without HTML, which is convenient for modification and maintenance. If you use the IntegratedTemplateExtension class and the Cache class in PHP4, you can get a good effect at speed. The PHP4 template class can also use Block, and the class with other PHP4 can easily realize the page page of database data retrieval, and easily write software such as the forum.
Supplementary description: To prevent users from watching the web directly with Welcome.htm, Welcome.htm is placed in the directory that the customer has visited (as long as it is not in the virtual directory of the web server). For large PHP projects, pictures, PHP code, HTML files, multilingual string files should be placed in different directories, so that they are not confusing when they do a project together.