Basic knowledge of the smarty template engine
This article describes the basic knowledge of the smarty template engine. Share it with you for your reference. The details are as follows:
I. Basic Concepts
1. What is mvc?
Mvc is a development model with core ideas: data input, data processing, and forced separation of data display.
2. What is smarty?
Smarty is a php template engine. More specifically, it can help developers better separate program logic and page display.
3. How smarty works
A template file is a template that displays data. The data to be displayed must be replaced by placeholders.
When smarty is running, it will read the template file, replace the placeholder in the template file with the real data, and output a processed PHP file for the server to run.
2. Write a smarty template by yourself
To better understand the smarty template, you need to write your own smarty template-minismarty, so that you can gain a deeper understanding of the operating principles of smarty.
1. Create a project minismarty
Create a template file path: templates
Path of the compiled template file: templates_c
Create a template file: intro. tpl
Create a new running file: index. php
Create your own smarty, that is, process the template file cls_MiniSmarty.php
2. Compile the index. php file
<? Php require_once './cls_MiniSmarty.php'; $ miniSmarty = new MiniSmarty (); // Transfer Data $ miniSmarty-> assign ("title", "hello minismarty! "); $ MiniSmarty-> assign (" content "," <font color = 'red'> this is content! </Font> "); // to which page the data is transferred, $ miniSmarty-> display (" intro. tpl ");?>
3. Compile the intro. tpl File
<! -- This is a template file -->
The content here is in the form of placeholders. The role of smarty is to replace the content of the placeholder with real data.
In this way, the template file and data file can be forcibly separated, and data can be transmitted through smarty.
4. Compile the cls_MiniSmarty.php file.
<? Php/***** originally provided data to the template through the smarty template engine * Now write a template by yourself and provide the template with a data class * When smarty is running, read the template file, replace the template file with a runable PHP file * the file actually running on the server is the processed file */class MiniSmarty {// template file path var $ template_dir = ". /templates/"; // file path var $ templates_c_dir =" after the template file is replaced ". /templates_c/"; // store the variable value var $ tpl_vars = array (); // simulate two methods./* ** add data * parameter 1: Key * parameter 2: value. The default value is null */function assign ($ tpl_var, $ var = null) {if ($ tpl_var! = '') {$ This-> tpl_vars [$ tpl_var] = $ var; // Add data to the array}/*** display data * parameter 1: in which template file is displayed */function display ($ tpl_file) {// obtain the path of the template file $ tpl_file_path = $ this-> template_dir. $ tpl_file; // obtain the path of the compiled Template File $ compile_file_path = $ this-> templates_c_dir. "com _". $ tpl_file. ". php "; // determine whether the object exists if (! File_exists ($ tpl_file_path) {return false;} // you do not need to generate a compilation file every time, A new compilation file is generated only when the compilation file does not exist or the template file is modified. // It is equivalent to caching the compilation file. // filemtime function: get the file generation time if (! File_exists ($ compile_file_path) | filemtime ($ tpl_file_path)> filemtime ($ compile_file_path) {// read the template file content $ fpl_file_content = contents ($ tpl_file_path ); $ newStr = myReplace ($ fpl_file_content); // generate a new file for the replaced string, that is, the compiled file file_put_contents ($ compile_file_path, $ newStr );} // introduce the compiled file include $ compile_file_path;}/*** to replace the content in the template file and obtain the new string */function myReplace ($ fpl_file_content) {$ pattern = Array ('/\ {\ s * \ $ ([a-zA-Z _] [a-zA-Z0-9 _] *) \ s * \}/I '); $ replace = array ('<? Php echo $ this-> tpl_vars ["$ {1}"]?> '); $ NewStr = preg_replace ($ pattern, $ replace, $ fpl_file_content); return $ newStr ;}}?>
Preg_replace:
Parameter 1: Replacement rule
Parameter 2: replaced with content
Parameter 3: content of the replace operation
5. Running result
The title and content are displayed:
Conclusion:
The actual running file is neither index. php nor intro. php, but the file after the two use smarty:
Com_intro.tpl.php. The data in this file comes from index. php. The layout is from intro. tpl, and the bridge in the middle is smarty.
Smarty accepts data, fills in data (replaces placeholders in the template), and loads the replaced files.
Iii. Explain the Usage Details of smarty
1. How to configure smarty?
Decompress the package, copy the libs folder to the project directory, and create two folders, templates and templates_c, respectively.
2. Precautions for using smarty
① Replace the variable identifier.
Because the default identifier is {}, which conflicts with {} in the style, you need to modify the default identifier to: {<>}
② Modify the identifier.
Method 1: directly modify the source code of the smarty class: not recommended.
Method 2: Use the method provided by smarty for modification.
$smarty->left_delimiter="{<";$smarty->right_delimiter=">}";
③ Some basic configurations of smarty
$ Smarty-> template_dir = ". /templates "; // template path $ smarty-> compile_dir = ". /templates_c "; // compilation path $ smarty-> caching = false; // whether to use the cache $ smarty-> cache_dir = ". /smarty_cache "; // If cache is used: cache path
3. Questions about Variable Allocation in smarty template Technology
One sentence: various data supported by php can be allocated.
Php basic data: int double string bool
Composite data type: array object
Special Data Type: resource null
I hope this article will help you with php programming.