Basic knowledge of the smarty template engine, basic knowledge of the smarty template _ PHP Tutorial

Source: Internet
Author: User
Tags php template
Basic knowledge of the smarty template engine, basic knowledge of the smarty template. 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. Details are as follows: 1. Getting started with basic knowledge of the smarty template engine and 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 "," this is content! "); // To which page the data is transferred, $ miniSmarty-> display (" intro. tpl ");?>

3. Compile the intro. tpl file

    
 
 
 {$ Title}{$ Content}

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.

The example in this article describes the basic knowledge of the smarty template engine. Share it with you for your reference. Details: 1. Basic overview...

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.