Choose the most appropriate template and usage in the PHP World 1th/2 page _php template

Source: Internet
Author: User
Tags php class php code
Choose the most appropriate template in the PHP world

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.

  1. 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).
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

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

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:

Level 1 Level 2 Level 3 Remark
/test The root directory of the test system
result.php A PHP file that tests and produces results, testing with the need to request the page in the browser to obtain 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 Use Phplib template to parse a php file with a block template
complex__test_fast.php Using Fasttemplate to parse a php file with a block template
/include Contains PHP class files. inc.php
phplibTemplate.inc.php Phplib template class file
FastTemplate.inc.php Fasttemplate class file
TplTest.inc.php Test classes that need to be used in the test, including methods such as timing, read/analyze logs, and so on.
data.inc.php Test the data file used with the block template.
/ihtml Contains template files. ihtml
Simple_phplib.ihtml Generic template files with Phplib template processing
Simple_fast.ihtml Generic template file with fasttemplate processing
Complex_phplib.ihtml Block-phplib template files with template processing
Complex_fast.ihtml A block template file with fasttemplate processing
/logs Contains log files.
Simple_phplib.log Using Phplib template to process a log generated by a generic template
Simple_fast.log Using Fasttemplate to process a log generated by a generic template
Complex_phplib.log Using Phplib template to process logs generated with block templates
Complex_fast.log Using Fasttemplate to process logs generated with block templates

Current 1/2 page 12 Next read the full text
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.