PHP-web application development: Template
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. 1 Template Treatment The design template processing class is mainly necessary to complete the following tasks: • Read the displayed HTML code from the template file. · Combine the template file and the actual data generated to generate the result of the output. • Allows multiple templates simultaneously. · Allow the nested template. • Allows a separate portion of the template to be processed. 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. 2 Template Treatment The implementation of 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. 3 Template Treatment The use of 3.1 The most basic example In order to see, it is assumed here that the template file, the file using the template PHP file and the template processing class is 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:
Please think, if you do not need to set it, is this example you need 3 template files? Template file 1, page.ihtml