Smarty is a template php template engine written using PHP, it provides a separation of logic and external content, simply, the purpose is to use PHP programmers with the United States Gongfen
Away, the use of the programmer to change the logical content of the program will not affect the graphic design of the page, the art of modifying the page will not affect the program logic, which in many people cooperation project
is particularly important.
Second, Smarty Advantages:
1. Speed: A program written with Smarty can achieve maximum speed, which is relative to other template engine technology.
2. Compile type: Use Smarty to write the program in the runtime to compile into a non-template technology php file, this file using PHP and HTML mixed way, in the next visit
When you ask the template, you convert the Web request directly into this file, and no longer template recompilation (without changes to the source program)
3. Caching technology: A caching technique chosen by smarty, which caches the HTML files that the user eventually sees as a static HTML page, when the cache property of the set Smarty is
True, the user's Web request is converted directly into this static HTML file during the Cachetime period 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 makes it easy to rearrange templates.
Third, not suitable to use Smarty place:
1. Content that needs to be updated in real time. For example, like the stock display, it needs to update the data frequently, and this type of program using Smarty will slow down the template processing speed.
2. Small projects. Small projects because the project is simple and art and programmer with a person's project, the use of Smarty will lose the advantages of PHP development quickly.
Four, installs the Smarty class:
Install Smarty Environment: PHP version 4.06 and above.
Installation Smarty method is very simple, download smarty.t from Http://samrty.php.net ... Put all files in Lib
Copy into the Comm directory and complete the basic installation.
Other advanced installation methods please see the manual.
V. Use of smarty in the template:
This section uses several examples to talk about the use of smarty. The Smarty template is usually identified by the. TPL, and some people write the extension directly for the sake of art. HTML, also can
Of In this paper, the Smarty standard is used to form: the. TPL is represented as a smarty template.
{* Display is smarty variable character with * included text for annotation content *}
{include file= "HEADER.TPL"} {* Page Header *}
Hello, my name is {$name}, you are welcome to read my Smarty learning materials.
{include file= "FOOT.TPL"} {* Page tail *}
This example above is a TPL template, where:
1. {* *} is a comment on the template page that does not produce any output when smarty the template, only for template designers to annotate the template.
2. {Include file= "XXX.TPL"} Use this sentence to include a template file in the current page, where the HEAD.TPL and foot.tpl of common things in the site are included and you can
In this way, you use this sentence to copy the contents of the XXX.TPL to the current statement. Of course, you can also copy the contents of the XXX.TPL to the current statement without using this sentence.
It's perfectly OK.
3.{$name}: Template variable, smarty in the core composition, using the smarty definition of the left border character {and the right boundary character} contains, in PHP variable form, in the Smarty program will use
$smarty->assign ("name", "Li Xiaojun"), and replace $name in the template with "Li Xiaojun" three words.
The entire instance source program is as follows:
=============================
Header.tpl
=============================
<html>
<head>
<title> big bro Smarty Tutorial </title>
</head>
<body>
===============================
Foot.tpl
===============================
<hr>
<center> CopyRight (C) by Big Bro August 2004 </center>
$smarty->assign ("name", "Li Xiaojun"); Make template variable substitution
Compile and display the Index.tpl template that is located under./templates
$smarty->display ("Index.tpl");
?>
The final execution of this program will appear as:
================================
Execute index.php
================================
<html>
<head>
<title> big bro Smarty Tutorial </title>
</head>
<body>
Hello everybody, my name is Li Xiaojun, welcome everybody to read my smarty study material.
<hr>
<center> CopyRight (C) by Big Bro August 2004 </center>
<hr>
</body>
</html>
Smarty Example Tutorial (2)
This example is an example of a comprehensive use of smarty template parameters, which are used to control the output of the template, I only select a few of the other parameters you go to see the reference bar.
================================================
Exmple2.tpl
================================================
<html>
<head><title> eldest brother Smarty Example 2</title></head>
<body>
1. Capitalize the first letter: {$str 1|capitalize}<br>
2. The second sentence template variable + li Xiaojun: {$str 2|cat: "Li Xiaojun"}<br>
3. Output Current date: {$str 3|date_format: "%y year%m Month%d Days"}
4. The fourth sentence. The PHP program does not process, it displays the default value: {$str 4|default: "No value!" "}
5. Let it indent 8 blank letters and use "*" to replace the 8 whitespace characters:<br>
{$STR 5|indent:8: "*"}} <br>
6. The sixth sentence TEACHerLI@163.com all into lowercase: {$str 6|lower}<br>
7. The seventh sentence replaces the Teacherli in the variable with: Li Xiaojun: {$str 7|replace: "Teacherli": "Li Xiaojun"}<br>
8. Use variable modifier for combination: {$str 8|capitalize|cat: "Here is the time of the new addition:" |date_format: "%y year%m month%d days" |lower}
</body>
</html>
===============================================
Example2. php
===============================================
<?php
Include_once ("./smarty.class.php"); Contains Smarty class files
$smarty = new Smarty (); Establish a Smarty instance object $smarty
$smarty->templates ("./templates"); Set up template catalogs
$smarty->templates_c ("./templates_c"); Setting up the compilation directory
$smarty->assign ("str1", "My name is Xiao June, Li."); Replace the str1 with the My Name is Xiao June, Li.
$smarty->assign ("str2", "My name is called:"); Output: My name is: Li Xiaojun
$smarty->assign ("Str3", "A.D."); Output A.D. August 21, 2004 (My current time)
$smarty->assign ("STR4", ""); The default value is displayed when the last sentence is not processed, if the preceding sentence is replaced with the
$smarty->assign ("STR5", "Front 8 *"); Output: ******** Front 8 *
$smarty->assign ("STR6", "TEACHerLI@163.com"); This will output teacherli@163.com
$smarty->assign ("Str7", "This is Teacherli"); displayed in template as: This is Li Xiaojun
$smarty->assign ("Str8", "Here is combining:");
Compile and display the Index.tpl template that is located under./templates
<head><title> eldest brother Smarty Example 2</title></head>
<body>
1. Capitalize the first letter: My Name is Xiao June, li.<br>
2. Second sentence template variable + li Xiaojun: My name is: Li Xiaojun <br>
3. Output current Date: August 21, 2004 A.D. <br>
4. The fourth sentence. PHP program does not handle, it displays the default value: No value! <br>
5. Let it indent 8 blank letters and use "*" to replace the 8 whitespace characters:<br>
Front 8 *<br>
6. The sixth sentence TEACHerLI@163.com all into lowercase:teacherli@163.com<br>
7. The seventh sentence replaces the Teacherli in the variable with the following: Li Xiaojun: This is Li Xiaojun <br>
8. Clause amendment for combination use variable modifier: Here is combining: This is the time of the new addition: August 21, 2004
</body>
</html>
These parameters in the template are referred to as variable modifier (variable modifiers), which can be used to control a series of modifications to the template. Variable modifier
Use the "|" and adjuster name application modifier, use ":" separate modifier parameter. Variable modifier can be combined to use, like the sentence, the actual use of flexible application.
<hr><b>copyright (C) by Xiaojun, Li 2004<b><a href= "mailto:teacherli@163.com" >áªïµx÷õß</a >
</body>
</html>
Example 3 uses some of the built-in functions in the Smarty template, similar functions can be found in the manual, the use of the method is very simple, you can find their own.
Example 4. Template Control (IF/ELSEIF/ELSE/ENDIF)
Process Control in <head><title> templates </title><head>
<body>
<table border= "1" >
<tr bgcolor= "#D4D0C8" >
<td>001</td>
<td> 1th News </td>
</tr>
<TR bgcolor = "#EEEEEE" >
<td>002</td>
<td> 2nd News </td>
</tr>
<tr bgcolor= "#D4D0C8" >
<td>003</td>
<td> 3rd News </td>
</tr>
<TR bgcolor = "#EEEEEE" >
<td>004</td>
<td> 4th News </td>
</tr>
<tr bgcolor= "#D4D0C8" >
<td>005</td>
<td> 5th News </td>
</tr>
<TR bgcolor = "#EEEEEE" >
<td>006</td>
<td> 6th News </td>
</tr>
<tr bgcolor= "#D4D0C8" >
<td>007</td>
<td> 7th News </td>
</tr>
<TR bgcolor = "#EEEEEE" >
<td>008</td>
<td> 8th News </td>
</tr>
</table>
</body>
</html>
Use in template files:
{if $tbColor = = "#D4D0C8"}
<tr bgcolor= "{$tbColor}" >
{Assign var= "Tbcolor" value= "#EEEEEE"}
{Else $tbColor = = "#EEEEEE"}
<TR bgcolor = "{$tbColor}" >
{Assign var= "Tbcolor" value= "#D4D0C8"}
{/if}
This statement block to set the background color of each row, {assign var= "Tbcolor" value= "#D4D0C8"} is also remembered, is example 3 to set the template internal variable definition method,
The use of template-built process control statements can sometimes greatly improve the control of the program, the following example is a friend of Phpx.com once asked, I will it as
Examples are put here for you to learn.
Example 5: Use the template built-in Process Control statement for a row of multiple cell content output, that is, visually smarty a few records per output:
================================================
Example5.tpl
================================================
<html>
<head><title> one line output multiple records </title></head>
<head><title> one line output multiple records </title></head>
<body>
<table>
<tr>
</tr>
<tr>
<td>001</td>
<td> 1th News </td>
<td>002</td>
<td> 2nd News </td>
<td>003</td>
<td> 3rd News </td>
<td>004</td>
<td> 4th News </td>
</tr>
<tr>
<td>005</td>
<td> 5th News </td>
<td>006</td>
<td> 6th News </td>
<td>007</td>
<td> 7th News </td>
<td>008</td>
<td> 8th News </td>
</tr>
</table>
</body>
</html>
Description: The original can also be optimized, so that the first line does not output a blank line of <tr> </tr>, but the learning process, simple for good, first so used. Here is a description:
{section name=loop loop= $News step=1}
{if $smarty. Section.loop.index% 4 = 0}
</tr>
<tr>
{/if}
<td>{$News [loop].newsid}</td>
<td>{$News [loop].newstitle}</td>
{/section}
{section} {/section} refers to a circular part, the next section will be described in detail, we mainly look at this sentence:
{if $smarty. Section.loop.index% 4 = 0}
$smarty. Section.loop points out that the section in the $smarty instance has a part called loop, which has an attribute called Index, which represents the index value of the current loop,
Incremented from 0, we compare it to 0, that is, if the current index value is a multiple of 4, it outputs a </tr><tr>, otherwise the following section is executed.
It's easy to solve a problem that is difficult to implement in a program.
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.