Home  >  Article  >  Backend Development  >  Learn about PHP template engine Smarty_PHP tutorial

Learn about PHP template engine Smarty_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:44697browse

There are a variety of template engines to choose from for the logic layer and presentation layer of the MVC development model using PHP, but after the birth of the official engine SMARTY, the choice has changed. Its concept and implementation are quite "avant-garde".

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

When CGI became popular in China in 1996, early WEB programmers were all self-taught from HTML. It was not difficult to print lines of HTML in PERL. However, as the Internet speeded up step by step, The page size has also increased tenfold from the original 20 to 30 K. Writing CGI programs creates an urgent requirement: separating PERL and HTML source code. Thus, 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 need to use an agreed "language" to communicate during cooperation.

This language is not our native language or English. The term is called "template", and the logic and representation depend on it. It is an expression method that combines the characteristics of HTML and scripting languages. 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 under the Windows platform, you will definitely be familiar with the encapsulation of Document/Document Template/View. This is a very typical MVC example. For Web applications, I personally think that EJB/servlets/JSP in J2EE are the most powerful, and of course there are simple and beautiful Structs. Another well-known implementation is COM/DCOM+ASP. This combination is used by the most people in our country.

What is PHP template

How can I make my PHP script independent from the design? This is undoubtedly one of the most asked questions on the PHP mailing list. Although PHP is billed as an "HTML embedded language", after writing many mixed PHP and HTML projects, I came up with the idea of ​​​​separating forms and content. Furthermore, in many companies the role of the planner is separate from that of the programmer. So, such a template solution was born...

For example, in a company, the development process of an application is as follows: After submitting the plan document, the interface designer [artist] makes the appearance model of the website and then hands it to the back-end programmer. Programmers use PHP to implement business logic and use appearance models to create the basic architecture. The project is then returned to the HTML page designer for further refinement. In this way, the project may go back and forth between the backend programmer and the page designer several times. Since back-end programmers don't like to interfere with any HTML tags, there is no need for artists to mix with PHP code; art designers only need configuration files, dynamic blocks and other interface parts, and do not need to touch the intricate PHP code. . Therefore, it is important to have a good template support at this time.

Looking at the many PHP template solutions that exist today (such as PHPLIB), most only provide basic methods of replacing variables with templates and formatting dynamic blocks with limited functionality. But our needs are much higher than this. We don't want PHP programmers to design HTML pages at all, but this is inevitable. For example, if the artist wants to alternate different background colors between dynamic blocks, he may have to agree with the programmer in advance. Similarly, artists should also have their own configuration files for page design, which can also be pulled into the template through variables.

What is smarty

Smarty is a template PHP template engine written in PHP. It is currently one of the most famous PHP template engines in the industry. It separates logical code and external content, providing an easy-to-manage and use method to logically separate PHP code that is originally mixed with HTML code. To put it simply, the purpose is to separate PHP programmers from artists. When programmers change the logical content of the program, it will not affect the page design of the artist. When the artist re-modifies the page, it will not affect the program logic of the program. This is a problem in multi-person applications. It is particularly important in cooperative projects.

Why choose smarty

For PHP, there are many template engines to choose from, such as the earliest PHPLIB template and the rising star Fast template. After several upgrades, they have become quite mature and stable. 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.

Except for personal preference, I have always tended to use official standard implementations, such as APACHE's XML 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 PHPLIB template and Fast template. From the syntax of the template to the processing of the template, the template is read into the memory and then the parse() function is called 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 script, and then redirect; if not, it means that the template of the URL has been "compiled" After passing the check, you can redirect immediately after checking that recompilation is not required. The recompile condition can be set to a fixed time limit. The default is that the template file is modified.

How about it? Does it look familiar? Come to think of it──Isn’t this the principle of JSP! Indeed, this kind of "compilation" seems incredible when used on an interpreted scripting engine like PHP, 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".

Now that we have talked about JAVA, let me express my views on the future of PHP. The official PHP website announced that version PHP 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 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 is in the growth stage, and the pros outweigh the cons by developers giving it more functions in the hope that it can be competent for commercial applications. As a loyal user of PHP, you certainly don't want PHP to always be accused of "insufficient capabilities", right?

Why choose SMARTY, just because it is very similar to JSP? There are certainly better reasons. First of all, in addition to the relatively high cost of the first compilation, as long as the template file is not modified, the compiled cache script is available at any time, saving a lot of parse() time; secondly, SMARTY has a rich function library like PHP. From counting words to automatic indentation, text wrapping and regular expressions, you can use it directly; if you feel it is not enough, for example, you need the function of paging display of the data result set, SMARTY also has strong expansion capabilities, which can be expanded through plug-ins.

Advantages of smarty

  • Speed: Programs written using smarty can achieve maximum speed improvements, which is compared to other template engine technologies.
  • Compiled type: A program written in smarty will be compiled into a non-template technology PHP file at runtime. This file uses a mixture of PHP and HTML. The WEB request will be directly converted to this file the next time the template is accessed. , instead of recompiling the template (when the source program has not been changed).
  • Caching technology: A caching technology selected by smarty. It can cache the HTML file finally seen by the user into a static HTML page. When the cache attribute of smarty is set to true, during the cachetime set by smarty The user's WEB request is directly converted into this static HTML file, which is equivalent to calling a static HTML file.
  • Plug-in technology: smarty can customize plug-ins. Plug-ins are actually some custom functions.
  • If/elseif/else/endif can be used in templates. Using judgment statements in template files can very conveniently reformat the template.

Places not suitable for smarty

Content that needs to be updated in real time. For example, like stock display, which needs to update data frequently, using smarty for this type of program will slow down template processing.

Small project. For small projects where the artist and programmer are both involved because the project is simple, using smarty will lose the advantage of rapid PHP development.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752420.htmlTechArticleUsing PHP to implement the logic layer and presentation layer of the MVC development model. There are a variety of template engines to choose from, but the official engine After the birth of SMARTY, the choices have changed. Its concept and implementation are quite...
Statement:
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