Home  >  Article  >  Backend Development  >  Smarty-1 (18)

Smarty-1 (18)

WBOY
WBOYOriginal
2016-08-08 09:25:03938browse

smarty

1. Template principle

1. Why use templates

1) Scene:

for($i=0;$i<$count;$i++){

echo ‘’.$row[‘proname’].’

}

The above writing method mixes HTML and PHP, which is not conducive to development and maintenance

2) Scene:

We have now developed a website. After a period of operation, the customer was dissatisfied with the front desk, so we developed another interface. However, after running it, the customer thought the previous style was better and often changed the website style

The best way to solve the above two problems is to use templates

2. Specific implementation of template

The implementation of template is actually string replacement

1) Example 1

Template file:

PHP file:

1) Example 2:

Template class:

PHP code:

The code in the above class file is not optimized enough. We can use the more popular template framework to implement our own projects

1. Introduction to Smarty

1. What is Smarty

Smarty is a PHP template engine developed based on PHP. It provides separation of logic from external content.

2. Features of Smarty

l speed

l Compiled type

l Caching technology

l Plug-in technology

l Free statement if/elseif/else/endif

l Real-time update: Real-time requirements are relatively high

3. Configuration

l http://smarty.php.net

l /lib

l Smarty 2.6 php4.0

l Smarty 3.0 php5.0

1 Unzip smarty and enter the unzipped directory

Demo: Case

Libs: Smarty source code

1) Enter the libs directory,

Plugins: Custom plug-in directory

Sysplugins: System plug-in directory

Debug.tpl: In Smarty, there is debugging function, you can observe the value of variables, debugging window template

Smarty.class.php: Smarty core class file (entry file)

SmartyBC.class.php: Smarty3.0 has been upgraded on the basis of 2.0. If you want to use the functions of 2.0, you can reference this entry file

3) Copy the libs directory to the project and rename it

The above introduces Smarty-1 (18), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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