The simple application of smarty template is analyzed in this paper. Share to everyone for your reference, specific as follows:
First you include the Smarty class:
Include_once '.. /libs/smarty.class.php ';
Then create a Smarty object:
You can customize the start terminator for Smarty, which defaults to {}
$smarty->left_delimiter = ' << '; The left sign is <<
$smarty->right_delimiter= ' >> ';//Right sign >>
The most important method seems to be assign, such as:
$smarty->assign (' Test ', $te);
Pay the $test value to test and display the {$test} in the template page TPL
Also like $arr=array (1,2,3), the assignment is still the same:
$smarty->assign (' arr ', $arr);
However, when the template page is displayed, you use the following foreach or Section,foreach usage:
{foreach Item=item from= $arr key=ke Name=foe}
The $item of the $item {/foreach}//Here is equivalent to $arr[$ke],foreach serialization {$smarty. Foreach.foe.iteration}
And the section usage is as follows:
{Section name= ' Test ' loop= $arr}
{$smarty. section.name.iteration}//the output is serialized, with the ordinal starting at 1, index starting at 0
{$arr [test]}
{/section}
The last and most important step you should never forget is this:
$smarty->display (' Test.tpl ');
Here are some common things to show on the template
1. Connection operation:
My name is {$str 1|cat: "Li Bai"};//output result is: My name is $str 1 Li Bai
2. Current Date:
{$str 2|rdate_format: "y%-m%-d%"}//Output result format $str2 date, shaped like 0000-00-00
3. Indent:
{$STR 3|indent:8: "*"} $str Indent 8 before 3 * The default indent is a space
4. Case:
{$str 4|lower}//$str 4 lowercase form
{$str 4|upper}//$str 4 uppercase
Filter:
{$url |escape: ' url '}//replace $url related special characters
<tr bgcolor= ' {cycle values= ' #EBEBEB, #ACABAB '} ' >//tr background alternating colors # Ebebeb, #ACABAB
Match Replacement:
{$str |regex_replace: "~[0-9]~": "ASD"}//if $STR matches [0-9] output ASD
Replace
{$str |replace: "NET": "com"}//Replace net in $str with COM
Include Header template file:
{include file= "TOP.TPL"}
Call the function inside the time.inc.php:
{Insert Name= "getcurrenttime" assign= "Current_time" script= "time.inc.php"}
The current time is {$current _time};
{/insert}
The contents of time.inc.php are as follows:
<?php
function Smarty_insert_getcurrenttime
{return
gmdate (' l,j F Y g:i a T ');//Get current date and time
}
? >
Contact:
mailto{
mailto address= "contact@smartyllc.com" subject= "Smarty LLC Contact" encode= "javascript"}
Load into test.conf:
{conf_load file= "test.conf" section= "test"}
{#tt #}
Test.conf contents are as follows:
More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on Smarty template.