Code separation people who use PHP to program have this problem: when PHP code and HTML code together, see the PHP code is laborious, the entire file can not use Dreamweaver to edit, this for PHP programmers and artists, modify such a file is like a nightmare.
The template (Template) technology in PHP is designed to solve this problem. There are a lot of PHP template classes, more common is fasttemplate and phplib, because the presence of early, in the PHP programming community has a great reputation. PHP programmers do not know these two classes, just like VB programmers do not know the MsgBox function, is an incredible thing.
Previously we need to download PHP template class, now PHP4 has its own template class Integratedtemplate and integratedtemplateextension, functions and Phplib almost. These two classes are the relationships between the subclass and the parent class. In general, we can use integratedtemplateextension. It is amazing that integratedtemplate does not inherit from the Pear class and cannot use the debug feature of the Pear class.
The following examples illustrate their use, assuming that the Integratedtemplate class and the Integratedtemplateextension class are in C:\php4\pear\HTML\ITX.php and C:\php4\pear\HTML\ respectively In itx.php. The code we write is placed in C:\TestPHP\PHP4\Welcome.htm and C:\TestPHP\HTML\Welcome.php. Make C:\TestPHP\PHP4 a virtual directory on the Web Server/testphp and Execute permissions on the script, confirming that C:\TestPHP\HTML\Welcome.htm cannot be accessed through a remote browser. Inside the php.ini set include_path = ".; C:\php4\pear "
Example 1:
We place the tag of the variable in the HTML file, set the value of the variable with the PHP code, and replace the markup in the HTML to the client browser.
The following is the welcome.htm code, we put three PHP tag (variable mark) is: {Welcometitle}, {UserName}, {welcomemessage}
<title>{WelcomeTitle}</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<style type= "Text/css" >
<!--
body,p,br,div,td,table {font-size:9pt}
-->
</style>
Here's the welcome.php code
<?php
Require_once "html/itx.php";
Here's the assignment to the variable, where you can get the data from the database in the actual code and then assign the value
$WelcomeTitle = "Welcome to the Web Paradise";
$UserName = "Pi Lu";
$WelcomeMessage = "Your arrival let us deeply honored!" ";
In general, this global variable is placed in a separate file for easy maintenance
$HTML _code_file_root = ". /html/";
$TPL = new Integratedtemplateextension ($HTML _code_file_root);
Specify the HTML file to replace tag
$tpl->loadtemplatefile ("welcome.htm");
Replace the tag in an HTML file
$TPL->setvariable Array (
"Welcometitle" => $WelcomeTitle,
"UserName" => $UserName,
"Welcomemessage" => $WelcomeMessage
) );
Output after the replacement of HTML
$tpl->show ();
?>
After this, welcome.htm can still use the Web page editor such as Dreamweaver, FrontPage for editing, welcome.php is pure PHP code, does not contain HTML, convenient for future code modification and maintenance.
If you combine the Integratedtemplateextension class with the Cache class in PHP4, you can get a good effect on speed.
PHP4 template classes can also use block, with other PHP4 classes can easily achieve the page of database data retrieval, you can easily write the forum software.
Supplemental Note: In order to prevent users from directly using Welcome.htm to view the Web page, place the welcome.htm in a directory that the customer cannot access (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 a different directory, so that many people work together to do a project without confusion.
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.