Comparison of phplib template and Fasttemplate

Source: Internet
Author: User
Tags comparison variables php code

The template application in 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).

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.