PHP programmers will encounter the following problem: When PHP code and HTML code are together, it is very difficult to read PHP code and the entire file cannot be edited using Dreamweaver, this is a nightmare for PHP programmers and artists to modify such files. The Template technology in PHP is SyntaxHighlighte.
PHP programmers will encounter the following problem: When PHP code and HTML code are together, it is very difficult to read PHP code and the entire file cannot be edited using Dreamweaver, this is a nightmare for PHP programmers and artists to modify such files.
The Template technology in PHP is designed to solve this problem. There are many PHP template classes, and FastTemplate and PHPLib are common ones. because of their early appearance, they have a great reputation in the PHP programming field. PHP programmers don't know these two classes, just like VB programmers don't know MsgBox functions, it's incredible.
In the past, we needed to download the PHP template class. now PHP4 has its own template class IntegratedTemplate and IntegratedTemplateExtension, which features similar to PHPLib. These two classes are the relationships between child classes and parent classes. Generally, we can use IntegratedTemplateExtension. What is incredible is that IntegratedTemplate does not inherit from the PEAR class and cannot use the debug function of the PEAR class.
The following example illustrates their usage. assume that the IntegratedTemplate class and the IntegratedTemplateExtension class are in C: php4pearHTMLITX. php and C: php4pearHTMLITX. php respectively. The code we wrote is stored in C: TestPHPPHP4Welcome.htm and C: TestPHPHTMLWelcome. php. Set C: TestPHPPHP4 to a virtual directory/testphp on the Web Server and give the script execution permission to confirm that C: testphtmlwelcome.htm cannot be accessed through a remote browser. Set include_path = ".; C: php4pear" in php. ini"
Example 1:
We place the variable tag in the HTML file, use PHP code to set the variable value, replace the tag in HTML, and finally output it to the client browser.
The following is the Welcome.htm code. We put three PHP tags (variable tag): {WelcomeTitle}, {UserName}, {WelcomeMessage}
{WelcomeTitle}
Hello, {UserName}
{WelcomeMessage}
The following is the Welcome. php code.
Require_once "HTML/ITX. php ";
File: // assign values to variables. in actual code, you may obtain data from the Database and assign values.
$ WelcomeTitle = "Welcome to Heaven ";
$ UserName = "pipiru ";
$ WelcomeMessage = "We are very honored to hear from you! ";
File: // generally, 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: // replace the tag in the HTML file
$ Tpl-> setVariable (array (
"WelcomeTitle" => $ WelcomeTitle,
"UserName" => $ UserName,
"WelcomeMessage" => $ WelcomeMessage
));
File: // output the replaced HTML
$ Tpl-> show ();
?>
After this example is completed, welcome.htm can still be edited using webpage editors such as Dreamweaver and FrontPage. Welcome. php contains pure PHP code without HTML, which facilitates code modification and maintenance in the future.
If you use the IntegratedTemplateExtension class together with the Cache class in PHP4, the speed can be very good.
You can also use blocks for the PHP4 template class. with other PHP4 classes, you can easily retrieve database data pages and easily write software such as forums.
Note: to prevent users from directly viewing Web pages with Welcome.htm, place 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-language string files should all be placed in different directories, so that it is not confusing when many people work together on a project.