Separating HTML code from PHP

Source: Internet
Author: User
Tags php template
If you use the IntegratedTemplate class in PHP4 to separate HTML and PHP code, you will encounter the following problem: when PHP code and HTML code are together, it is very difficult to read PHP code, the entire file cannot be edited using Dreamweaver, which is a nightmare for PHP programmers and artists to modify such a file. PHP

If you use the IntegratedTemplate class in PHP4 to separate HTML and PHP code, you will encounter the following problem: when PHP code and HTML code are together, it is very difficult to read PHP code, the entire file cannot be edited using Dreamweaver, which is a nightmare for PHP programmers and artists to modify such a file. PHP

Use the IntegratedTemplate class in PHP4 to separate HTML and PHP code

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:/php4/pear/HTML/ITX. php and C:/php4/pear/HTML/ITX. php. The code we wrote is stored in C:/TestPHP/PHP4/Welcome.htm and C:/TestPHP/HTML/Welcome. php. Set C:/TestPHP/PHP4 to a virtual directory/testphp on the Web Server and give the script execution permission to confirm that C:/TestPHP/HTML/Welcome.htm cannot be accessed through a remote browser. Set include_path = ".; C:/php4/pear" 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 ";

// Assign values to variables. In actual code, you may obtain data from the Database and then assign values.
$ WelcomeTitle = "Welcome to heaven ";
$ UserName = "pipiru ";
$ WelcomeMessage = "we are very honored to hear from you! ";

// 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 );

// Specify the HTML file to replace the tag
$ Tpl-> loadTemplatefile ("Welcome.htm ");

// Replace the tag in the HTML file
$ Tpl-> setVariable (array (
"WelcomeTitle" => $ WelcomeTitle,
"UserName" => $ UserName,
"WelcomeMessage" => $ WelcomeMessage
));

// 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.

========================================================== ====================================


Use the IntegratedTemplate class in PHP4 to implement the BLOCK Function

Keywords: PHP4, template, template, IntegratedTemplateExtension, block
Reader requirements: Understand the PHP4 template Concept

It is very good to use the PHP template class for programming, but sometimes you may encounter a problem, such as outputting a table, but the number of rows of the table is not known until it is run, such as message boards, BBS, and shopping websites. At this time, the artist cannot decide to use several rows of tables in the HTML file. If loop output is written in the PHP code file, it will make it inconvenient for the artist and PHP programmers to view the code, the artist will say, where is the form? What should I do if I want to modify the color and background of the table? PHP programmers will also say, why is there,, What is the purpose? Where is the HTML file embedded ?.
PHP template-type programming generally treats this uncertain number of HTML elements as a "block". bolck programming is similar to writing a loop in code. This function is available in common PHP template classes (such as FastTemplate and PHPLib. Writing nested blocks is similar to writing multiple loops. Now we will give an example to illustrate the block programming method in the IntegratedTemplateExtension class in PHP4. In this example, we will use a two-repeating loop, the outer block is GoodsList, and the layer block is GoodsListOfSomeType.
Basic settings: assume that the code we write is stored in C:/TestPHP/PHP4/GoodsList.htm and C:/TestPHP/HTML/GoodsList. php. Set C:/TestPHP/PHP4 to a virtual directory/testphp on the Web Server and grant the script execution permission. Make sure that C:/TestPHP/HTML/GoodsList.htm cannot be accessed through a remote browser. If PHP4 is installed in C:/php4, set include_path = ".; C:/php4/pear" in php. ini"


The following is the goodslist.htm content:



Commodity List in shopping bag









































 

{UserName}: Your shopping bag contains the following items:




  Product Type Product Name Product Price  
  {Type}      
    {GoodsName} {Price}  
 

 





The following is the PHP4 code file GoodsList. php.

Require_once "HTML/ITX. php ";

// Assign values to variables. In actual code, you may obtain data from the Database and then assign values.
$ UserName = "pipiru ";
$ GoodsTypeArray = array ("Household Appliances", "books ");
$ GoodsNameArray = array ("Samsung display", "Sony single-host", "Changhong TV "),
Array ("C ++ programming ideology", "Java 2 Advanced Development Guide", "Visual Basic 5 Advanced Development Guide ",
"Flash 4 Flash pages", "design patterns can reuse the basis of object-oriented software "));
$ GoodsPriceArray = array (1024,302,102 4 ),
Array (35,62, 76, 66.5, 55 ));

// 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 );

// Specify the HTML file to replace the tag
$ Tpl-> loadTemplatefile ("GoodsList.htm ");

$ Tpl-> setVariable ("UserName", $ UserName); // User Name

// Specify the name of the outer block
$ Tpl-> setCurrentBlock ("GoodsList ");
// I like to assign the number of cycles separately before the loop
$ GoodsTypeCount = count ($ GoodsTypeArray );

// Loop the external layer block
For ($ I = 0; $ I <$ GoodsTypeCount; $ I ++)
{
$ Tpl-> setVariable ("Type", $ GoodsTypeArray [$ I]); // goods Type

// Specify the name of the internal block
$ Tpl-> setCurrentBlock ("GoodsListOfSomeType ");

$ GoodsNameArrayCount = count ($ GoodsNameArray [$ I]);

// Loop the layer block
For ($ j = 0; $ j <$ GoodsNameArrayCount; $ j ++)
{
// Replace the tag in the HTML file
$ Tpl-> setVariable (array ("GoodsName" => $ GoodsNameArray [$ I] [$ j],
"Price" => $ GoodsPriceArray [$ I] [$ j]);
$ Tpl-> parsecurityblock (); // You can also write $ tpl-> parse ("GoodsListOfSomeType") here ");
}
$ Tpl-> parse ("GoodsList"); // ends the outer block.
}

// Output the replaced HTML
$ Tpl-> show ();
?>

After running the command, you can see the effect of multiple loop replacement. After writing the code in this way, GoodsList.htm can still be edited using webpage editors such as Dreamweaver and FrontPage. GoodsList. php contains pure PHP code without HTML, which facilitates code modification and maintenance in the future.

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.