Smarty, PHP template engine from: www.phpe.net, what is smarty? Smarty is a template PHP template engine written by PHP, which provides the separation of logic and external content, simple speaking, the purpose is to use The PHP programmer is separated by the US, the logical content of the programmer who uses the program will not affect the artist page design. The art re-modification page will not affect the program logic of the program, which is especially important in multiplayer. . Second, Smarty Advantages: 1. Speed: Programs Writing with Smarty can improve the maximum speed, which is relative to other template engine technology. 2. Compiling: The program written by Smarty is compiled into a non-template-based PHP file. This file uses PHP and HTML mixed mode. When the next access template, the web request is directly converted to this file, No longer performing template recompilation (in the case where the source program is not changed) 3. Cache technology: Smarty selected a cache technology, which can cache the user's final HTML file into a static HTML page, When Smarty's Cache property is True, convert the user's web request directly to this static HTML file during the Cachetime period set by Smarty, which is equivalent to calling a static HTML file. 4. Plugin Technology: Smarty can customize the plugin. The plugin is actually some custom functions. 5. IF / ELSEIF / ELSE / ENDIF can be used in the template. 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. 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. 2. 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: Install the environment: PHP version 4.06 or later. Installing the smarty method is very simple, download smarty.tar.gz from http://samrty.php.net, decompress the lib directory, create directory CommM in the current site, copy all files in the libm directory, complete the basics Installation. Other advanced installation methods Please see the manual. Five, smarty uses in the template: This section takes a few instances to use Smarty. 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 first look at a simple example.
============================================================================================================================================================================================================= === Index.tpl ================================================= ========= {* Display is the use of the smarty variable identifier * contains text to comment content *} {include file = "header.tpl"} {* page *} Hello, my name {$ name}, welcome everyone to read my smarty learning materials. {include file = "foot.tpl"} The example on the top of the page *} is a TPL template, where: 1. {**} is a comment for the template page, which is not paid when SMARTY parsed the template Output, only template designers are annotated. 2. {include file = "xxx.tpl"} Use this sentence to include a template file to the current page, the example in the example will contain the Head.tpl and FOOT.TPL of the public thing 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 copy the contents of XXX.TPL to the current statement. 3. {$ Name}: Template variable, core composition in Smarty, using the left boundary {with the right boundary} in smarty, is given in the form of a PHP variable, where $ smarty-> assign will be used in the Smarty program ("Name", "Li Xiaojun"); replacing the $ Name in the template into three words of "Li Xiaojun".
The entire example source program is as follows: ================================================ =================
{include file = "foot.tpl"} {* page tail *} ================================== ============== Index.php ================================= =============== php / ************************************** **************** file name: index.php * effect: an example routine ** author: big Brother * Email: teacherli@163.com******** ********************************************************* / include_once ("./ Comm / smarty.class. PHP "); // contains smarty file $ smarty = new smarty (); // Established Smarty instance object $ smarty $ smarty-> templates (" ./ Templates "); // Setting template directory $ smarty-> templates_c ( "./Templates_c"); // Set the compilation directory // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------- // Left and right boundary, default is {}, but it is easy to conflict with JavaScript // in practical applications, so it is recommended to set <{}> or other.
/ / -------------------------------------------------------------------------------------------- - $ Smarty-> Left_DELIMITER = "{"; $ smarty-> right_delimiter = "}"; $ smarty-> assign ("name", "Li Xiaojun"); // Template variable replacement // compile and display INDEX.TPL Template under ./Templates $ Smarty-> Display ("index.tpl"); "I will display this program as: ================ ================ Execute index.php ================================== ==
========================================================= exmple2. TPL ================================================= < HTML>
The fifty sentence wants to indent the 8 blank alphabets and use "*" to replace these 8 blank characters:
{$ STR5 | Indenter: 8: "*"}}
6. Sixth All the sentences turn to lowercase: {$ str6 | lower}
7. The seventh sentence replaces the TEacherli in the variable: 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% M month% D days" | lower} < / body> html> ============================================= ==== example2 .php ============================================ ==== php / ************************************************** ***** file name: example2.php * role: ** 2 shows an example of the program: big Brother * Email: teacherli@163.com****************** **************************** / include_once ("./ Smarty.class.php"); // Contains Smarty Class File $ SMARTY = new smarty (); // Establish a Smarty instance object $ smarty $ smarty-> templates ("./ Templates"); // Settings template directory $ smarty-> templates_c ("./ Templates_C"); // Set the compilation directory / / -------------------------------------------------------------------------------------------- ---- // Left and right boundary, default is {}, but it is easy to conflict with JavaScript // in practical applications, so it is recommended to set <{}> or other.
/ / -------------------------------------------------------------------------------------------- - $ smarty-> left_delimiter = "{"; $ smarty-> right_delimiter = "}"; $ smarty-> assign ("str1", "my name is xiao jun, li."); // Put Str1 Replacement into my name is xiao jun, li. $ Smarty-> assign ("str2", "My name is:"); // Out: My name is: Li Xiaojun $ smarty-> assign ("str3", " AD "); // Output AD AUGM 21 (My Current Time) // $ Smarty-> Assign (" STR4 ","); // When the fourth sentence does not process the default value, if The previous sentence is replaced with "" $ smarty-> assign ("str5", "8 *"); // Fifth sentence output: ******* Front 8 * $ smarty-> Assign ("Str6", "Teacherli@163.com"); // This will output teacherli@163.com $ 4marty-> Assign ("Str7", "this is teacherli"); // Displayed in the template: This is Li Xiaojun $ smarty-> assign ("str8", "here is combining:"); // Compile and display the index.tpl template under ./Templates $ smarty-> display ("eXample2.tpl");? > Final output effect: ============================================== ========= example2.php output effect: ======================================== ===================
4. The fourth sentence is not processed in the PHP program. It shows the default value: no value!
5. The fifter sentence wants to indent 8 blank alphabets, and use "*" to replace these 8 blank characters:
******** 8 *
6. Sixth sentence TEACHERLI@163.com
7. The seventh sentence replaces TEacherli in the variable: Li Xiaojun: This is Li Xiaojun
8. The eighth sentence is combined using variable modification Here is Combining: Here is a new time: August 21, 2004 body> html> These parameters in the template are called variable modifiers, using these parameters to templates Perform a series of modifications control. Variable modifiers use the "|" and regulator name application modifier, use ":" to separate the modifier parameters. Variable modifiers can be used in combination, as in the eighth sentence, in actual use can be flexible.
Example 3. =============================================== === example3.tpl ============================================== ======
================== PHP / *********************************** ******************* file name: example3.php * role: ** 3 shows an example of the program: big Brother * Email: teacherli@163.com**** ************************************************** / Require_once ("./comm/ Smarty.class.php "); $ smarty = new f117_smarty; $ smarty-> template_dir = './templates/'. $ $ m >cPILE_DIR =' ./Templates_C/'Dir = './configs / '; $ smarty-> cache_dir =' ./cache/'A ($$$$$$$; // ------------------------ -------------------------------------------------- ------------ // Handle {html_checkboxes name = "checkbox" value = $ checkname checked = $ ischecked output = value sepator = "
"} // ----- -------------------------------------------------- ------------------------------- $ smarty-> assign ('checkname ", array (1001 =>' language", 1002 => 'Mathematics', 1003 => 'Foreign Language'); $ Smarty-> Assign ('IsChecked', 1001); // ------------------- -------------------------------------------------- ----------------- // Handle {HTML_RADIOES Name = "Radiobox" Values = $ radioname ch Ecked = $ ischecked Output = $ value separator = "
"} // ------------------------------- -------------------------------------------------- ----- $ Smarty-> Assign ('Radioname', Array (1001 => '", 1002 =>' Mathematics', 1003 => 'Foreign Language')); $ Smarty-> Assign ('IsChecked', 1001);
/ / -------------------------------------------------------------------------------------------- -------------------------------------- // {HTML_SELECT_DATE} No processing will automatically output // -------------------------------------------------- ------------------------------------ $ Smarty-> Display ("EXAMPLE3.TPL");? > ================================================================================================================================================================= ===== example3.php output: ======================================= ================
Foreign language
The following shows a month, day, annual selection box: