Smarty Instance Tutorials _php Basics

Source: Internet
Author: User
Tags html page lowercase modifier php template smarty template
Smarty Example Tutorial (1)


One, what is Smarty?


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.





PHP Code:--------------------------------------------------------------------------------





Example 1:





Let's look at a simple example.


=====================================================


Index.tpl


=====================================================





{* 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>


<hr>


</body>


</html>





=====================================================


Index.tpl


=====================================================





{* 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 *}





================================================


index.php


================================================


<?php





Include_once ("./comm/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





//----------------------------------------------------


Left and right border characters, default to {}, but practical applications are easy to use with JavaScript


Conflict, it is recommended to set <{}> or other.


//----------------------------------------------------


$smarty->left_delimiter = "{";


$smarty->right_delimiter = "}";





$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





//----------------------------------------------------


Left and right border characters, default to {}, but practical applications are easy to use with JavaScript


Conflict, it is recommended to set <{}> or other.


//----------------------------------------------------


$smarty->left_delimiter = "{";


$smarty->right_delimiter = "}";





$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


$smarty->display ("Example2.tpl");


?>





Final output effect:


======================================================


example2.php Output Effect:


======================================================


<html>


<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.











Instance 3.


==================================================


Example3.tpl


==================================================


<html>


<head><title> some of the default functions in the template </title></head>


<body>





{* The following paragraph is equivalent to defining a variable within the template username*}


{Assign var= "UserName" value= "Big Bro"}


A variable within the template definition is shown here: UserName = admin





The following row shows 3 checkbox:<br>


{html_checkboxes name= "CheckBox" values= $CheckName checked= $IsChecked output= $value separator= "<br/>"}


The following line shows 3 radio:<br> in this row


{html_radioes name= "Radiobox" values= $RadioName checked= $IsChecked output= $value separator= "<br/>"}








The following shows one month, day, year selection box:<br>


{Html_select_date}





<hr><b>copyright (C) by Xiaojun, Li 2004<b>{mailto address= "teacherli@163.ccom" text= "contact Author"}





</body>


</html>





======================================================


example3.php


======================================================


<?php





Require_once ("./comm/smarty.class.php");





$smarty = new F117_smarty;


$smarty->template_dir = './templates/';


$smarty->compile_dir = './templates_c/';


$smarty->config_dir = './configs/';


$smarty->cache_dir = './cache/';


$smarty->caching = false;





//--------------------------------------------------------------------------------------


Processing {html_checkboxes name= checkbox values= $CheckName checked= $IsChecked output= $value separator= "<br/>"}


//--------------------------------------------------------------------------------------


$smarty->assign (' CheckName ', Array (


1001 => ' Language ',


1002 => ' mathematics ',


1003 => ' foreign language ');


$smarty->assign (' ischecked ', 1001);








//--------------------------------------------------------------------------------------


Processing {html_radioes name= "Radiobox" values= $RadioName checked= $IsChecked output= $value separator= "<br/>"}


//--------------------------------------------------------------------------------------


$smarty->assign (' Radioname ', Array (


1001 => ' Language ',


1002 => ' mathematics ',


1003 => ' foreign language ');


$smarty->assign (' ischecked ', 1001);





//--------------------------------------------------------------------------------------


{Html_select_date} is automatically exported without processing


//--------------------------------------------------------------------------------------





$smarty->display ("Example3.tpl");


?>





Smarty Example Tutorial (3)








======================================================


example3.php Output Effect:


======================================================


<html>


<head><title> some of the default functions in the template </title></head>


<body>





{Assign var= "UserName" value= "Big Bro"}


Here you will see a variable for the internal definition of the template: UserName = big bro





The following row shows 3 checkbox:<br>


<input type= "checkbox" Name= "checkbox[]" value= "1000" > Language <br/>


<input type= "checkbox" Name= "checkbox[]" value= "1001" checked= "Checked" > Mathematics <br/>


<input type= "checkbox" Name= "checkbox[]" value= "1002" > Foreign language <br/>


The following line shows 3 radio:<br> in this row


<input type= "Radio" name= "radiobox[" value= "1000" > Chinese <br


<input type= "Radio" name= "radiobox[" value= "1001" checked= "Checked" > Mathematics <br/>


<input type= "Radio" name= "radiobox[" value= "1002" > Foreign language <br/>


The following shows one month, day, year selection box:<br>


<select name= "Date_month" >


<option label= "January" value= ">January</option>"


<option label= "February" Value= ">February</option>"


<option label= "March" value= ">March</option>"


<option label= "April" value= ">April</option>"


<option label= "may" value= "a" >May</option>


<option label= "June" value= "a" >June</option>


<option label= "July" value= "modified" >July</option>


<option label= "August" value= "selected=" "Selected" >August</option>


<option label= "September" value= "a" >September</option>


<option label= "October" Value= "ten" >October</option>


<option label= "November" value= "one" >November</option>


<option label= "December" value= ">December</option>"


</select>


<select name= "Date_day" >


<option label= "A" value= "1" >01</option>


<option label= "value=" 2 ">02</option>


<option label= "value=" 3 ">03</option>


<option label= "value=" 4 ">04</option>


<option label= "A" value= "5" >05</option>


<option label= "A" value= "6" >06</option>


<option label= "Modified" value= "7" >07</option>


<option label= "A" value= "8" >08</option>


<option label= "A" value= "9" >09</option>


<option label= "Ten" value= "ten" >10</option>


<option label= "One" value= "one" >11</option>


<option label= "value=" >12</option>


<option label= "value=" >13</option>


<option label= "value=" >14</option>


<option label= "value=" >15</option>


<option label= "value=" >16</option>


<option label= "value=" >17</option>


<option label= "value=" >18</option>


<option label= "value=" >19</option>


<option label= "value=" >20</option>


<option label= "value=" selected= "selected" >21</option>


<option label= "value=" >22</option>


<option label= "value=" >23</option>


<option label= "value=" >24</option>


<option label= "value=" >25</option>


<option label= "value=" >26</option>


<option label= "value=" >27</option>


<option label= "value=" >28</option>


<option label= "value=" >29</option>


<option label= "value=" >30</option>


<option label= "value=" >31</option>


</select>


<select name= "Date_year" >


<option label= "value=" selected= "selected" >2004</option>


</select>


<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)


=======================================================


Example4.tpl


=======================================================


<html>


Process Control in <head><title> templates </title><head>


<body>


<table border= "1" >


{Assign var= "Tbcolor" value= "#D4D0C8"}


Color: {$tbColor}<br>





{section Name=loop loop= $News}


{if $tbColor = = "#D4D0C8"}


<tr bgcolor= "{$tbColor}" >


{Assign var= "Tbcolor" value= "#EEEEEE"}


{Else $tbColor = = "#EEEEEE"}


<TR bgcolor = "{$tbColor}" >


{Assign var= "Tbcolor" value= "#D4D0C8"}


{/if}


<td>{$News [loop].newsid}</td>


<td>{$News [loop].newstitle}</td>


<tr>


{/section}


</table>


</body>


</html>








=======================================================


example4.php


=======================================================


<?php





Require_once ("./public/inc/f117_smarty.php");





$smarty = new F117_smarty;


$smarty->template_dir = './templates/';


$smarty->compile_dir = './templates_c/';


$smarty->config_dir = './configs/';


$smarty->cache_dir = './cache/';


$smarty->caching = false;





$array []= Array ("NewsID" => "001", "Newstitle" => "1th piece of News");


$array []= Array ("NewsID" => "002", "Newstitle" => "2nd piece of News");


$array []= Array ("NewsID" => "003", "Newstitle" => "3rd piece of News");


$array []= Array ("NewsID" => "004", "Newstitle" => "4th piece of News");


$array []= Array ("NewsID" => "005", "Newstitle" => "5th piece of News");


$array []= Array ("NewsID" => "006", "Newstitle" => "6th piece of News");


$array []= Array ("NewsID" => "007", "Newstitle" => "7th piece of News");


$array []= Array ("NewsID" => "008", "Newstitle" => "8th piece of News");








$smarty->assign ("News", $array);





$smarty->display ("Example4.tpl");


?>





Smarty Example Tutorial (4)








==================================================


example4.php output:


==================================================


<html>


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>


<body>


<table>


<tr>


{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}


</tr>


</table>


</body>


</html>








====================================================


example5.php


====================================================


<?php





Require_once ("./public/inc/f117_smarty.php");





$smarty = new F117_smarty;


$smarty->template_dir = './templates/';


$smarty->compile_dir = './templates_c/';


$smarty->config_dir = './configs/';


$smarty->cache_dir = './cache/';


$smarty->caching = false;





$array []= Array ("NewsID" => "001", "Newstitle" => "1th piece of News");


$array []= Array ("NewsID" => "002", "Newstitle" => "2nd piece of News");


$array []= Array ("NewsID" => "003", "Newstitle" => "3rd piece of News");


$array []= Array ("NewsID" => "004", "Newstitle" => "4th piece of News");


$array []= Array ("NewsID" => "005", "Newstitle" => "5th piece of News");


$array []= Array ("NewsID" => "006", "Newstitle" => "6th piece of News");


$array []= Array ("NewsID" => "007", "Newstitle" => "7th piece of News");


$array []= Array ("NewsID" => "008", "Newstitle" => "8th piece of News");








$smarty->assign ("News", $array);





$smarty->display ("Example5.tpl");


?>





==================================================


example5.php Output content:


==================================================


<html>


<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&gt, otherwise the following section is executed.


It's easy to solve a problem that is difficult to implement in a program.


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.