The template application in the PHP project is a good way to handle the presentation layer recommended in medium-sized or large projects. However, the implementation of the template requires a comparison of the existing template technologies.
In the PHP world, phplib template and fasttemplate are the most popular templates. We have evaluated the usability and speed of the technology-do you want to know the results? Cause: Have you used fasttemplate? For the template application in the PHP project, my colleagues and I have already been in touch with many projects-about its advantages, I think there have been many discussions on "Advanced xianzhe" in both the actual development stage and the design mode. In terms of project implementation, in some medium-sized or even large projects, HTML (and other text-style presentation layers) and PHP code are effectively separated, in the development phase, the work efficiency of the interface designers and application programmers can be improved, and the project testing and maintenance will be greatly facilitated. However, the purpose of this article is not to discuss the advantages and disadvantages of templates, nor to teach you how to use templates in PHP projects as a guiding tutorial, from the perspective of application, compare the two most popular template processing methods in the PHP world (in fact, there are only two template classes): phplib template and fasttemplate. In fact, I have been using the phplib template for "quiet" all the time -- very stable and looks good, so much that I don't want to worry about finding a better alternative-although I also know that there is something like fasttemplate on this earth (and it's still famous in the Perl world ). One day, a colleague asked me, "I don't know how about fasttemplate? Why don't we try fasttemplate? " "Okay, let's try it! "But as a secure method, it is better to have a better understanding of any new model or method before it is introduced into the project, and find one or several reasons to persuade yourself and your colleagues to use it-fasttemplate is no exception. Lead appearance: Learn about phplib template and fasttemplate I have already said that I have been using phplib for a while. I think you may have been very impressed with this excellent tool library like me in front of the screen! Similarly, when I started looking for a template solution, I naturally searched in the toolbox closest to my side, so I found the template class in phplib. After quickly browsing the APIS provided by phplib (of course thanks to phplib for its detailed documentation), I started using it until now. Fasttemplate seems to be more famous, and it is naturally like this in the Perl world where it was created. It seems like it is in the PHP world. From this point alone, it is enough to convince people of its capabilities. I wanted to talk a few more nonsense about how to use the two; however, after all, I feel that I am afraid I have written two tutorials specially, and there are no existing Tutorials that are popular.-The reference documents in this article include the famous phplib template and fasttemplate tutorials, if you do not know either of the two templates, we recommend that you first read the two articles and get a lot of useful template application knowledge. (After some mouse clicks, eye turns, and even writing test code in person) Now you have some knowledge about the two templates, and you may have found many similarities between them, I will summarize these points below.
- Variable settings
Obviously, the {Foo} or {bar} format is specified in both templates. That is, in the two template processing methods, the appearance of the template file should be consistent (for example, all HTML files contain variables marked with {} to be replaced ).
- Template class initialization (class builder)
You must specify the directory location of the template file when creating the template class.
- Replace variable
The most common method for template processing is variable replacement. The two methods are different except the method names (phplib template uses set_var (), while fasttemplate uses assign ()), the usage is almost the same-you can use (Key, value) or directly pass an array (Key => value )).
- Template File Processing
You can specify a handler for each template file, and replace the variable in another template file with the variable value.
- Parsing and output process
You must call the parse () method (this method name is the same) to parse the template file to be output and assign it to a handle, then, call the respective output methods (P () in phplib template and fastprint () in fasttemplate) to output the content of the handle and end the processing.
- Repeat the parsing process
For example, when several records need to be displayed from the database and the template file has only one variable that can be replaced, such a function is required. Both of them have such functions, but they are slightly different when used (phplib template uses parse (handler, value, true), while fasttemplate uses parse (handler ,. value) add a point before the value), it should be said that the method of phplib template is relatively beautiful.
- Block resolution (or dynamic resolution)
Imagine that you need to retrieve qualified data from the database and display it on the webpage-but because the conditions will be different, you don't know exactly how many pieces of data will be. If you want to use a template again, the block is the best choice. It is defined by a specific symbol in the template. This part can be repeatedly parsed and added to the output webpage (instead of being overwritten by the previous resolution. The block may be as shown below (the block settings used by phplib template on the left, and fasttemplate on the right ):
Well, if you are still confused about the pale text above, let's take a look at two detailed template processing routines! (If you are interested in exploring the subsequent test code, you will find that the following two examples are actually from there) How do I feel almost the same? The following is an example of block resolution, and you will find the same effect:
Our test objectives and results After learning about phplib template and fasttemplate, you should be able to go to the topic of this article-in the application environment, of course, you should choose components that are easy to use and with ideal speed to build the system, therefore, it is necessary to evaluate the two similar technologies. The evaluation should be made up of two parts: the difficulty of using the technology and the speed of the speed-the former is the comment, and the latter is the test part. For the former, we mainly comment on the APIS provided by the two classes. For the latter, we will let the test data speak. Of course, there is no need to write some simple test code in the middle.
Integration: ease of use This round mainly discusses the use of Apis provided by phplib template and fasttemplate. It should be said that the API provided by the former is more in line with some common coding practices of PHP (especially when other PHP classes are used in your project, such standardization will have a good impact on the entire project ); some method names in the latter always feel awkward (I hope you don't think this is just my narrow view, such as fastprint (), and the method parameters are not very "authentic ", you can also see this from the code just now. In addition, fasttemplate does not support template block parsing until the latest version. That is to say, if you use a previous version, you have to store the content separately when processing such content as the output recorded in the database, then attach this file to the template analysis and processing, which is really uncomfortable, especially for web designers. Of course, there is another point to consider-that is, the support for the PHP version. Phplib was created in the php3 era, which is similar to fasttemplate. But according to our application, phplib runs quite well in the current PhP4 environment, the fasttemplate webpage displays some information indicating that PhP4 may have some bugs. Well, after talking so much about it (You may think it's all about fasttemplate), the winner of this round is obvious: phplib template, especially when you are using other phplib classes at the same time, this technology is more easy to use (you will not feel unfamiliar with these Apis from the same development group ). Round 2: processing speed Perhaps this is the most important part of many people's attention-in this collection, we will adopt two templates for processing: one is regular analysis and replacement, the other is block parsing and replacement. At the same time, these two methods are most widely used in the actual system: the former is a general page processing, the latter is about the output processing of database content. At the same time, because the template files used by the two template classes are basically the same format, we can provide almost identical template files for parsing of the two templates separately, which increases the credibility of the test. A test scheme will be developed before such a speed test is conducted. Simply put, two PHP test pages will be written for the two processing methods respectively, at the same time, there is a control test page that calls the two pages multiple times and records the time for collecting test data. (If you are interested, you can also refer to the following detailed test plan, which may be helpful for you to gain an in-depth understanding of this test) Summary -- after the test system is complete, we should be able to get the following file list in the/test directory:
(A complicated test solution) The first is to determine the hardware and software environment for testing-the Hardware Must Be Your Own machine, Intel celon 733 MHz, 256 m Ram, 40g HDD; the OS in the software platform is Win2k pro, the Web server is Apache + PHP and runs in modules. Second, plan the system for this test. Of course, open a new tpl_test directory under the document root directory of the Web server to store all the files for this test; then create the include directory under/tpl_test to store two template class files (the core of our test,. inc. PHP is a file extension. inc. PHP is a file extension. inc. PHP is the file extension), and The ihtml directory is created to store the template files used (the template files to be parsed,. ihtml is a file extension). Create a logs directory to store the logs generated by the test. (The test data is actually obtained by analyzing these logs. log is the file extension ). Of course, the PHP files processed by the two templates are stored in the/test directory. The most critical point of this test is that you also need to create a PHP file to call the file responsible for template processing several times. For example, fast_test.php uses the fasttemplate parsing template for a file, phplib_test.php uses phplib template for parsing. Therefore, the PHP file that obtains the result is responsible for multiple requests to the preceding two pages in http mode to obtain test data. Select the template to be parsed and PHP programming-because the two template processing methods have almost the same format requirements for the template file itself (for example, the variables to be replaced are in the form of {var ), therefore, we can try our best to ensure that the templates used in the same test are the same as possible in order to maximize the fairness of the test. At the same time, we mentioned the two template applications commonly used in the simulation of real-world systems: for general page processing and database content output processing, the template files used for testing are also divided into two types: a common template file with some variables to be replaced, the other is the template file that needs to be replaced repeatedly based on the content to be output. For these two template files, you also need to write two different PHP files for parsing. Test method-forward/test/result in the browser. when a PHP request is submitted, the parameter type = [simple | complex] is required. In the returned results, the test results of the two templates in simple or complex mode are displayed. |
Level 1 |
Level 2 |
Level 3 |
Remark |
/Test |
|
|
Root directory of the Test System |
|
|
|
|
|
Result. php |
|
PHP file for testing and result generation. You only need to request this page in the browser to obtain the test information. |
|
Simple _ test_phplib.php |
|
PHP file for analysis of general Templates using phplib Template |
|
Simple _ test_fast.php |
|
PHP file for analysis of general Templates using fasttemplate |
|
Complex _ test_phplib.php |
|
Use phplib template to analyze PHP files with block templates |
|
Complex _ test_fast.php |
|
Use fasttemplate to analyze PHP files with block templates |
|
|
|
|
|
/Include |
|
Contains PHP files. Inc. php |
|
|
Phplibtemplate. Inc. php |
Phplib template files |
|
|
Fasttemplate. Inc. php |
Fasttemplate files |
|
|
Tpltest. Inc. php |
The test class to be used in the test, including timing, reading/analyzing logs, and other methods. |
|
|
Data. Inc. php |
Data Files used to test the blockchain template. |
|
|
|
|
|
/Ihtml |
|
Contains the template file. ihtml |
|
|
Simple_phplib.ihtml |
General template files processed using phplib Template |
|
|
Simple_fast.ihtml |
General template files processed with fasttemplate |
|
|
Complex_phplib.ihtml |
Template files with blocks processed using phplib Template |
|
|
Complex_fast.ihtml |
Template files with blocks processed using fasttemplate |
|
|
|
|
|
/Logs |
|
Contains the log file. Log |
|
|
Simple_phplib.log |
Use phplib template to process logs generated by General templates |
|
|
Simple_fast.log |
Use fasttemplate to process logs generated by General templates |
|
|
Complex_phplib.log |
Use phplib template to process logs generated with blockchain templates |
|
|
Complex_fast.log |
Use fasttemplate to process logs generated with blockchain templates |
After the design and compilation of the test system, and the two templates were requested by the person in charge of web design, now we can access this system-the hard work in the early stage allows the work of viewing the results to be entered in the address bar of the browser.Http: // localhost/tpl_test/result. php? Type = [simple | complex](If you perform this test on another non-local server, the domain name should use the Domain Name of the server, for example, my own machine is called Patrick ). The following are my own results in a test: (test result data explanation)
Name |
Explanation |
Remarks |
Amount |
Total number of tests (the total number of consecutive requests to this page) |
This parameter can be modified in the result. php file. |
Max_seq |
Serial number of the maximum processing time |
Range: 1-amount |
Max_value |
Maximum Processing Time |
Peak data for Reference |
Min_seq |
Sequence Number of the minimum processing time |
Range: 1-amount |
Min_value |
Minimum Processing Time |
Peak data for Reference |
Average |
Average processing time |
The most valuable data in the test |
Of course, if you think the result of a test is not reliable, you can press the refresh button of the browser repeatedly to observe the results of different tests (theoretically, it should be almost the same ). Test results and "XX selection Award" Well, in the second round of Speed-oriented testing, phplib template beat fasttemplate at an astonishing 2 times speed; at the same time, in the first round, phplib template had the upper hand with good API design and ease of use. The result is obvious-our selection award is certainly awarded to the phplib template. At the same time, this test also gives us a deeper understanding of the design of the phplib class library. Subjective Evaluation As a result, then fasttemplate will naturally not be able to enter our project-although it seems that we have spent half a day to get a result without change (phplib template continues to work well in the Project), however, the testing process is very valuable. Especially the PHP testing method should play a reference role in similar decision-making in the future. References Download the test code in this article (article_256.zip) Famous articles about phplib template and fasttemplate
- Templates, The phplib way
By David Orr, May of 2000 Http://www.phpbuilder.com/columns/david20000512.php3) (Phplib template)
- Templates-why and how to use them in php3
By Sascha Schumann, March of 1999. Http://www.phpbuilder.com/columns/sascha19990316.php3) (Fasttemplate)
Home page of phplib template and fasttemplate
- PHP Library homepage
Http://phplib.netuse.de)
- Fasttemplate Homepage
Http://www.thewebmasters.net/php/FastTemplate.phtml)
Other templates process articles and other documents about PhP PHP Builder (http://www.phpbuilder.com) The Chinese translations of the above articles and other Chinese PHP documents PHP Chinese user (http://phpuser.com)
About the author Wang Chen, a Web Developer, is also interested in writing and translating articles related to his own technical hobbies (Patrick is preferred when he is working and publishing articles ). My main development experience is focused on PHP. Now I love Java more, and it is also one of the advocates of open source. In my spare time, I often like to go to a bookstore. I often have activities near my home and near my alma mater-I graduated from Southeast University. You can contact him by email patrickwang@163.com. |
|