Tutorials to .com

Tutorials to .com » Php » Others » SMARTY template engine

SMARTY template engine

Print View , by: iSee ,Total views: 13 ,Word Count: 2694 ,Date: Sun, 19 Apr 2009 Time: 10:34 PM

MVC with php development model to achieve the logic layer and that layer there are several templates to choose from the engine, but after the birth of the official engine SMARTY, the changes will have a choice. And the realization of the idea of it are very "avant garde" of. This paper mainly discusses the SMARTY template engine in the other the different features, a brief introduction of the engine installation and use, and a small test case SMARTY compared PHPLIB template and the speed and ease of use.
A, MVC template need
MVC was first developed in the SmallTalk language, the process of summing up of a design pattern, MVC, respectively, on behalf of the "model", "view" and "control", the purpose is to allow the development of different roles to perform their respective roles in large and medium-sized projects . In the development of Web applications, you can use the following chart to indicate the relationship between the concepts.



The diagram below shows a simple application of the WEB, the user in a browser to see the information on the contents of the database server, but before processing through the application server. Developer is responsible for the establishment of data structures, data processing logic and said data.

CGI in 96 years when China began to pop, early WEB programmers are learning to become useful to start from the HTML, and PERL in print in his line of HTML is not difficult, but step by step with the network speed, page size Second, from the original, 30 K rose tenfold. Write CGI programs have had an urgent request: PERL and HTML source code separately. Thus, social progress and development teams in the division of labor within. Artists and programmers as a result of the work on each other is not very familiar with the process of carrying out cooperation agreement in the need for a "language" to exchange.

This is not our mother tongue language or English, the term is called "template" that rely on logic and its contacts. It is a combination of HTML and scripting language features of a form of expression. In this way, that layer can be hoped for in accordance with the users through the logical format to display the processed data layer. If you have the MFC under Windows platform development experience, it will be very familiar with the Document / Document Template / View the package, which is a very typical example of the MVC. For Web applications, j2ee personal view is that the EJB / servlets / jsp is the most powerful and, of course, the beautiful simplicity of Structs. Another well-known is the realization of COM / DCOM + ASP, this portfolio in China is the most used.

MVC realization of several applications at the WEB in contrast, can be a template concept: a set of scripts to insert the HTML or a script to insert the HTML, insert the content of this change to that data. The following is an example of a template file, the template has been treated in the browser display "Hello, world!"


<html>
<head>
<title> $ greetings </ title>
</ head>
<body>
$ greetings
<body>
</ html>




Approach for the time being omitted here, in contrast to the back of the discussion so specialized.

Second, why the election SMARTY?
Of PHP, there are a lot of template engines to choose from, for example, the first coming PHPLIB template and Fast template, after several upgrades, is already quite mature and stable. If you are the hands of the current template engine very satisfied with, then please read on ...... also, I believe you as a free software enthusiasts, or the pursuit of efficiency and elegance of the developers, the following number of SMARTY introduced a bit mean.

In addition to the impact of personal preferences, I have been inclined to use the official standard to achieve, such as APACHE's xml engine Axis. Get the best possible advantage is that compatibility (MFC for example, early Win3x on the compatibility of applications than any other framework, and of course the various versions are now perfected). SMARTY before release I have been using PEAR's Integrated Template eXtension. The engine and PHPLIB template, Fast template is almost compatible, from the template to the template syntax to deal with the same: the template are read into memory and then calls parse () function, using data for the replacement of pre-marking .

The following is a look at how to do the SMARTY. After receiving the request, before the first request to determine whether the url, and if so, the required template file url "compiled" into a php script, and then redirect; if not, that is, the url template has been "compiled" a , and do not need to re-inspection can be compiled immediately after the redirect, can re-compile the conditions set to a fixed time limit, the default template file is being modified.

How to look is a bit familiar? Think of it this does not ─ ─ JSP is the principle of it! Indeed, this kind of "compiled" with the explanation in the PHP scripting engine so it is the incredible, but think about, java is not explained by the implementation of this JVM? This is called "do not only think."

As for the JAVA, on the future of PHP and then to make an observation. PHP official website announced the end of 2003 to PHP5.0 version released. This version has many new features: for example, exception handling, namespaces, and object-oriented and so on. java is bound to become increasingly closer to, SMARTY is also one of the new features, making PHP more suitable for the development of large and medium-sized projects. But it seems that I had to choose from the reasons for its easy-to-use smart ─ ─ ─ ─ the distance. However, the life cycle of a software point of view, PHP is in the long-term, developers of the functions entrusted to it more, with a view to commercial application is capable of more good than harm. As a faithful user of PHP, PHP always sure do not want to be accused of "inadequate", right?

Why SMARTY, just because it very much like JSP? Of course there are more good reasons. First of all, in addition to the cost of the first to compile a relatively high but they do not modify the template file, compiled on the readily available script cache, saving a lot of parse () time; SMARTY followed the same as PHP has a rich library, from the statistics on the number of words to the auto-indent, text surround, as well as regular expressions can be used directly; If not, such as the need for result set data show that the functions of paging, SMARTY have a strong ability to expand through the form of plug-ins expansion.

Facts speak louder than words. I have devised a test procedure, through the development of speed and the difficulty of comparing these two factors and SMARTY click PHPLIB template, choose PHPLIB template because patrick article in "In the PHP world to choose the most suitable template" there is a PHPLIB template Fast template of the competition, resulting in a landslide victory PHPLIB template, which makes a very good SMARTY rivals. Testing before the installation process to talk about the need to pay attention to.

Third, the likely problems
The official website of the SMARTY, a detailed user's manual, you can choose online HTML and PDF format version. Here there is no longer involved in the content of the manual, but the initial use of the likely problems to be explained.

The first question is very fatal: can not find the required documents prompted that? Everyone is not the default directory structure in accordance with the SMARTY to write applications. It should be specified manually, assuming the directory structure is as follows:



Index.php will be required to specify the directory structure:
$ smart-> template_dir = "smarty / templates /";
$ smart-> compile_dir = "smarty / templates_c /";
$ smart-> config_dir = "smarty / configs /";
$ smart-> cache_dir = "smarty / cache /";




The first problem has been solved, then the second is: I just use Dreamweaver how to generate the template can not be beautiful? Template file is not what is the problem, but because the default SMARTY separator tag is (), Unfortunately sure javascript contains this tag. Fortunately, we can use any character as a separator, together with these two:


$ smart-> left_delimiter = "{/";
$ smart-> right_delimiter = "/}";




This is installed on basically completed, no problem.

IV contrast and analogy
First idea about the design of the test. Of course, the major rating factors is the speed. In order to speed test, taken by the practice of arithmetic mean. Repeat the test page will be generated N times the page, and then comparing the total page generation time. Another important factor is the ease of use (without regard to the expansion of the results have been compared), and therefore can not be too small to use a template. I used my personal home page of the page, a Firework + Dreamweaver generated by the HTML file, the size of about 7K. Set of variables which have taken the most commonly used blocks in PHPLIB template name in the block, and SMARTY said section. Do not look down on this call is different from standard-to-use sub-2: Template files and script files are simple-to-use syntax.



The following tests on the in-depth to come. Take a look at the syntax of the two template files: the left blue bar PHPLIB template is a template, the right belongs to SMARTY. Is not the same as personal preferences, so do not want to comment here. Comparing the focus on the handling of statements in the script, look at the PHPLIB template:


$ tpl-> set_file ( 'phplib', 'bigfile.htm');
$ tpl-> set_block ( 'phplib', 'row', 'rows');
for ($ j = 0; $ j <10; $ j + +) (
$ tpl-> set_var ( 'tag', "$ j");
$ tpl-> parse ( 'rows',' row ', true);
)
$ tpl-> parse ( 'out', 'phplib');
$ tpl-> p ( 'out');




The following is the SMARTY:


$ smart-> assign ( 'row', $ row);
$ smart-> display ( 'bigfile.htm');




SMARTY only row with the tags and the two variables, and more PHPLIB template is the template file handler, there is a baffling out. To tell the truth out of this school when I was not sure why there is now, it seems, is still uncomfortable. SMARTY why so many fewer words to deal with it? The answer is completed by the engine. If you like to study the source code, can be found in the name of Smarty_compiler.class.php there is a _compile_tag () function, which is responsible for the section of the label into a php statement. This is not an ordinary label, with its parameters and data, saving the workload of scripting, and templates on the label is not big difference in the workload can be found on the ease of use of a domain SMARTY higher.

Here we are most concerned about the turn of speed, and, after all, for a skilled web developers, difficult to master the tools and then a matter of time, however, not to mention the learning curve of this template engine technology gently. And the speed of web applications is the life, especially in the concurrent use of a template engine visits the site a lot, which is even more important. Test before the start of PHPLIB template I think will win in this link, because it has undergone many upgrades, has been basically no bug, but the engine head SMARTY too, unlike its competitors, only two documents.

Sure enough, the test results below, PHPLIB template 25% of the speed advantages:



But not always the case, I also based on a set, this was not the same as the result of:



Did not change the basic PHPLIB, but SMARTY increased rate of 25%. Continue to refresh, to be similar are the results of the second: SMARTY faster than PHPLIB template nearly 10%. I think this is the type compiler-type faster than to explain the principle. SMARTY engine itself is great, and would also like to compile the template into a php file, the speed of course, smaller than the PHPLIB template. But this is only the first situation. The second time, upon request, SMARTY The template has been found to have been compiled, so the most time-consuming step was skipped, and the opponent would also like to proceed step by step the work of Find and Replace. This is where the compiler mentioned principle is classic "space for time" examples.

V. CONCLUSIONS
The conclusion is that if you have fallen in love with SMARTY, then waiting for what? Of course, it all does not mean that, just as I used MVC model to write my personal website, not only did not reduce the workload, but always to the coupling between different levels of trouble.

SMARTY not suitable for what? To give an example of a classic manual: the weather forecast Web site. I am also thinking of one thing: the stock market. In this site will be used SMARTY re-compiled as a result of regular and low efficiency, or more suitable PHPLIB template.

This article is not to contrast the two engines, but to illustrate the advantages of SMARTY. Use it the most significant is that it is the PHP part of the new system, as an independent force, in addition to. NET and JAVA ONE of these two systems, the large and medium-sized web development there are other options. This is for the GNU project, and its meaning is no different from thousands of miles in the army of Liu and Deng Dabieshan leap.

References

SMARTY official site: smarty.php.net
Wang Chen: "In the PHP world to choose the most suitable template"
Download the test procedure in this article: test.tar.bz2


php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.