Home > Backend Development > PHP Tutorial > Use template engine Smarty to develop PHP programs_PHP tutorial

Use template engine Smarty to develop PHP programs_PHP tutorial

WBOY
Release: 2016-07-13 17:30:46
Original
873 people have browsed it


 1. MVC requires templates
MVC was first summarized as a design pattern during the development of SmallTalk language. MVC represents "model", "view" and "control" respectively. , the purpose is to allow different development roles to perform their respective duties in large and medium-sized projects. In the development of network applications, the following diagram can be used to represent the relationship between concepts.
 

 Use template engine Smarty to develop PHP programs_PHP tutorial

This picture shows a simple WEB application. The information the user sees on the browser is the content on the database server, but it has been processed by the application server before . Developers are responsible for establishing data structures, logic for processing data, and methods for representing data. "
 
 In 1996 cgi(not many people are using it now)When it became popular in China, early WEB programmers were self-taught from HTML. In PERL It is not difficult to print lines of HTML, but as the Internet speeds up step by step, the page size has increased tenfold from the original 20 to 30 K. Writing cgi (not many people are using it now). )The program created an urgent requirement: to separate PERL and HTML source code. Therefore, social progress is reflected in the division of labor within the development team. Since artists and programmers are not very familiar with each other's work, they are not very familiar with each other's work. The process needs to use an agreed "language" to communicate
 
This language is not our native language or English. The term is called "template", and the logic and presentation rely on it. It is a combination of HTML and. An expression of scripting language features. In this way, the presentation layer can display the data processed by the logic layer in the format desired by the user. If you have MFC development experience on the Windows platform, you will be familiar with Document. /Document Template/View encapsulation, this is a very typical MVC example. For Web applications, I personally think that EJB/servlets/jsp(the first choice for SUN enterprise-level applications) in J2EE is the strongest. The big one, of course, is the simple and beautiful Structs. Another well-known implementation is COM/DCOM+ASP. This combination is the most used in our country.
 
It is implemented in WEB applications through several MVCs. By comparison, we can get a concept about templates: a set of scripts inserted into HTML, or script HTML, through which the changed data is represented. The following is an example of a template file. This template is passed through. After processing, "Hello, world!" is displayed in the browser
 
 
 
 ;
 
 $greetings
 
2. Why choose SMARTY?

For php
(as the current mainstream development language)
, there are many template engines to choose from, such as the earliest php (As the current mainstream development language) LIB template and the rising star Fast template have been quite mature and stable after several upgrades. If you are very satisfied with the template engine you currently have, then...please read on. I believe that as a free software enthusiast or a developer pursuing efficiency and elegance, the following SMARTY introduction will be somewhat interesting.
  In addition to the influence of personal preference, I have always tended to use official standard implementations, such as apache (the most popular WEB server platform on the Unix platform) ’s xml (standardization is becoming more and more Close)
Engine Axis. The advantage is that you can get the best possible compatibility (for example, the compatibility of early MFC with Win3x was better than other application frameworks, and of course now all versions are very complete). Before SMARTY was released, I had been using the Integrated Template eXtension in PEAR. This engine is almost compatible with php
(as the current mainstream development language)LIB template and Fast template. From the syntax of the template to the processing of the template, they are the same: both read the template into the memory. Then call the parse() function to replace the preset tags with data.   Let’s see how SMARTY does it. After receiving the request, first determine whether the URL is requested for the first time. If so, "compile" the template file required for the URL into a php (as the current mainstream development language) script, and then redirect ; If not, it means that the template of the URL has been "compiled". You can redirect immediately after checking that no recompilation is required. The recompilation condition can be set to a fixed time limit. The default is that the template file has been modified.
 
How about it? Does it look familiar? Come to think of it──Isn’t this the principle of jsp(SUN’s first choice for enterprise-level applications)! Indeed, it is incredible that this kind of "compilation" is used on an interpreted script engine such as php(as the current mainstream development language), but if you think about it carefully, isn't JAVA also interpreted and executed by the JVM? This is called "nothing is impossible, only imaginable".
 
Since we talked about JAVA, let me express my views on the future of php(as the current mainstream development language). The official website of php (as the current mainstream development language) announced that version php (as the current mainstream development language) version 5.0 will be released at the end of 2003. This version has many new features: such as exception handling, namespaces, more object-oriented, etc. It can be said that it is getting closer to JAVA, and SMARTY is also one of the new features, making php (as the current mainstream development language) more suitable for the development of large and medium-sized projects. But it seems to be getting further and further away from the reason why I chose it in the first place--flexibility and ease of use. But from the perspective of the life cycle of a software, php(as the current mainstream development language) is in the growth stage. Developers give it more functions in the hope that it can be competent for commercial applications, which has more advantages than disadvantages. of. As a loyal user of php(as the current mainstream development language), I definitely don’t want php(as the current mainstream development language) to always be accused of "lack of ability". ?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/509135.htmlTechArticle1. MVC requires templates. MVC was first summarized as a design pattern in the development process of SmallTalk language. MVC Represent "model", "view" and "control" respectively, the purpose is to allow different developers...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template