Smarty instance tutorial
--- Template Design Some Keywords: Smarty, PHP Template Engine One, what is Smarty? Smarty is a template PHP template engine written by PHP, which provides the separation of logic and external content, simple speaking, purpose is To use the PHP programmer to separate, the logical content of the programmer to change the program is not affected by the artist page design, the art re-modify the page does not affect the program logic of the program, which is displayed in multiplayers. especially important. 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, shares show that it needs to be updated often, this type of program uses Smarty to slow down the template processing speed. 2. Small project. Small projects use 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, from http: //smarty.php.net/do_download.p ... 獿 IBS all files to copy to the comm directory, complete the basic installation. Other advanced installation methods Please see the manual. Five, smarty Use in the template: This section takes several instances to talk about 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. PHP code: ----------------------------------------------- -------------------------------- Example 1: 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 a smarty file $ smarty = new smarty (); // Establish a smarty instance object $ smarty $ smarty-> template_dir =" ./Templates";// set template directory $ smarty-> compile_dir = " ./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 others.
/ / -------------------------------------------------------------------------------------------- - $ 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 ================================== ==
======================================================== xample2. TPL ================================================= < HTML>
PHP / ********************************************************* file name: example2.php * role: ** 2 shows an example of the program: big Brother * Email: teacherli@163.com*********************** *********************** / include_once ("./ Comm / Smarty.class.php"); // contains smarty file $ smarty = new smarty (); // Establish a smarty instance object $ smarty $ smarty-> template_dir = "./Templates";// set template directory $ smarty-> compile_dir =" ./Templates_c "; // Settings Compile 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 smarty (); $ smarty-> template_dir = './templates/'Dir =' ./Templates_C/ '($ 4MARTY B / ISTERES_C /' - $$. / configs / '; $ smarty-> cache_dir =' ./Cache/' '$$.// ------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------- // Handle {HTML_CHECKBOXES name = "checkbox" value = $ checkname checked = $ ischecked output = value sepator = "
"} // --- -------------------------------------------------- ------------------------------- $ smarty-> assign ('checkname ", array (1001 =>" ', 1002 =>' Mathematics', 1003 => 'Foreign Language'); $ Smarty-> Assign ('IsChecked', 1001); // ----------------- -------------------------------------------------- ------------------- // Handle {html_radioes name = "Radiobox" value = $ radioname checked = $ ischecked output = value sepator = "
"} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------ $ 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: