Home  >  Article  >  Backend Development  >  Smarty quick start one_PHP tutorial

Smarty quick start one_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:51705browse

Smarty is a template engine written in PHP and 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. Simply put, the purpose is to separate PHP programmers from front-end personnel, so that programmers change the logical content of the program without affecting the page design of the front-end personnel, and front-end personnel re-modify the page without affecting the program logic of the program. This is particularly important in projects involving multi-person collaboration.

Smarty’s advantages:


1. Speed: Programs written using Smarty can achieve maximum speed improvements, which is compared to other template engine technologies.

2. Compiled type: A program written in Smarty must 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. file without recompiling the template (when the source program has not been changed)

3. Caching technology: A caching technology selected by Smarty. It can cache the HTML file that the user finally sees into a static HTML page. When the cache attribute of Smarty is set to true, in the Smarty setting During the cachetime period, the user's WEB request is directly converted into 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. If/elseif/else/endif can be used in templates. Using judgment statements in template files can very conveniently reformat the template.

Smarty apps:

1. Download the latest smarty.

2. Download the smarty kernel folder libs and put it into the php website folder. (For safety reasons, you can change the folder name yourself, such as changing it to smarty)

3. Create four folders: templates, templates_c, configs, and cache in the website directory.

4. Write templates in the templates/ directory and create index.htm with the content:







{$world}

OK, let’s test it. PHP calls smarty and writes the following program:

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

That’s right! Configuration successful~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478800.htmlTechArticleSmarty is a template engine written in PHP and 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...
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