Home > Article > Backend Development > Smarty template engine assigns data types_PHP tutorial
This article mainly introduces the allocation data type of smarty template engine, and analyzes the usage skills of smarty template engine data type with examples. For reference value, friends in need can refer to it
The example in this article describes the usage of allocated data types by smarty template engine. Share it with everyone for your reference. The specific analysis is as follows:
1. Distribute basic data
?
3 4 56 7
8 9
|
//Assign basic data
$smarty->assign("str","hello smarty!");
$smarty->assign("int",143);
$smarty->assign("double",12.1344);
$smarty->assign("bool",true);
$smarty->assign("bool2",false);
String type: <{$str}> Integer type: <{$int}> Floating point type: <{$double}> Boolean type true: <{$bool}> Boolean type false: <{$bool2}> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //Index array $res=array('Shanghai','Beijing','Shenzhen'); $smarty->assign("arr",$res); //Associative array $res2=array('city1'=>'Beijing','city2'=>'Guangzhou','city3'=>'Hunan'); $smarty->assign("arr2",$res2); //Index two-dimensional array $res3 = array( array('Xiaoxiao','Changshan','Wu Bei'), array('Shanshan','Changming') ); $smarty->assign("arr3",$res3); //Associated two-dimensional array $res4 = array( array('id'=>'001','name'=>'Zhang San','email'=>'zhangsan@1163.com'), array('url'=>'http://www.baidu.com','age'=>'28') ); $smarty->assign("arr4",$res4); //Associated two-dimensional array 2 $res5=array( 'emp1'=>array('id'=>'001','name'=>'Zhang San','email'=>'zhangsan@1163.com'), 'emp2'=>array('url'=>'http://www.baidu.com','age'=>'28') ); $smarty->assign("arr5",$res5); |
Template file
?
3 4 56
7 8
|
Index array: Element 1: <{$arr[0]}>, Element 2: <{$arr[1]}>, Element 3: <{$arr[2]}> Associative array method 1 (not recommended): Element 1: <{$arr2['city1']}>, Element 2: <{$arr2['city2']}>, Element 3:< ;{$arr2['city3']}> Associative array selection method 2 (recommended): Element 1: <{$arr2.city1}>, Element 2: <{$arr2.city2}>, Element 3: <{$arr2.city3} > Two-dimensional index array: Element 1:<{$arr3[0][0]}>, Element 2:<{$arr3[0][1]}>, Element 3:<{$arr3[0][2]}>, Element 4:<{$arr3[1][0]}>, Element 5:<{$arr3[1][1]}> Associative two-dimensional array form 1: id-<{$arr4[0].id}>, name-<{$arr4[0].name}>, email-<{$arr4[0].email}>, url-<{$arr4[1].url}>, age-<{$arr4[1].age}> Associative two-dimensional array form 2: id-<{$arr5.emp1.id}>, name-<{$arr5.emp1.name}>, email-<{$arr5.emp1.email}>, url-<{$arr5.emp2.url}>, age-<{$arr5.emp2.age}> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Master{ var $name; var $age; function __construct($name,$age){ $this->name=$name; $this->age=$age; } } class Dog{ var $name; var $age; var $color; var $arr; var $master; function __construct($name,$age,$color,$arr6,$master){ $this->name=$name; $this->age=$age; $this->color=$color; $this->arr=$arr6; $this->master=$master; } } $arr6=array('001','002','003'); $master = new Master('Xiao Ming',22); $dog1 = new Dog('小白',1,'white',$arr6,$master); $smarty->assign("dog",$dog1); |
Template file
?
3 4 56 7 8 |
Object: //Basic attributes name-<{$dog->name}>, age-<{$dog->age}>,
color-<{$dog->color}> //Array attribute arr-<{$dog->arr[0]}>, arr-<{$dog->arr[1]}>, arr-<{$dog->arr[2]}> //Object properties object-<{$dog->master->name}>, object-<{$dog->master->age}> |