Introduction of Smarty Template engine
What is Smarty?
Smarty is a template engine written in PHP, designed to separate the PHP code from the HTML code, so that the PHP programmer only focus on the writing of PHP code, Web art only focus on the beautification of the Web page
Having problems with the template engine?
PHP code in HTML is getting bigger and heavier
The number of pages is getting more and more
The division of programming and art design is not clear
Traditional PHP Files:
1. PHP code together with HTML, not conducive to the division of work between programmers and artists, programmers and artists can not modify a file at the same time
2. Data transfer on the same page, PHP data in the page display needs to use PHP tags
For example: <?php echo $username?>
index.php
After using the template engine:
The 1.php code is separated from the HTML code, and the PHP code is placed in a separate PHP file, and the HTML code is placed in a separate HTML file.
2. Data transfer first the data in PHP is given to the template engine, and then the template engine then displays the received data to the template file.
3. There is no PHP code or PHP tags in the template file, the data displayed in the template is displayed through the template engine's markup.
index.php smarty Template engine index.html
PHP is currently a lot of template engine, why use the Smarty template engine?
1. The general template engine, such as, Phplib are in the establishment of template objects to parse the template, and then put the variable into the template, and then through the parse () this method to parse the model, and finally the page output, summed up into a sentence, the sentence is to be re-parsed every time the execution
2. For the Smarty template engine, do not have to do in the program to do the Parse (), Smarty will automatically help us do, and has compiled the Web page, if there is no change in the case, Smarty will skip the recompilation of the action and go directly to execute the compiled page, To save compilation time.
Other advantages of Smarty:
1. Fast speed: Relative to other template engines
2. Compiled: A program written in Smarty is compiled into a non-template PHP file at run time. This file is a mix of PHP and HTML, the next time you access the template, the Web request directly to the file, without recompiling (in the case of template files without changes)
3. Caching technology: It can cache the HTML page that the user eventually sees as a static HTML page. When the user opens the cache, the cache time can be set, before the cache file expires, the user's request directly to the static cache file, the equivalent of a call to a static HTML file.
4. The Process Control statement can be used in the template.
Not suitable for use in smarty places
1. The content that needs to be updated in real time, for example: stock display, it needs to update the data frequently
2. Small project.
Smarty template engine operation flow.
① initializing the Smarty template engine while the PHP program is running
②smarty template engine reads template files (. html or TPL files)
③ when reading a template file, the Smarty template engine automatically determines if the template file needs to be recompiled.
④ If no recompilation is required, the template variable is replaced directly.
⑤ If this is the first time you have executed this program file or the template has been changed, then you need to recompile and then replace the template variable.
The ⑥ finally executes the compiled PHP file.
⑦ output results to the browser.
Second, Smarty template engine Small instance
Go to the official website to download the Smarty Template engine compression pack
Official website: http://www.smarty.net
Unzip this package, discover that it has a Libs folder under it, rename the Libs folder to Smarty, and move to the Libs folder under the Web site root directory
Open the Smarty Engine Libs folder with the following files:
Sysplugins system plug-in directory
Plugins External Plugins
DEBUG.TPL output debug Debug information Output template
Smarty.class.php Smarty template Engine core class file, to use the Smarty template engine, first introduce the file
SmartyBC.class.php
Example of building a smarty template:
1. Copy the Libs Library directory.
2. Create a template directory templates template file directory
Create a configs configuration file directory
3. Write index.php file (entry file) to do smarty import and create
1. Import the Smarty class
Require ("libs/smarty.class.php");
2. Create an Object
$smarty = new Smarty ();
3. Initializing information
$smarty->left_delimiter= "{";//redefine the left delimiter of the smarty template
$smarty->right_delimiter= "}";//redefine the right delimiter of the Smarty template
Static caching
$smarty->caching=true; Whether to turn on static cache True (ON)
$smarty->cache_lifetime=5; Set cache time (5 for cache 5 seconds)
4. Place the variable:
$smarty->assign ("name", "Zhangsan");//place variable in smarty template name value is Zhang San
$smarty->assign ("date", Date ("y-m-d h:i:s"));//Place a time for the template
5. Load the template:
$smarty->display ("index.html");
4. Using the browser to access index.php, the directory Templates_c and the cache directory will be created
5. The final structure:
root directory
|--libs//smarty Library
| |--smarty.class.php
|--templates//Template directory
| |--index.html
|--templates_c//Template compilation Directory
|--cache//template static cache directory (note the need to turn on caching)
|--configs//configuration file directory
|--index.php//php file (entrance)
Simple analysis of the Smarty.class.php core class file
It is a class file: A class is composed of attributes (variables) and methods
Name of the class: Smarty
Main attribute or variable:
$template _dir: Template catalog, mainly used to store template files, such as:. html files,. tpl files
$compile _dir: Compile the directory, mainly used to store the compiled PHP file, that is, a mixed-mode file
$config _dir: Configuration directory, mainly used to store public configuration files
$cache _dir: Cache directory, mainly used to store cache files
$left _delimiter: Left Border character
$right _delimiter: Right boundary character
$caching whether to turn on caching
$cache _lifetime Custom Cache time
Main methods:
Assign (): used primarily to assign a scalar type of data in PHP to a template variable.
Display (): Mainly used for displaying the specified template file
Before using Smarty, the core class file (Smarty.class.php) needs to be configured first
1. To introduce the core class file first
Include_once "libs/smarty/smarty.class.php";
2. Create a core class instance object using the New keyword
$smarty =new smarty ();
3. Configuration
$smarty->template_dir= "./templates"; Set up a template catalog
$smarty->compile_dir= "./templates_c"; Set compilation directory
$smarty->config_dir= "./configs"; Set the Public profile directory
$smarty->caching=false; Set the cache, which is not normally turned on during project debugging
$smarty->cache_dir= "./cache"; Set the cache directory
/* 1. Set the left and right boundary characters, the default left-to border is a pair of curly braces, the actual application generally do not use the default boundary character, because it is easy to conflict with the definition of functions in JavaScript
2. In the template template, the template variables should be placed in the left and right boundary
*/
$smarty->left_delimiter= "{";
$smarty->right_delimiter= "}";
After the configuration is complete, do the following
4. Assign the value of the type of PHP bid to the template variable
$smarty->assign ("username", $username);
5. Display the corresponding template file, according to $smarty->template_dir= "./templates" Go to the Templates folder under the current directory to find index.html file
$smarty->display ("index.html");
As configured above, we need to make sure that the current folder has the following directory, if not, manually set up
Templates//Store template files
Templates_c//Store compilation Directory
Configs//Store Public Profile
Cache//Store cached files
1. The file deployment for the entire project at this time:
root directory
|--libs
| |--smarty.class.php
|--templates
| |--a.html
|--templates_c
|--cache
|--configs
|--init.inc.php
|--a.php
|
2. Relative path: The introduction of external files or setting up directories in program files is based on this file
For example: to introduce the Smarty.class.php file in the index.php file because the Smarty.class.php file is located under the Libs/smarty folder, and the index.php file is similar to the Libs folder, this should be introduced:
Include_once "libs/smarty/smarty.class.php";
Smarty Template engine Technology (i)