Smarty template engine (I) basic knowledge, smarty Template

Source: Internet
Author: User
Tags php template smarty template

Smarty template engine (I) basic knowledge, smarty Template
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. The smarty operating principle template file is a template for displaying 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. In order to better understand the smarty template, write your own smarty template-minismarty, so that you can gain a deeper understanding of the working principle 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.php2. Compile the index. php file.

<? Phprequire_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 running PHP file. The files actually run on the server are processed files. */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 method Description: parameter 1: Replacement rule
Parameter 2: replaced with content
Parameter 3: content of the replace operation 5. The running result 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. explains how to use 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. Notes 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. The smarty template technology issues Variable Allocation details in 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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.