Smarty instance tutorial - template design section -1
Author: teacherlxj Document Type: Posted Visitor Statistics: 239 Source: 9cbs Time: 2004-08-22 08:38:31 Keywords: smarty, PHP template engine First, what is a smarty smarty is written using PHP? Template PHP template engine, which provides the separation of logic and external content, and simple speaking, the purpose is to use PHP programmers to separate, the logical content of the programmer to change the program is not affected by the artist page design. The US re-modify the page does not affect the program logic of the program, which is especially important in multiplayers. Second, Smarty Advantages: 1. Speed: Programs Writing with Smarty can improve the maximum speed, which is relative to other template engine technology. In the middle, no more template recompile (in the case where the source program is not changed) cache technology: Smarty selects a cache technology, which can cache the user's final HTML file into a static HTML page When setting Smarty's Cache property is true, the user's Web request is directly converted to this static HTML file during Smarty, which is equivalent to calling a static HTML file. . Plugin technology: Smarty can customize the plugin. The plugin is actually some custom functions. . The template file is used to use the judgment statement that can be easily reundrained to the template. Third, not suitable for using Smarty: 1. Requires real-time update content. For example, a stock display, it needs to be updated frequently, this type of program uses smarty to slow down the template processing speed. small project. Small projects are quickly and programmers, using Smarty to lose the rapid advantages of PHP development because of the simple projects and programmers and one person. Fourth, install the Smarty class: Installation Smarty's environment: PHP version 4.06 or later. installation Smarty method is very simple, download Smarty.tar.gz from http://samrty.php.net, find the lib directory after decompression, create directory CommM in the current site, and all documents in LIB To copy it into the comm directory, complete the basic installation. Please see the manual in other advanced installation methods. 5. SMARTY uses in the template: This section takes several examples. The Smarty Template is usually used. TPL logo, some people write the extensions directly into .html for the convenience of the United States, and it is possible. This article uses a Smarty standard write: in .tpl to represent a smarty template. Example 1: Let's take a simple example.
================================================================================================================================================================================ ==== ========================================= ============ {* Display is the use of the smarty variable identifier * contains text to comment content * {include file = "header.tpl"} {* page * Hello, my name is {$ name}, welcome everyone to read my smarty learning materials. {**} is a template page comment, it is template on Smarty Do not perform any outputs when parsing, only for templates for annotation. {include file = "xxx.tpl"} Use this sentence to include a template file to the current page, the example in the example will be included in the public.tpl with FOOT.TPL in the website, you can Think, use this sentence to copy all of the contents in XXX.TPL at the current statement. Of course, you can also use this sentence, you can also copy the contents of XXX.TPL to the current statement. {$ name}: Template variable, the core composition in Smarty, uses smarty defined left boundary {and right boundary}, given in the form of PHP variables, will be used in the Smarty program $ smarty -> Assign ("Name", "Li Xiaojun"); replace $ Name in the template into three words of "Li Xiaojun".
The entire example source program is as follows: ===================================================================================================================================================================================================== ==================
====================== =================== index.php ============================================================================================================================================================================ ==========================? PHP / ******************* ************************* * * File Name: Index.php ********** ******************** / incrude_once ("./ Comm / smarty.class.php"); // contains smarty class file $ smarty = new Smarty (); Set the compilation directory ----------------------------------------- ----------- {}, but the actual application is easy to conflict with JavaScript //, but it is recommended to set <{}> or others.
/ ------------------------------------------------------------------------------ ------ Template variable replacement : ================================= Execute index.php ========== =======================
Hello, my name is Li Xiaojun, welcome everyone to read my smarty learning materials. ================================================== exmple2.tpl =============================================================================================================================================================================== ===
The fifty sentence wants to indent the 8 blank alphanumeric, and use "*" to replace these 8 blank characters:
{$ str5 | indent: 8: "*"}}
6. The sixth sentence transforms all of the TEACHERLI@163.com: {$ str6 | lower}
7. The seventh sentence replaces the Teacherli in the variable into: Li Xiaojun: {$ STR7 | Replace: "TEACHERLI": "Li Xiaojun"}
8. The eighth sentence is a combination of variable modifiers: {$ str8 | Capitalize | Cat: "This is a new time:" | DATE_FORMAT: "% y year% Month% d day "| Lower} ===================== =============================================================== ======== ===================== PHP / ******************************* ********************* * * * File name: example2.php * * Make] Master "* email: ******************* onc c.class.php "); // contains the Smarty class file $ smarty = new smarty (); . / / --------------------------------- ------ // left and right boundary, default is {}, but the practical application is easy to conflict with JavaScript //, so it is recommended to set <{}> or other.
/ ------------------------------------------------------------------------------ ------ $ smarty-> left_delimiter = "{smarty-> right_delimiter ="} "; $ smarty-> assign (" str1 "," My name is xiao jun, li "); // replace STR1 to my name is xiao jun, li. $ Smarty-> assign (" str2 "," My name is: "); // Out: My name is: Li Xiaojun $ SMARTY-> Assign ("Str3", "AD"); // Output AD ("SMARTY-> Assign (" STR4 ",") ("Str4", ") (" str4 ","); // The default value is displayed when the fourth sentence is not processed. If you use the previous sentence, it is replaced with "" $ smarty-> assign ("str5", "8 * * *"); // Fifth sentence output: ******** 8 * $ smarty-> assign ("str6", "teacherli@163.com"); // will output teacherli@163.com ("Str7", "this is teacherli"); // Displayed in the template: this is Li Xiaojun ("str8", "str8", "here is combining:"); // Compile and display INDEX.TPL template under ./Templates The final output: =============== ================================================================================================================================================================================== =============================================================================================================================================================================================================