This is a very old article, but its introduction of the Smarty template engine related knowledge, it seems, still benefit, here to share, for your reference. The logical and presentation layers of the MVC development model in PHP are available in a variety of template engines, but when the official engine Smarty is born, the choice changes. Its concept and implementation are quite "avant-garde". This paper mainly discusses the different characteristics of smarty to other template engines, briefly introduces the installation and use of the engine, and compares the speed and usability of Smarty and phplib template with a small test case. First, MVC needs templates MVC was the first design pattern that was summed up in the development of the Smalltalk language, and MVC represented "model", "View" and "control", respectively, in order to make different development roles perform in large and medium-sized projects. In the development of network application, it can be used to express the relationship between the concepts. The diagram shows a simple Web application in which the user sees the information on the database server, but before it is processed by the application server. Developers are responsible for building data structures, the logic of processing data, and methods of representing data. When CGI began to pop in China in 96, early Web programmers began learning from HTML, and it was not difficult to print a line of HTML in Perl, but as the network stepped up, the page size soared 10 times times from the original 20 or 30 K. Writing CGI programs creates an urgent requirement: to separate Perl from the HTML source. Thus, social progress is embodied in the Division of labor within the development group. Because artists and programmers are not very familiar with the work of each other, in the process of cooperation need to use a contractual "language" to communicate. This language is not our mother tongue or English, the term is called "template", logic and expression rely on it to contact. It is a form of expression that combines HTML and scripting language features. In this way, the presentation layer can display data that has been processed by the logical layer in the format that the user wants. If you have the development experience of MFC under the Windows platform, then you will be familiar with the Document/document Template/view package, which is a typical example of MVC. For Web applications, the ejb/servlets/jsp is the most powerful and, of course, a concise and graceful structs. Another well-known implementation is com/dcom+asp, which is the most used in our country. By comparing several MVC implementations in Web applications, you can get a concept about templates: A set of HTML-inserted scripts or inserted script HTML to represent the changed data through this inserted content. Here is an example of a template file that is processed and displayed in the browser "Hello, world!"
- $greetings
- $greetings
Copy CodeThis is omitted for the moment, and is discussed in detail later. Second, why Choose Smarty? For PHP, there are a number of template engines to choose from, such as the first phplib template and the Rising Star fast template, after several upgrades, has been quite mature and stable. If you are satisfied with the template engine currently in hand, then ... Please also look down, I believe you as a free software enthusiast or the pursuit of efficiency and elegant developers, the following smarty how much will be a bit of meaning. In addition to the impact of personal preferences, I have been inclined to use the official standards of implementation, such as Apache's XML engine axis. The advantage is that you get the best possible compatibility (such as early MFC's compatibility with WIN3X is better than other application frameworks, and of course the various versions are now perfect). I have been using the integrated Template eXtension in PEAR before Smarty was released. This engine is almost compatible with the Phplib template, Fast template, from the syntax of the templates to the processing of templates expatiating: it reads the template into memory and then calls the parse () function, replacing the preset tag with data. Let's see how Smarty is doing. After receiving the request, first determine whether the URL is requested for the first time, if so, the template file required for the URL "compiled" into a PHP script, and then redirect, if not, that is, the URL template has been "compiled", check do not need to recompile can immediately redirect , the recompile condition can be set to a fixed time limit, and the default is that the template file is modified. What, does it look familiar? Remember--this is not the principle of JSP! It is true that this "compilation" is unthinkable on an explanatory scripting engine such as PHP, but think about it that Java is not being interpreted by the JVM. This is called "No can not do, only unexpected." Now that we've talked about Java, I'll make a little bit of a comment on PHP's future. The official PHP website announced the release of the PHP5.0 version by the end of 2003. This version has many new features: exception handling, namespaces, more object-oriented, and more. It can be said that more and more toward Java, Smarty is also one of the new features, making PHP more suitable for large and medium-sized project development. But it seems that the reason I chose it--smart and easy to use--is getting farther away. But in terms of the lifetime of a software, PHP is growing, and developers are giving it more functionality to be competent for business applications that outweigh the disadvantages. As a loyal user of PHP, certainly do not want PHP is always accused of "insufficient capacity" it? Why choose Smarty, just because it looks like a JSP? There are, of course, better reasons. First, in addition to the high cost of the first compilation, as long as the template file is not modified, the compiled cache script is readily available, eliminating a large number of parse () time, followed by smarty like PHP has a rich library of functions, from the count of words to automatic indentation, Text wrapping and regular expressions can be used directly, if not enough, such as the need for data result set pagination display function, Smarty also has a strong ability to expand the form of plug-ins can be expanded. Truth speaks louder than eloquence. I designed a test program that compares the speed and development difficulty of the smarty and phplib template, choosing Phplib template because there is a phplib in Patrick's article "Choosing the most appropriate template in the PHP world" Template to Fast template contest, the result Phplib template victory, this makes Smarty have a good opponent. Before testing, talk about the issues that you need to be aware of during the installation process. |