smarty example tutorial (1) 1. What is smarty? smarty is a template PHP template engine written in PHP. It provides the separation of logic and external content. Simply put, the purpose is to use PHP programmers to Artists are separated. Changing the logical content of the program will not affect the page design of the artist. Re-modification of the page by the artist will not affect the program logic of the program. This is particularly important in multi-person cooperation projects. 2. Advantages of smarty: 1. Speed: Programs written using smarty can achieve maximum speed improvements, which is compared to other template engine technologies. 2. Compiled type: A program written in smarty must be compiled into a non-template technology PHP file at runtime. This file uses a mixture of PHP and HTML. The WEB request will be directly converted to this the next time the template is accessed. file without recompiling the template (when the source program has not been changed) 3. Caching technology: a caching technology selected by smarty, which can cache the HTML file finally seen by the user into a static HTML page, when the cache attribute of smarty is set to true, the user's WEB request will be directly converted into this static HTML file within the cachetime set by smarty, which is equivalent to calling a static HTML file. 4. Plug-in technology: smarty can customize plug-ins. Plug-ins are actually some custom functions. 5. If/elseif/else/endif can be used in templates. Using judgment statements in template files can very conveniently reformat the template. 3. Places where smarty is not suitable for use: 1. Content that needs to be updated in real time. For example, like stock display, which needs to update data frequently, using smarty for this type of program will slow down template processing. 2. Small projects. For small projects where the artist and programmer are both involved because the project is simple, using smarty will lose the advantage of rapid PHP development. 4. Install smarty class: Environment for installing smarty: PHP version 4.06 or above. The installation method of smarty is very simple. Download smarty.t from http://samrty.php.net... Copy all the files in LIB into the comm directory to complete the basic installation. For other advanced installation methods, please refer to the manual. 5. The use of smarty in templates: This section will talk about the use of smarty through several examples. Smarty templates are usually identified by .tpl. For the convenience of art, some people write the extension directly as .html, which is also . This article adopts smarty standard writing method: expressed as .tpl as a smarty template. PHP code:------------------------------------------------ ---------------------------------- Example 1: Let’s look at a simple example first. ================================================= ==== Index.tpl ========================================== ============ {* Displayed is the text included with * in the smarty variable identifier as the comment content *} {include file="header.tpl"}{*Page header*} Hello everyone, my name is {$name}, and you are welcome to read my smarty learning materials. {include file="foot.tpl"}{*End of page*} The above example is a tpl template, where: 1. {**} is a comment on the template page, which is used when smarty parses the template No output is performed, only the template designer can annotate the template. 2. {include file="xxx.tpl"}Use this sentence to include a template file into the current page. In the example, head.tpl and foot.tpl that are common in the website are included. You can Like this Think about it, use this sentence to copy all the contents in xxx.tpl to the current statement. Of course, you don’t need to use this sentence. It’s also perfectly fine to copy the content in XXX.tpl to the current sentence . 3.{$name}: Template variable, the core component in smarty, is contained in the left boundary character {and right boundary character} defined by smarty and is given in the form of a PHP variable. It will be used in the smarty program $smarty- >assign("name", "Li Xiaojun"); Replace $name in the template with the three words "Li Xiaojun". The entire example source program is as follows: ============================== header.tpl ======== =====================
================================ ===================== index.tpl ======================== ============================ {{*display is the text included in the Smarty variable recognition symbol*} {include file="header.tpl"}{*page header*} Hello everyone, my name is {$name}, and you are welcome to read my smarty learning materials. {include file="foot.tpl"}{*End of page*} ================================ ================ Index.php ============================== =================== include_once("./comm/Smarty.class.php"); //Include smarty class files $smarty = new Smarty(); //Create smarty instance object $smarty using using using - Set the compilation directory . --------/// Left and right boundary symbols, the default is {}, but in the actual application, it is easy to conflict with JavaScript //, so it is recommended to set up & lt; {} & gt; or others. //-------------------------------------------------- ----- $smarty->left_delimiter = "{"; $smarty $smarty Variable substitution // Compile and display the index.tpl template located under ./templates $smarty->display("index.tpl"); with in - - , , out out out ============================== Execute index.php ============== ================== smarty tutorial ;body> Hello everyone, my name is Li Xiaojun, welcome everyone to read my smarty learning materials. marty example tutorial (2) This example is an example of comprehensive use of smarty template parameters. These parameters are used to control the output of the template. I only selected a few of them. You can read the other parameters for reference. ================================================ exmple2.tpl ================================================ == capitalize}
2. The second sentence template variable + Li Xiaojun: {$str2|cat:"Li Xiaojun"}
3. The third sentence outputs the current date: {$str3|date_format:"%Y Year %m month %d day"} 4. The fourth sentence is not processed in the .php program, it displays the default value: {$str4|default: "No value! "} 5. The fifth sentence should be indented by 8 blank characters and use "*" to replace these 8 blank characters: 6. In the sixth sentence, change all TEACHerLI@163.com to lowercase: {$str6|lower}
7. In the seventh sentence, replace teacherli in the variable with: Li Xiaojun: {$str7|replace:"teacherli ":"Li Xiaojun"}
8. The eighth sentence is a combination of variable modifiers: {$str8|capitalize|cat:"Here is the newly added time:"|date_format:"%Y year %m month %d日"|lower} ================= example2.php ============================ =================== include_once("./Smarty.class.php"); //Include smarty class files $smarty = new Smarty (); //Create a smarty instance object $smarty using using using using through out using off ’s through out ‐ to ‐set up‐template's directory Table of Contents //------------------------------------------------- ------ . //-------------------------------------------------- ---- s $ SMARTY- & GT; left_delimiter = "{"; $ SMARTY- & GT; Right_delimiter = "}"; n, li. "); //Replace str1 with My Name Is Xiao Jun, Li. $smarty->assign("str2", "My name is: "); //Output: My name is: Li Xiaojun $ smarty->assign("str3", "AD"); //Output August 21, 2004 AD (my current time) //$smarty->assign("str4", ""); / /When the fourth sentence is not processed, the default value will be displayed. If the previous sentence is used, replace it with "" $smarty->assign("str5", ""The first 8 *"); //The fifth sentence output: * *******The first 8* $smarty->assign("str6", "TEACHerLI@163.com"); //teacherli@163.com will be output here $smarty->assign( "str7", "this is teacherli"); //Displayed in the template as: this is Li Xiaojun $smarty->assign("str8", "HERE IS COMBINING:"); //Compile and display at. index.tpl template under /templates index.tpl index.tpl ===================================== Example2.php output effect: ===== =================================================
.
2. The second sentence template variable + Li Xiaojun: My name is: Li Xiaojun
3. The third sentence outputs the current date: August 21, 2004
4. The fourth sentence is not processed in the php program, it displays the default value: no value!
5. The fifth sentence should be indented by 8 blank characters, and use "*" to replace these 8 blank characters:
********The first 8 * . In the sixth sentence, change all TEACHerLI@163.com to lowercase: teacherli@163.com
7. In the seventh sentence, replace teacherli in the variable with: Li Xiaojun: this is 李小jun
8. Eight sentences for combining variable modifiers: Here is Combining: Here is the newly added time: August 21, 2004 < Variable modifiers, these parameters can be used to control a series of modifications to the template. Variable Modifiers Use "|" and the modifier name to apply the modifier, and use ":" to separate the modifier parameters. Variable modifiers can be used in combination, like the eighth sentence, and can be applied flexibly in actual use. Example 3. =============================================== ===== example3.tpl ======================================== ==========
Some default functions in the template
{*The following This paragraph is equivalent to defining a variable username*} {assignay var = "username" value = "Master"} Here is a variable defined by the template: username = admin the line below will display 3. checkBox:
{html_checkboxes name="CheckBox" values=$CheckName checked=$IsChecked output=$value separator=" "} 3 radios will be displayed in this line:< ;br> . gt ;
; : == "#D4D0C8"} < ;tr bgcolor = "{$tbColor}"> . tbColor" value="#D4D0C8"} remember, it is the definition method of setting the internal variables of the template in Example 3. Using the process control statements built into the template can sometimes greatly improve the control ability of the program. The following example is A friend in phpx.com once asked, I will put it here as an example for everyone to learn.
Example 5: Use template built-in flow control statements to output multi-cell content in one row, which is visually smart How many records are output for each record: ============================================ ====== example5.tpl ======================================== =========
Optimize so that the first line does not Output a blank line of
, but to learn the program, it is better to keep it simple, so I will use it like this first. Here is a description: if $smarty.section.loop.index % 4 == 0} {$News[loop].newsID} Let's take a look at this sentence: i {if $ Smarty.section.load.index%4 == 0} $ SMARTY.SECTION.LOOP indicates that there is a part called Loop in the section section of the $ Smarty instance, it has a attribute It's called index, it represents the index value of the current loop, starts incrementing from 0, we compare it with 0 after %4, that is to say, if the current index value is a multiple of 4, it outputs a tr>
, otherwise execute the following part, It easily solves a problem that is very troublesome to implement programmatically.
The above introduces the saving private ryan smarty example tutorial, including the content of saving private ryan. I hope it will be helpful to friends who are interested in PHP tutorials.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn