Smarty template technology, smarty Template

Source: Internet
Author: User
Tags php template

Smarty template technology, smarty Template

1. What is smarty?
Smarty is a php template engine written using php. It provides the separation of logic and external content. In short, it aims to separate php programmers from the artist, changing the program's logic does not affect the page design of the artist. Modifying the page does not affect the program's program logic, which is particularly important in projects that involve multiple partners.

Ii. Advantages of smarty:
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.

3. areas not suitable for using 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 have one project at the same time, using smarty will lose the advantage of rapid php development.

Iv. smarty installation and configuration:

Download the smarty installation package, decompress it, put the libs folder under the root directory of the website, and then create Several folders
Templates

Templates_c stores compiled files

Configs stores configuration files

Cache stores cache files
Create the initialization file smarty. init. php.

<? Php include ". /libs/Smarty. class. php "; // file containing the Smarty class library $ smarty = new Smarty (); // create a Smarty class Object $ smarty-> template_dir = ". /templates/"; // set the directory for storing all template files $ smarty-> compile_dir = ". /templates_c/"; // set the directory for storing all compiled template files $ smarty-> config_dir = ". /configs/"; // set the directory where special configuration files are stored in the template $ smarty-> cache_dir = ". /cache/"; // set the directory for storing the Smarty cache file $ smarty-> caching = 1; // set to enable the Smarty cache template function $ smarty-> cache_lifetime = 60*60*24; // set the template cache validity period to 1 day $ s Marty-> left_delimiter = '<{'; // set the left Terminator $ smarty-> right_delimiter = '}>' in the template language '; // set the right terminator in the template language?>

V. smarty Engine Running Mechanism:

1. Create the index. tpl template file and the configuration file my. conf required by the project. The configuration file can also be supplemented later.

<{Config_load file = "../configs/my. conf"}> <! -- Load the configuration file --> 

2. Create the program entry file index. php, introduce the Controller file, pass the value, assign the variable, and display the template index. tpl.

<? Php // introduce smarty. init. php include 'smarty. init. php '; $ smarty-> assign ("title", "My first file title"); $ smarty-> assign ("content ", "My first file content"); $ smarty-> display ("index. tpl ");?>

3. Run index. php. a php compilation file is generated through the smarty controller file. When the cache mechanism is not enabled, the browser will read the compilation file and display it. When the cache mechanism is enabled, the smarty controller generates a static HTML page, that is, the cache file com_index.tpl, which improves the browser reading performance. For details about the cache process, refer to smarty's detailed tutorial 9th.

 

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.