Using the Smarty template, Smarty class files in the Libs directory, the Libs directory is copied to the current directory of the workspace, set up templates and Templates_c folders, can also be specified by the code, the system default directory is recommended.
Require_once (".. /smarty-3.1.15/libs/smarty.class.php ");
$smarty =new Smarty;
$smarty->left_delimiter= "<{"; Set smarty variable left tag, default = {
$smarty->right_delimiter= "}>"; Set smarty variable right tag, default to}
assigning variables
$smarty->assign ("title", "title");
Read the assigned content in the template <{title}>
An array in the program
$arr 5=array (
"Emp1" =>array ("id" = "001", "name" = "Xiaoming", "age" = "19"),
"EMP2" =>array ("id" = "002", "name" = "Daming", "Age" = "15")
);
$smarty->assign ("Arr5", $arr 5);
Array reads in templates
<{$arr 5.emp1.name}>
Objects in the program
Class dog{
var $name;
var $age;
var $color;
function __construct ($name, $age, $color) {
$this->name= $name;
$this->age= $age;
$this->color= $color;
}
}
$dog 1=new Dog ("Xiao Duo", "5", "Red");
$smarty->assign ("Dog1", $dog 1);
Object Reading in Templates
<{$dog 1->name}>
Configuration file config/my.conf
Title= "My first configuration file";
Bgcolor= "Pink";
Read the configuration file in the template
Two-dimensional subscript array in the program
$arr 3=array (
Array ("Hebei", "Jilin", "Zhongzhou"),
Array ("Fortuna", "sa-halogen", "Becqing")
);
The template iterates through the data in the two-dimensional array, where key is the subscript index, and the outer temp represents a one-dimensional array, and item represents the contents of the elements in the array
{foreach from= $arr 3 item=temp key=k}
{foreach from= $temp item=val}
{$val}
{/foreach}
{/foreach}
Smarty Direct Fetch value
<{$smarty. get.username}>//directly get the value of username from Get mode
<{$smarty. server.server_name}>//get the IP server name
Registering a custom function
function test ($args) {
$str = "";
for ($i =0; $i < $args [' time ']; $i + +) {
$str. = "<font size=". $args [' Size ']
."‘ Color= '. $args [' Color ']. "' > "
. $args [' content ']. "." </font> ";
}
return $str;
}
Registration function
$smarty->registerplugin ("function", "HSP", "test");
Calling a custom function in a template
{HSP time= "4" size= "5" content= "Custom Function call" Color= "Blue"}
Registering a custom block
function Test2 ($args, $con) {
$str = "";
for ($i =0; $i < $args [' time ']; $i + +) {
$str. = "<font size=". $args [' Size ']
."‘ Color= '. $args [' Color ']. "' > "
. $con. " "." </font> ";
}
return $str;
}
$smarty->registerplugin ("block", "Blk", "test2");
Calling blocks in a template
{BLK time= "4" size= "5" color= "Blue"}
Call test for Blocks
{/BLK}
Smarty Plugin, Smarty plugin in libs/plugins/directory
The format of the function plug-in is function.hlp.php, the block name block.blk.php, which is functions. php,block. Block name. php
The format of the program code in the file is
Smarty_function_hlp ($args,& $smarty) {} or block name Smarty_block_blk ($args, $con,& $smarty)
& $smarty is the necessary format, $con is the content of the closed tag in the template
Variable adjuster, connection string cat, first letter capital capitalize
{$varname | cat: "YEZ": "Second" | capitalize}//connect first Letter and capitalize
{$eshtml |escape}//HTML output in plain text
Regulator plug-in modifer.xxx.php, program function named Smarty_modifer_xxx ($string)
Verify that the mailbox format is a legitimate regulator
<?php
function Smarty_modifier_isemail ($STR) {
$pattern = "/^[a-za-z_][a-za-z0-9_-]*@[a-za-z0-9]+ (\.[ a-za-z0-9]+) +$/i ";
if (Preg_match ($pattern, $str) ==1) {
Return "is a valid format";
}else{
Return "Invalid mailbox format";
}
}
?>
Using in templates
<{$varname | isemail}>
Preg_match ($pattern, $str, $arrs);//The resulting result is a one-dimensional array
Preg_match_all ($pattern, $str, $arrs);//The result is a two-dimensional array
{include file= "BOTTOM.TPL" varname= "value passed"}
The value passed in {$varname} in Bottom.tpl
$smarty->caching=true;//Open Cache
$smarty->cache_lifetime=60;//Set cache time to 60 seconds
$smarty->debugging=true;//Open the Debug Console control window
Local code updates in real time
function Insert_mytime () {return date ("y-m-d h:i:s");}
Use {Insert Name=mytime} in the template
Set a numbered cache
$smarty->display ("Test.tpl", "11");//Set cache with number 11
$smarty->clearallcache ();//Delete all caches
$smarty->clearcache ("Test.tpl", "11");//delete the cache of the specified number
Turn Smarty Learning notes electronic version finishing