Smarty template technology, Smarty template _php Tutorial

Source: Internet
Author: User
Tags php template smarty template

Smarty template technology, Smarty template


First, what is smarty?
Smarty is a template php template engine written in php , which provides the separation of logic and external content, simply speaking, the purpose is to use PHP programmers separated from the art , The use of programmers to change the logic of the program will not affect the design of the page, the artist re-modify the page does not affect the program logic, which is particularly important in the multi-person cooperation project.

Second,smarty Advantages:
1. speed: The program written in Smarty can get the maximum speed improvement, which is relative to other template engine technology.

2. compiled: A program written in Smarty is compiled into a PHP file that is not a template technology at runtime , this file uses the the way PHP blends with HTML , the next time the template is accessed , the Web request is translated directly into the file, Instead of recompiling the template (in case the source program has not changed)

3. Caching technology:smarty A cache technology, which can cache the HTML file that the user eventually sees as a static HTML page, when setting the smarty Cache property to true , the smarty Set the Cachetime during the period, the user's Web The request is converted directly to this static HTML file, which is equivalent to calling a static HTML file.

4. plug-in technology:smarty can customize the plugin. Plugins are actually some of the custom functions.

5. If/elseif/else/endif can be used in the template . The template file can be easily reformatted by using a judgment statement.

Third, not suitable for the use of Smarty places:

1. content that needs to be updated in real time. For example, like a stock display, it needs to update the data frequently, and this type of program uses smarty to slow down the processing of the template.

2. Small Project. Small project because the project is simple and the artist and programmer are in one person's project, using smarty will lose The advantage of rapid development of PHP.

Four, Smarty installation and configuration:

After downloading the Smarty installation package, place the Libs folder in the root directory of the Web site and create a few folders
Templates storing template files

Templates_c storing the compiled file

Configs storing the configuration file

Cache files Stored
Then create the initialization file smarty.init.php

 
    PHP
 include"./libs/smarty.class.php";//contains the file where the Smarty class library resides    $smarty=NewSmarty ();//creates an object of the Smarty class $smarty    $smarty->template_dir= "./templates/";//set all template files to store directory    $smarty->compile_dir= "./templates_c/";//set up all compiled template files to store directory    $smarty->config_dir= "./configs/";//set the directory where special profiles are stored in the template    $smarty->cache_dir= "./cache/";//set the directory where the Smarty cache files are stored    $smarty->caching=1;//set the Enable Smarty Cache template feature    $smarty->cache_lifetime=60*60*24;//setting the template cache validity time is 1 days    $smarty->left_delimiter= ' <{';//set the left terminator in the template language    $smarty->right_delimiter= '}> ';//set the right terminator in the template language?>

Five, Smarty engine operating mechanism:

1, the new template file Index.tpl and the required configuration files in the project my.conf, configuration files can be later supplemented

   <   {config_load   file   = "... /configs/my.conf " }  ;  !--  load profile  -->   <   HTML     ;   <   Head         ;   <   Meta   CharSet   = "Utf-8"         ;   <   title   ><   {$title  }   >!--  title     ;  !--  Head     ;   <   Body   bgcolor   = "<{#bgColor #}>"         ;   <   {$content  }     ;  !--  Body  ;  !--  HTML  ;       

2, the new program entry file index.php, the introduction of controller files, transfer values and assign variables, and display template Index.tpl

  
    php    // Introducing smarty.init.php    include ' smarty.init.php ';     $smarty->assign ("title", "My first file title");     $smarty->assign ("content", "My first file contents");     $smarty->display ("Index.tpl");? >

3, run index.php, through the Smarty controller file will generate a PHP format of the compilation file, when the caching mechanism is not open, the browser will read the compilation file and finally display it. When the caching mechanism is turned on, the Smarty controller generates a static HTML page, that is, the cache file Com_index.tpl, so that the browser reads higher performance. Detailed caching procedures are described in Smarty Tutorial 9th

http://www.bkjia.com/PHPjc/1096613.html www.bkjia.com true http://www.bkjia.com/PHPjc/1096613.html techarticle smarty Template Technology, Smarty template One, what is Smarty? Smarty is a template php template engine written in PHP, which provides the separation of logic and external content, simply speaking, ...

  • Related Article

    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.