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 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.
Smarty applications:
1. download the latest smarty.
2. Download the smarty kernel folder libs and put it in the php website folder. (For security, you can change the folder name, for example, smarty)
3. Create Four folders, templates, templates_c, configs, and cache, respectively under the website directory.
4. Write a template in the templates/directory and set index.htm:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
{$ World}
</Body>
</Html>
OK. Let's test it. php calls smarty and write the following program:
<? Php
Include (smarty/Smarty. class. php );
// Program directory
Const DIR_SEP = DIRECTORY_SEPARATOR;
Define (SITE_ROOT, dirname (_ FILE _). DIR_SEP );
$ Smarty = new Smarty;
$ Smarty-> template_dir = SITE_ROOT.templates.DIR_SEP;
$ Smarty-> complie_dir = SITE_ROOT.templates_c.DIR_SEP;
$ Smarty-> config_dir = SITE_ROOT.configs.DIR_SEP;
$ Smarty-> cache_dir = SITE_ROOT.cache.DIR_SEP;
$ Smarty-> assign (world, hello world !);
$ Smarty-> display(index.htm );
?>
Output result: hello world
Thats right! Configuration successful ~