Each developer designed for a larger PHP-web application has probably has the following experience: spend a lot of time writing hypertext statements, for page typography, combined with art, etc.; or integrated program code in and HTML static A lot of time spent on the page. Indeed, using a scripting language development web application is not easy to separate the data processing and data display, but in the case of multiplayer cooperation, if data and display are unable to separate, it will greatly affect the efficiency of development, professional division of labor. In order to solve this problem, PHP also provides its own solution, there are many, this article mainly introduces the Template class in phplib. First, the design template processing class for the template processing class mainly needs to complete the following tasks:
Read the HTML code for display from the template file. Combine the template file and the actual data generated, generate the result of the output. Allows multiple templates simultaneously. Allow the nested of the template. Allows a separate portion of the template for processing. Increte the above tasks, the design objectives of the template class are: from the displayed HTML code from multiple template files, replace these display code to replace the data derived from the PHP program calculation, then output according to certain order . Among them, the replacement portion can be freely set. Reading the displayed HTML code uses a read file mode template file and data combination using a regular replacement to process multiple templates with array storage. The main idea of the nested implementation of the template is: Template and output (any intermediate analysis result) can be replaced by colleagues, and can be implemented. The processing of individual sections is configured by setting labels in a template file and then bind to the regular replacement to control, and implement some replacement. Second, the implementation of the template processing class Please refer to the template.inc in phplib, a total of 345 lines of code, with a detailed note. The following list is listed in the main function, and it is reviewed for reading: 1) Function set_file ($ handle, $ filename = ") Line 77, read file 2) Function set_var ($ VARNAME, $ VALUE =") LINE 119, setting mapping Data - Replacement Variable 3) Function Set_block ($ PARENT, $ Handle, $ Name = ") Line 96, Settings Target 4) Function Subst ($ Handle) Line 136, Perform Data Replacement 5) Function Parse ($ TARGET, $ HANDLE , $ append = false) Line 165, the combination of template files and data 6) Function P ($ VarName) line 268, output processing result Note: I downloaded the pHP-lib7.2c of the Template.inc file in the template.inc file Less one "/", add normal. Third, the use of the template processing 3.1 The most basic example is for the sake of simplicity, where the template file is assumed, the files with the template PHP file and the template processing class are placed in the same directory. The habits in phplib are the suffix of the template file using the IHTML suffix. Here is the template file to use:
Next, use the template processing class to handle the above template: // introduced Template class include ("template.inc"); // Get data required to replace $ TIMENOW = Date ("YMD H: i: s", Time ()); // instantiate a Template class $ template = new template (); // Load Test.ihtml Template $ TEMPLATE-> Set_File ("Handle1", "Test.ihtml"); // Using $ TIMENOW CurrentTime $ Template-> set_var ("CurrentTime", $ TIMENOW); // Template to Template-> Parse ("Output", "Handle1"); // Output last Result $ TEMPLATE- > P ("OUTPUT");?> Note: If you just want to use the template class in phplib, you only need to contain the template.inc class on the file header. When you create a Template object, you can specify a template file path, such as: New Template ("/ HTDOCS / Apps / Templates /"), the default is the current path. 3.2 Template Nested and Block Settings The following example comes from the reference manual with phpo, strong comprehensive, point herein is that the purpose of the setting block is independent of nested, but this example contains both. Please read it carefully, block settings to avoid this: I originally completed in a template file (static page), and partial loop content will be made separately into a template file in a template file (static page). Please think, if you do not need to set it, is this example you need 3 template files? Template file 1, page.ihtml Note: If you just want to use the template class in phplib, you only need to include the template.inc class in the file header. When you create a Template object, you can specify a template file path, such as: New Template ("/ HTDOCS / Apps / Templates /"), the default is the current path. 3.2 Template Nested and Block Settings The following example comes from the reference manual with phpo, strong comprehensive, point herein is that the purpose of the setting block is independent of nested, but this example contains both. Please read it carefully, block settings to avoid this: I originally completed in a template file (static page), and partial loop content will be made separately into a template file in a template file (static page). Please think, if you do not need to set it, is this example you need 3 template files? Template file 1, Page.iHTML