Choose the most appropriate template in the PHP world--Compare phplib template and Fasttemplate

Source: Internet
Author: User
Tags format array variables php class php file php code version variable
Comparison | template application in template PHP engineering is a good way to handle the presentation layer recommended in medium and large projects. But specific to the implementation of the template, the use of what existing template technology needs to be compared.
There are two kinds of phplib template and Fasttemplate in the more focused template processing in the PHP world, we evaluate the usability and speed of technology-want to know the result?

The cause of the matter: Have you ever used fasttemplate?
As for the template application in PHP Engineering, my colleagues and I have already touched on a lot of projects-for the benefit of it, I think there have been a lot of "predecessors" discussed in both the actual development stage and the design pattern. As far as project implementation is concerned, in some medium-sized or even large projects, effectively the HTML (and other forms of the presentation layer) and PHP code, not only in the development phase can improve the efficiency of the interface designers and application writers, but also to the testing and maintenance of the project to bring great convenience.

But--the purpose of this article is not to discuss the pros and cons of templates, nor to teach instructional tutorials on how to use templates in PHP projects, but to compare the most popular template processing methods in the two PHP worlds with an application perspective (in fact, it's just two template classes): Phplib Template and Fasttemplate.

In fact, I have been in the "quiet" use of the Phplib template--is stable and looks very good speed, So that I don't want to go out of my way to look for a better alternative--although I know there's something fasttemplate on this planet (and it's famous in the World of Perl). Until one day, a colleague asked me: "Do not know how fasttemplate?" Why don't we try Fasttemplate? "

"Well, let's try it!" "But as a safe way, it's best to be able to understand it more fully before any new models or methods are introduced into the project, and find one or several reasons enough to convince yourself and your colleagues to adopt it--for fasttemplate."

Protagonist appearances: Understanding Phplib template and Fasttemplate
As I have said before, I have been using phplib for some time-I think you may be just like me in front of the screen and very impressed with this excellent tool class library. Again, when I started looking for a template solution, it was natural to search the toolbox closest to me, so I found the template class in Phplib. After first browsing through its API (and, of course, thanking Phplib for detailed documentation), I began the process of using it-until now.

And Fasttemplate seems to be a bit more famous, naturally in the Perl world of its own making, and it seems to be in the PHP world alone enough to convince people of its power.

On the use of both the way, I would like to have a few more nonsense here; but after all, I'm afraid I'm probably writing two tutorials and not being popular with existing tutorials--a famous tutorial on phplib template and fasttemplate in the resources of this article, If you think you haven't got a sense of the two templates or one of them, it's recommended that you take a look at the two articles and you'll get a lot of useful knowledge of template applications.

(Some mouse clicks and eye movements even after writing the test code in person) now that you have some understanding of both templates, you may have found a lot of similarities between them, and I will summarize these places below.

Settings for variables
It is obvious that the form of {FOO} or {BAR} is in the specified form in both templates; that is, the template file itself should look consistent in both template processing (for example, all of the HTML files contain a variable that will be replaced with the {} identified in the middle).
Initialization of template classes (builder of classes)
You need to specify the directory location where the template file exists when you build the template class.
Substitution of variables
The most commonly used in template processing is variable substitution, both ways except for the method name (phplib template uses Set_var (), and Fasttemplate uses assign ()), which is almost consistent with the use of (key, value) , you can also pass an array (array (key=>value)) directly.
Processing of template files
is a way to specify a handle (handler) for each template file, and the handle can also replace the variable in another template file as the value of the variable.
Parsing, output process
It is necessary to invoke the parse () method (the method name is the same) to parse the template file that needs to be output and assign it to a handle, and then invoke the method of the respective output (phplib template is P () and Fasttemplate is Fastprint () ) outputs the contents of the handle and ends the processing.
The process of repeating parsing
For example, a few records from the database need to show that the template file only can be replaced by a row of variables, it is very necessary to this function. Both have this functionality, but are slightly different when used (phplib template uses parse (handler, value, True) and fasttemplate with parse (handler,. Value) Add a point to the front of the value), it should be said that the Phplib template method is relatively graceful.
The process of block parsing (or it can be called dynamic parsing)
Imagine that you need to take the qualifying data out of the database and display it in a Web page--but because the conditions are different, you don't know exactly how many data you're going to have--and if you have to adopt a template then the block is the best choice. It is the part of the template that is defined with a specific symbol that can be parsed and added to the output page (rather than the previous parse being overwritten later). The blocks may be as shown below (The block setting on the left is the Phplib template, and the fasttemplate on the right):

Well, if you're still a little confused about the pale text, let's take a look at the two detailed template processing routines! (If you are interested in discovering the following test code, you will find that two examples are actually from there.)

How does it feel to be almost the same? Here is an example of block parsing, and you'll find the same effect:

Our testing objectives and results
End of the Phplib template and fasttemplate Understanding, should be able to go to the point of this article--in the application environment, of course, you should choose easy to use the same speed of the ideal parts building system, then for such two similar technologies, it is necessary to evaluate. The evaluation should be made up of two parts: the degree to which technology is used and the speed at which the former is part of the review, while the latter is part of the test. For the former, we comment primarily on the APIs provided by the two classes; for the latter, we will let the data of the test speak, and of course there is a need to write some simple test code.

Round one: Ease of use of technology
This round is focused on the use of APIs provided by Phplib template and Fasttemplate. It should be said that the API provided by the former is more consistent with some of the common coding conventions in PHP (especially when you use other classes of phplib in your project, such a norm would have a good effect on the whole project), and some of the latter's methods were always a bit awkward (I hope you don't think it's just my narrow view, For example, Fastprint () and so on, and the parameters of the method is not very "authentic", which you can also see from the code just now.

Another point to note is that for the parsing of the template blocks, Fasttemplate is not supported until the most recent version. That is, if you're using a previous version, you have to store the content separately somewhere, and then attach the file to the template analysis process--it's really unpleasant, especially for web designers, to work with the output of records in the database.

Of course there is one more thing to look at--that's support for PHP versions. Phplib was created in the PHP3 era, which is similar to fasttemplate, but according to our application, Phplib is pretty good in the current PHP4 environment, and Fasttemplate's web page shows some information indicating that there may be some bugs for PHP4.

Well, that's a lot of talk (maybe you'll think it's all fasttemplate), the winner of this round is obvious: Phplib Template, especially if you're using Phplib's other classes at the same time, This technology is more user-friendly (you will not be unfamiliar with the APIs from the same development team).

Round two: processing speed
Maybe that's what a lot of people are most concerned about--in this round, we will use two types of template processing: one is the normal analysis, substitution, the other is the block parsing, replacement-and this is also the two ways in the actual system to apply the most: the former is the General page processing, The latter is about the output processing of the database content. At the same time, because the template file format of the two template classes is basically the same, we can provide almost consistent template file for two template parsing, which increases the reliability of the test.

This speed test will be developed before a test plan, simply to two different processing methods to write two PHP test pages, while there is a control of the test page multiple calls to the two pages and record the time to collect test data. (If you are interested you can also refer to the following detailed test plan, you may be more in-depth understanding of this test will help)

Summary-After the entire test system is complete, we should be able to get a list of the following files in the/test directory:

(a bit of a complex test scenario)
The first is to determine the test hardware and software environment-the hardware must be its own machine, the Intel Celeron 733MHz, 256M ram,40g HDD; the OS in the software platform is apache+php for Win2K Pro,web Server and operates as a module.

The second is the system that plans this test--of course, start with a tpl_test new directory in the Web server's document root directory to place all of this test files, and then create an include directory under/tpl_test to store two template class files (the core of our testing, With the. inc.php as the file name extension, and a test class file (including timing and logging, and reading logs and parsing), with. inc.php for file extensions) and a data file (for block parsing test preparation, including a two-dimensional array, the same as the. inc.php for file extension), the establishment of IHT ML directory to store the template files used (need to be resolved template files, to. ihtml for file extension), set up the logs directory to store the test generated log (the following is found, in fact, the test data is obtained by the analysis of these logs, to. Log for the file name extension). Of course, the two template processing php file is placed in the/test directory. The key point of this test is that you also need to create a PHP file, the above mentioned in charge of the template processing of the file to make several calls: for example, a file fast_test.php is a fasttemplate parsing template, and phplib_ Test.php is a phplib template resolution, then the results of the PHP file is responsible for multiple HTTP to request the above two pages to obtain test data.

Select the template to be resolved and the PHP program written--because both template processing is almost consistent with the format requirements of the template file itself (for example, to replace variables in the form of {VAR}, and so on), so you can try to ensure that the template used in the same test is the same as possible for the At the same time mentioned above, for the simulation reality system commonly used in two types of template applications: the General page processing and the output of the database content processing, the test template file is divided into two types: one is a common with some variables to replace the template file, The other is a template file with chunks that need to be replaced repeatedly based on what should be output. Also for these two template files, you need to write two different PHP files for parsing.

Test Method--in the browser to/test/result.php request, with parameters Type=[simple|complex], in the returned results can be seen in the two templates in simple or complex mode test results.

Level 1 Level 2 level 3 remark
root directory of/test test system

result.php to test and produce results of the PHP file, testing only need to be in the browser to request the page to get the test information
simple__test_phplib.php php files that use Phplib template to parse a generic template
simple__test_fast.php php files that use Fasttemplate to analyze a generic template
Complex__test_phplib.php uses phplib template to analyze php files with block templates
complex__test_fast.php PHP files using fasttemplate for parsing with a block template

/include contains PHP class files. inc.php phplib template class file Fasttemplate class file test classes that need to be used in the test, including methods such as timing, read/analyze logs, and so on. The data file that is used when testing with a block template.

/ihtml contains template files. ihtml
simple_phplib.ihtml a generic template file using Phplib template processing
simple_fast.ihtml a generic template file with fasttemplate processing
complex_phplib.ihtml template file with blocks used Phplib template
complex_fast.ihtml template files with blocks used for fasttemplate processing

/logs contains log files.
Simple_phplib.log uses phplib template to process a log generated by a generic template
Simple_fast.log uses fasttemplate to process a log generated by a generic template
Complex_phplib.log uses phplib template to process logs generated with block templates
Complex_fast.log uses Fasttemplate to process logs generated with block templates

After the design and writing of the test system, and the two templates for the Web design colleagues, we can access the system--------------------------------------------- Result.php?type=[simple|complex] (if you are doing this test on a different non-local server, the domain name should be the domain name of the server on which it is located-for example, my own machine is called Patrick, etc.). Here's what I've achieved in one test: (test result data explanation)

Name explanation notes
Total number of amount tests (total number of consecutive requests for this page) This parameter can be modified in the result.php file
MAX_SEQ Maximum processing time ordinal range between 1-amount
Max_value Maximum processing time value peak data for reference
Min_seq the ordinal range of the minimum processing time between 1-amount
Min_value value Peak data for minimum processing time for reference
The most valuable data in the average average processing time test

Of course, if you think the results of a test are not reliable, you can repeatedly press the browser refresh button, you can observe the results of different tests (theoretically should be the same).

Test results and issue "XX Selection Award"
Well, in the second leg of the speed-weighted test, phplib template beat Fasttemplate at an astonishing twice times, while in the first round phplib template had the upper hand with good API design and ease of use. The result is obvious-our choice award is certainly given to Phplib Template, and this test also gives us a deeper understanding of Phplib's class library design.

Subjective evaluation
Now that we have the result, fasttemplate will naturally not be able to enter our project-although it seems that we spent half a day in the results of a fruitless outcome (phplib template continues to work well on the project), but the process of testing is valuable, In particular, the use of PHP testing methods, should be in the future of similar decisions to play a certain reference role.

Test code downloads in this article (

Introduction to the famous articles of Phplib template and Fasttemplate

Templates, the Phplib Way
By David Orr, May of 2000
(Phplib Template)
Templates-why and how to use the them in PHP3
By Sascha Schumann, March of 1999
Phplib template and Fasttemplate's homepage

PHP Library Homepage
Fasttemplate Homepage
Other template processing articles and other information about PHP
PHP Builder (

The Chinese translation of the above articles and other Chinese PHP materials
PHP Chinese User (

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