Smarty Template Engine (i) basic knowledge

Source: Internet
Author: User
Tags php template

first, the basic concept1. What is MVC? MVC is a development model, the core idea is: data input, data processing, data display of the forced separation.
2. What is Smarty? smarty is a php template engine. More clearly, it can help developers better separate program logic and page display.
3.smarty Execution Principle    a template file, which is a template that displays data, is replaced with a placeholder for the data that needs to be displayed.
    when Smarty executes, it reads the template file, replaces the placeholder in the template file with the actual data, and outputs a processed PHP file to be executed by the server.

Two, write a smarty template    in order to better understand the smarty template, now write their own smarty template-minismarty, let oneself more in-depth understanding of smarty implementation principle. 1. New Project MinismartyNew Template file path: Templates
file path After the new template file is compiled: Templates_c
New template file: Intro.tpl
newly executed file: index.php
Create your own smarty, which is the file that processes the template: cls_minismarty.php2. Writing the index.php file
<?phprequire_once './cls_minismarty.php '; $miniSmarty = new Minismarty ();//Pass Data $minismarty->assign ("title", " Hello minismarty! "); $miniSmarty->assign ("Content", "<font color= ' Red ' >this is content!</font>");//Pass data to which page display $ Minismarty->display ("Intro.tpl");? >
3. Writing the Intro.tpl file
<!--This is a template file-->
The content of this is in the form of placeholders, where the smarty is to replace the contents of the placeholder with real data.
This enables the separation of template files and data files and the transfer of data through Smarty.
4. Writingcls_minismarty.php File
<?php/** * * Originally used to provide data to the template through the Smarty template engine * Now I imitate write a template, give the template to provide data class * Smarty Execute, read the template file, replace the template file with the executable php file. The file that the server really executes is the processed file. */class Minismarty {//template file path var $template _dir = "./templates/";//template file is replaced with file path var $templates _c_dir = "./templates_c/" ;//store variable value var $tpl _vars = Array ();//Main simulation 2 methods/** * Add data * 1: Key * Parameter 2: value, default feel null */function assign ($tpl _var, $var = null) {if ( $TPL _var! = ") {$this->tpl_vars[$tpl _var] = $var;//Add data to the array}}/** * Show data * Parameters 1: Show to which template file */function display ($tpl _fil e) {//Get the path to the template file $tpl_file_path = $this->template_dir. $TPL _file;//Get the file path after the template file is compiled $compile_file_path = $this Templates_c_dir. "Com_". $tpl _file. ". PHP";//infer if the file exists if (!file_exists ($tpl _file_path)) {return false;} Instead of generating the compiled file every time, only the compiled file does not exist or the template file is altered to generate a new compilation file//equivalent to cache the compiled file//filemtime function: Get the file generation time if (!file_exists ($compile _file_ Path) | | Filemtime ($tpl _file_path) > Filemtime ($compile _file_path)) {//Read the contents of the template file $fpl_file_content = file_get_contents ($ Tpl_file_path); $newStr = Myreplace ($FPL _file_content);//The replacement string is borninto a new file, which is the compiled file file_put_contents ($compile _file_path, $newStr);} Introduction of compiled files include $compile _file_path;} /** * Replace the contents of the template file to obtain a 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:Number 1: Rules for substitution
number 2: Replace with content
Number 3: Replace the contents of the Operation5. Implementation results    The title and the contents are displayed.

Conclusion:The files that are actually executed are neither index.php nor intro.php, but the files after they have been smarty function:
com_intro.tpl.php. The data in this file comes from index.php, which shows the layout from Intro.tpl, and the bridge in the middle is smarty. The role of Smarty is to accept data, populate data (replace placeholders in templates), and load the replaced file.
third, explain smarty use details1. How do I configure Smarty?after extracting, copy the Libs directory to the project directory and then create 2 directories templates and Templates_c, respectively, to put the template files and template compiled files.

2. Precautions for using Smarty① The identifier of the replacement variable.since the default identifier is {} This conflict with {} In style style, it is necessary to change the default identifiers, the general change is: {<>}
② method for changing identifiers.method One: Directly change the Smarty class source code: not recommended.
method Two: Use the method provided by Smarty to make changes. $smarty->left_delimiter="{<"; $smarty->right_delimiter=";}";Some basic configuration of ③smarty    $smarty->template_dir="./templates"; Template path $smarty->compile_dir="./templates_c"; //compile path $smarty->caching=false; //Whether to use cache $smarty->cache_dir="./smarty_cache"; //Assuming cache is used: Cached path

Details of allocation variables for 3.smarty template technologyBottom line : The ability to allocate various data supported by PHP.
PHP basic data: int double string bool
composite data type: Array Object
Special Data type: Resource null




Smarty Template Engine (i) basic knowledge

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.