定界符重新定义 预防在模板中有js css 代码时 smarty模板引擎解析出现错误!
注意点:
tpl页面模板中参数和值 在 php页面中对应关系。
例如PHP页面代码
class Dog{
var $arrT;
function __construct($arr){
$this->arrT=$arr;
}
}
//一维索引数组
$arrA=array("beijing","shanghai","tianjin");
//实例化对象
$dog=new Dog($arrA);
$dog->master=$master;
$smarty->assign("dog",$dog);
$smarty->display("hello.tpl");
TPL页面代码
arrT.city1}>
在PHP页面中Dog类中的成员变量arrT 对应 TPL页面arrT
Dog类中构造方法形式参数$arr
function __construct($arr){
$this->arrT=$arr;
}
定义要传入Dog类参数中的实参$arrA $arrA=array("beijing","shanghai","tianjin");
在实例化对象时传入实参$arrA $dog=new Dog($arrA);
//创建smarty对象
require_once "./libs/Smarty.class.php";
$smarty=new Smarty();
$smarty->left_delimiter="
$smarty->right_delimiter="}>";
布尔型boolean
php页面$smarty->assign("yang_boolean",true);
tpl页面
浏览器1
整型integer
php页面$smarty->assign("yang_int",567);
tpl页面
浏览器567
浮点型float
php页面$smarty->assign("yang_float",56.7);
tpl页面
浏览器56.7
字符串string
php页面$smarty->assign("yang_string","i come from China");
tpl页面
浏览器i come from China
数组array 一维索引数组
php页面$arr=array("beijing","shanghai","tianjin");
$smarty->assign("arr1",$arr);
tpl页面||||
浏览器beijing||shanghai||tianjin
数组array 一维索引数组
php页面$arr2=array(
"city1"=>"beijing", "city2"=>"上海", "city3"=>"天津"
);
$smarty->assign("arr2",$arr2);
tpl页面||||
浏览器beijing||上海||天津||
数组array 二维数组 (第一层为索引数组,第二层为索引数组)
php页面$arr3=array(
array("北京","上海","天津"),
array("宋江","李逵","卢俊义")
);
$smarty->assign("arr3",$arr3);
tpl页面||||||||||
浏览器北京||上海||天津||宋江||李逵||卢俊义
数组array 二维数组 (第一层为索引数组,第二层为关联数组)
php页面//关联二维数组
$arr4=array(
array(
"id"=>"a001",
"name"=>"yang",
"age"=>"33"
),
array(
"id"=>"a002",
"name"=>"wang",
"age"=>"23"
)
);
$smarty->assign("arr4",$arr4);
tpl页面||||||||||
浏览器a001||yang||33||a002||wang||23
数组array 二维数组 (第一层为关联数组,第二层为关联数组)
php页面$arr5=array(
"emp1"=>array(
"id"=>"a001",
"name"=>"yang",
"age"=>"33"
),
"emp2"=>array(
"id"=>"a002",
"name"=>"wang",
"age"=>"23"
)
);
$smarty->assign("arr5",$arr5);
tpl页面||||||||||
浏览器a001||yang||33||a002||wang||23
数组array 二维数组 (第一层为关联数组,第二层为索引数组)
php页面$arr6=array(
"emp1"=>array(
"a001","yang", "33"
),
"emp2"=>array(
"a002","wang", "23"
)
);
$smarty->assign("arr6",$arr6);
tpl页面||||||||||
浏览器a001||yang||33||a002||wang||23
对象object
php页面class Dog{
var $name;//var是公开变量
var $age;
var $color;
var $arrT;
var $arr2;
var $master;//小狗主人
function __construct($name,$age,$color,$arr,$arr2){
$this->name=$name;
$this->age=$age;
$this->color=$color;
$this->arrT=$arr;
$this->arr2=$arr2;
}
}
//一维索引数组
$arrA=array("beijing","shanghai","tianjin");
//一维关联数组
$arrB=array(
"city1"=>"beijing", "city2"=>"上海", "city3"=>"天津"
);
$arrC=array(
array(
"id"=>"a001",
"name"=>"yang",
"age"=>"33"
),
array(
"id"=>"a002",
"name"=>"wang",
"age"=>"23"
)
);
//对象的分配
class Master{
var $name;
var $address;
}
$master=new Master();
$master->name="津沙港湾";
$master->address="厦门";
//实例化对象
$dog=new Dog("小明",5,"红色",$arrA,$arrC);
$dog->master=$master;
$smarty->assign("dog",$dog);
$smarty->display("hello.tpl");
tpl页面(对象成员,对象中一维索引数组,对象中二维数组,对象中对象的属性)name}>||age}>||color}>
arrT[1]}>
arr2[0].name}>
master->name}>
浏览器小明||5||红色
shanghai
yang
津沙港湾
php页面 (对象中参数传入是一维关联数组$arrB)$arrB=array(
"city1"=>"beijing", "city2"=>"上海", "city3"=>"天津"
);
$dog=new Dog("小明",5,"红色",$arrB,$arrC);
tpl页面(对象中一维关联数组)arrT.city1}>
浏览器beijing