Select the most appropriate template in the PHP world-compare PHPLIBTemplate and FastTemplat

Source: Internet
Author: User
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, the most popular templates are PHPLIBTemplate and FastTemplate. we have evaluated the usability and speed of the technology-for the template application in the PHP project, it is a good way to handle the performance layer recommended for 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 the root directory of the test system

Result. 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
Php file for simple _ test_fast.php to analyze general templates using FastTemplate
Complex _ test_phplib.php uses PHPLIB Template to analyze php files with block templates
Complex _ test_fast.php uses FastTemplate to analyze php files with block templates

/Include a PHP file. inc. php
PhplibTemplate. inc. php PHPLIB Template class file
FastTemplate. inc. php FastTemplate class file
The test classes required for the TplTest. inc. php test, including timing, Reading/analyzing logs, and other methods.
Data. inc. php: data files used to test block templates.

/Ihtml contains the template file. ihtml
Simple_phplib.ihtml: General Template files processed by PHPLIB Template
Simple_fast.ihtml: General template files processed by FastTemplate
Complex_phplib.ihtml uses PHPLIB Template to process Template files with blocks
Complex_fast.ihtml uses FastTemplate to process template files with blocks

/Logs contains the log file. log
Simple_phplib.log uses PHPLIB Template to process logs generated by general templates.
Simple_fast.log uses FastTemplate to process logs generated by general templates
Complex_phplib.log uses PHPLIB Template to process logs generated with blockchain templates
Complex_fast.log uses 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 makes it necessary to enter http: // localhost/tpl_test/result in the address bar of the browser. 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 where it is located-for example, my own machine is called patrick ). The following are my own results in a test: (test result data explanation)

Name Description Remarks
Amount test count (total number of consecutive requests to this page) this parameter can be modified in the result. php file
The maximum processing time of max_seq ranges from 1 to amount.
Max_value peak data of the maximum processing time for reference
The number range of min_seq minimum processing time is between 1-amount.
Min_value: the peak value of the minimum processing time for your reference.
The most valuable data in the average processing time 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)
Related Article

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.