Template
The logical layer and presentation layer of the MVC development pattern in PHP has a variety of template engines to choose from, but when the official engine Smarty is born, the choice changes. Its idea and realization are quite "avantgarde". 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 ease of smarty and phplib template with a small test case.
One, MVC needs template
MVC was the first design pattern to be summed up in the development of Smalltalk language, and MVC represented "model", "View" and "control" respectively, so that different development roles could be involved in large and medium-sized projects. In the development of Web applications, the following diagram can be used to represent the relationships among 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. What developers are responsible for is building data structures, processing the logic of data, and methods of representing data.
When the 96 CGI became popular in China, early web programmers began to learn from HTML, and it was not difficult to print a row of HTML in Perl, but as the network sped up, the page size jumped 10 times times from the original 20 or 30 K. Writing CGI programs creates an urgent requirement: Separate Perl and HTML source code. Thus, social progress is embodied in the Division of labor within the development group. Because art and programmers are not very familiar with each other's work, in the process of cooperation need to use a "language" to communicate.
This language is not our mother tongue or English, the term is called " template ", logic and representation depend on it. It is an 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 Windows platform, you will be familiar with the encapsulation of document/document Template/view, this is a typical MVC example. For Web applications, individuals think that the ejb/servlets/jsp in Java EE is the most powerful, and of course there are simple and graceful structs. Another well-known realization is com/dcom+asp, which is the most used in our country.
By comparing several MVC implementations in a Web application, you can get a concept of a template: A set of HTML-inserted scripts or scripting HTML that represents the changed data through the inserted content. The following is an example of a template file, which is processed to display "Hello, world!" in the browser.
<title> $greetings </title>
<body>
$greetings
<body>
Here for the moment to omit the treatment, in the following special comparative discussion.
Second, why Choose Smarty?
For PHP, there are a number of template engines to choose from, such as the first phplib template and the Up-and-comer fast template, after several upgrades, has been quite mature and stable. If you are satisfied with the current template engine, then ... Also look down and believe that you as a free software enthusiast or for the pursuit of efficiency and elegance of the developers, the following Smarty introduced how much will be somewhat interesting.
In addition to the impact of personal preferences, I have tended to use official standards for implementations such as Apache's XML engine axis. The advantage is that you get as good a compatibility as possible (for example, early MFC's compatibility with WIN3X is better than other application frameworks, but now all versions are perfect). I have been using the integrated Template eXtension in PEAR before Smarty released. This engine is almost compatible with phplib template, Fast template, from the syntax of the template to the processing of the template expatiating: All the templates are read into memory and then called the Parse () function, replacing the preset tags with the data.
Let's see how Smarty is doing. After receiving the request, first decide whether to request the URL for the first time, if it is, the template file required for the URL "compiled" into a PHP script, and then redirect; if not, it means that the template for the URL has been "compiled", checking that you do not need to recompile to immediately redirect , the recompile condition can be set to its own fixed time limit, and the template file is modified by default.
What, does it look a little familiar? Come to think of it--this is not the principle of JSP! It is true that this "compilation" is unthinkable in an interpretative scripting engine such as PHP, but is Java not also interpreted by the JVM? This is called "nothing can not be done, only unexpected."
Now that we've talked about Java, let's comment on the future of PHP. 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 to the 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--the deft use of it--is getting far. But in terms of the life cycle of a software, PHP is in the growth period, the developer gives it more functions, in order to be able to do business applications is more advantages than disadvantages. As a loyal user of PHP, you certainly don't want PHP to always be blamed for "lack of capacity"?
Why choose Smarty just because it looks like a JSP? There are, of course, more good reasons. First of all, in addition to the cost of the first compilation is relatively high, as long as the template file is not modified, compiled cache script is available at any time, save a lot of parse (), and then smarty like PHP has a rich library of functions, from counting words to automatic indentation, Text wrapping and regular expressions can be used directly, if not enough, such as the need for data results set paging display function, Smarty also has a strong ability to expand, through the form of plug-ins can be expanded.
Facts speak louder than words. I designed a test program that compares Smarty and phplib template with speed and development difficulty, and phplib template because there is a phplib in Patrick's article, "Choosing the right template in the PHP world." Template to fast template competition, the result phplib template swept, this makes smarty have a good opponent. Before testing, talk about the issues you need to be aware of during the installation process.
Iii. problems that may be encountered
On the official website of Smarty, there is a detailed user manual that allows you to select online HTML and PDF versions. There is no longer a reference to what is already in the manual, but an explanation of the problems that may be encountered in the first use.
The first question is fatal: Does the hint say you can't find the file you want? Not everyone writes the application according to the Smarty default directory structure. This needs to be manually specified, assuming the directory structure is as follows:
You need to specify the directory structure in index.php:
$smart->template_dir = "smarty/templates/";
$smart->compile_dir = "smarty/templates_c/";
$smart->config_dir = "smarty/configs/";
$smart->cache_dir = "smarty/cache/";
The first problem was solved, followed by the second: how can I use the beautiful template I just created with Dreamweaver? It is not a problem with the template file, but because the default tag delimiter for Smarty is {}, and unfortunately JavaScript certainly contains this tag. Fortunately we can use any character as a separator, plus these two sentences:
$smart->left_delimiter = "{/";
$smart->right_delimiter = "/}";
This installation is basically done, no problem.
[1] [2] Next page