People using PHP programming will encounter a problem: when the PHP code and HTML code together, look at the PHP code is very laborious, the entire file can not be edited with Dreamweaver, for PHP programmers and artists, it is like a nightmare to modify such files.
Template technology in PHP is designed to solve this problem. PHP template class has a lot of, more common is fasttemplate and Phplib, because appear early, in the PHP programming world reputation is very big. PHP programmers do not know these two classes, as VB programmers do not know the MsgBox function, is an incredible thing.
Before we need to download PHP template class, now PHP4 has its own template class integratedtemplate and integratedtemplateextension, function and Phplib almost. These two classes are the relationships of subclasses and parent classes. In general, we can use integratedtemplateextension. It is inconceivable that integratedtemplate is not inheriting from the PEAR class and cannot use the debug function of the PEAR class.
The following examples illustrate their usage, assuming that the Integratedtemplate class and the Integratedtemplateextension class are in c:php4pearhtmlitx.php and c:php4pearhtmlitx.php respectively. The code we write is placed in c:testphpphp4welcome.htm and c:testphphtmlwelcome.php. Make C:TESTPHPPHP4 the virtual directory on the Web Server/testphp and give the script execute permission to confirm that c:testphphtmlwelcome.htm cannot be accessed through the remote browser. Inside the php.ini set include_path = ".; C:php4pear "
Example 1:
We place the tag of the variable in the HTML file, set the value of the variable in the PHP code, then replace the tag in the HTML and output it to the customer's browser.
Here is the welcome.htm code, we put three PHP tag (variable tag) to: {Welcometitle}, {UserName}, {welcomemessage}
<title>{Welcometitle}</title>
Hello, {UserName}
{Welcomemessage}
The following is the welcome.php code
Require_once "html/itx.php";
file://the following is assigning a value to a variable that may be obtained from the database in the actual code and then assigned a value
$WelcomeTitle = "Welcome to Web Paradise";
$UserName = "Phi Phi Lu";
$WelcomeMessage = "We are deeply honored to have you here!" ";
file://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);
FILE://Specifies the HTML file to replace the tag
$tpl->loadtemplatefile ("welcome.htm");
file://replacing tags in an HTML file
$TPL->setvariable (Array (
"Welcometitle" = $WelcomeTitle,
"UserName" = $UserName,
"Welcomemessage" = $WelcomeMessage
) );
file://output replaced HTML
$tpl->show ();
?>
After this is written, welcome.htm can still use the Web page editor such as Dreamweaver, FrontPage for editing, welcome.php is pure PHP code, without HTML, convenient for later code modification and maintenance.
If you use the Integratedtemplateextension class with the Cache class in the PHP4, you can get a good effect on speed.
PHP4 template classes can also use Block, with other PHP4 classes can easily implement database data retrieval page, it is easy to write the forum and other software.
Additional note: In order to prevent users from using welcome.htm to see the page directly, put 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, images, PHP code, HTML files, and multi-lingual string files should all be placed in different directories, so that there is no confusion when multiple people work together on a project.
http://www.bkjia.com/PHPjc/532055.html www.bkjia.com true http://www.bkjia.com/PHPjc/532055.html techarticle people using PHP programming will encounter the problem: when the PHP code and HTML code together, look at the PHP code is very laborious, the entire file can not be edited with Dreamweaver, this for ...