Smarty, smarty tutorial

Source: Internet
Author: User
Tags php template

Smarty, smarty tutorial

Brief description:

Smarty is a template engine written in PHP and one of the most famous PHP template engines in the industry. It separates the logic code from the external content and provides an easy-to-manage and use method to combine the originally HTML code with the PHP code logic separation.

Simply put, the purpose is to separate PHP programmers from front-end personnel so that programmers can change the logic content of the program without affecting the front-end personnel's page design, the re-modification of the page by the front-end personnel does not affect the program logic, which is particularly important in projects with multiple partners.

Advantages:

1. Speed: Programs Written with Smarty can achieve the maximum speed improvement, which is relative to other template engine technologies.

2. compilation type: a program written in Smarty must be compiled into a non-template PHP file during runtime. This file adopts a mix of PHP and HTML, during the next template access, the WEB request is directly converted to this file, instead of re-compiling the template (without modifying the source program)

3. cache Technology: A cache technology used by Smarty to cache the HTML file that the user sees as a static HTML page. When the cache attribute of Smarty is set to true, during the cachetime period set by Smarty, user WEB requests are directly converted to this static HTML file, which is equivalent to calling a static HTML file.

4. Plug-in technology: Smarty can customize plug-ins. Plug-ins are actually some custom functions.

5. You can use if/elseif/else/endif in the template. Using judgment statements in the template file can easily rearrange the template format.

Not suitable for Smarty:

1. content to be updated in real time. For example, for a stock display, it needs to update data frequently. Using smarty for this type of program slows down the processing speed of the template.

2. small projects. Small projects because the project is simple and the artist and programmer are both one-person projects, using Smarty will lose the advantage of rapid PHP development to a certain extent.

(PS: However, for the sake of project specifications, please try to use the template engine. As a matter of fact, the development speed will be faster. The above are problems that developers who are not used to the template engine encounter)

The program design section of smarty:

<? Php shortde_once ("./Smarty. class. php"); // contains the smarty class file
$ Smarty = new Smarty (); // create a smarty Instance Object $ smarty-> templates (". /templates "); // set the template directory $ smarty-> templates_c (". /templates_c "); // set the compilation directory $ smarty-> cache (". /cache "); // cache directory $ smarty-> cache_lifetime = 0; // cache time $ smarty-> caching = true; // cache method $ smarty-> left_delimiter = "{#"; $ smarty-> right_delimiter = "#}"; $ smarty-> assign ("name ", "zaocha"); // Replace the template variable with $ smarty-> display ("index.htm"); // compile and display. /template What is the s's index.htm template?>

 

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.