Learn about the PHP template engine smarty_php Tutorial

Source: Internet
Author: User
Tags php template php website
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".

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.

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.

What is a php template

How do I make my PHP scripts stand out from the design? This is undoubtedly one of the most frequently asked questions on the PHP mailing list. Although PHP was advertised as an "HTML embedded language", after writing a lot of PHP and HTML-mixed projects, I had an idea of separating the forms and content. Moreover, in many companies the role of the planning designer is separate from the programmer. So, a template solution like this produces ...

For example, in a company, the development process for an application is as follows: After submitting the plan document, the interface designer [artist] makes the appearance model of the website and then gives it to the background programmer. Programmers use PHP to implement business logic while using a façade model to create a basic architecture. Then the project is returned to the HTML page as the designer continues to refine. In this way, the project may go back and forth several times between the background programmer and the page designer. Because background programmers do not like to interfere with any of the HTML tags, but also do not need to mix the artists and PHP code, the designer only need to configure files, dynamic chunks and other interface parts, do not have to touch the intricacies of the 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 of them simply provide a basic way to replace variables with templates and to format the dynamic chunks 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 background colors between dynamic chunks, he may have to speak with the programmer beforehand. Also, artists should have their own configuration files for page design, which can also be used to pull them into the template by using variables.

What is Smarty

Smarty is one of the most famous PHP template engines in the industry, which is a PHP template engine written using PHP. It separates the logic code from the external content, provides an easy-to-manage and useful way to separate the PHP code that was originally mixed with HTML code. Simply speaking, the purpose is to use the PHP programmer with the Art of separation, the use of programmers to change the logic of the program will not affect the design of the page, the art of re-modifying the page will not affect the program logic, which in the multi-person cooperation project is particularly important.

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.

Smarty Advantages

    • Speed: The program written in Smarty can get the maximum speed improvement, which is relative to other template engine technology.
    • Compiled: A program written in Smarty is compiled into a non-template PHP file at runtime, which uses a mix of PHP and HTML to convert the Web request directly into the file the next time the template is accessed. The template is no longer recompiled (in the case where the source program has not changed).
    • Caching technology: Smarty chooses a caching technology that caches the HTML files the user eventually sees as a static HTML page, and when the cache property of the Smarty is set to True, The user's Web request is converted directly to this static HTML file during the Cachetime period set by Smarty, which is equivalent to calling a static HTML file.
    • Plug-in technology: Smarty can customize plugins. Plugins are actually some of the custom functions.
    • If/elseif/else/endif can be used in templates. The template file can be easily reformatted by using a judgment statement.

Not suitable for use in smarty places

Content that needs to be updated in real time. For example, like a stock display, it needs to update the data frequently, and this type of program uses Smarty to slow down the processing of the template.

Small project. Small project because the project is simple and the artist and programmer are in one person's project, using Smarty will lose the advantage of rapid development of PHP.

http://www.bkjia.com/PHPjc/752420.html www.bkjia.com true http://www.bkjia.com/PHPjc/752420.html techarticle 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 realization are quite ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.