Smarty The allocation data type of the template engine
This article mainly introduced the Smarty template engine allocation data type, the example analyzes the Smarty template engine data type use skill, has the certain reference value, needs the friend to refer to under
This article describes the allocation data type usage of the Smarty template engine. Share to everyone for your reference. The specific analysis is as follows:
I. Allocation of BASIC data
?
1 2 3 4 5 6 7 8 9 10 11 |
Assigning 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}>
Integral type: <{$int}>
Floating-point type: <{$double}>
Boolean type true: <{$bool}>
Boolean type false: <{$bool 2}>
|
The browser displays the results:
1 means true, 0 means false, false is null, nothing is displayed.
Second, the allocation of composite data array
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 |
Indexed array $res =array (' Shanghai ', ' Beijing ', ' Shenzhen '); $smarty->assign ("arr", $res); Associative arrays $res 2=array (' city1 ' = ' Beijing ', ' city2 ' and ' Guangzhou ', ' City3 ' and ' Hunan '); $smarty->assign ("arr2", $res 2); Indexed two-dimensional arrays $res 3 = Array ( Array (' Xiao Hsiao ', ' Changshan ', ' Wu Bei '), Array (' Sunshine ', ' Keep your ') ); $smarty->assign ("Arr3", $res 3); Associative two-dimensional arrays $res 4 = Array ( Array (' id ' = ' 001 ', ' name ' = ' Zhang San ', ' email ' = ' zhangsan@1163.com '), Array (' url ' = = ' http://www.baidu.com ', ' age ' = ' 28 ') ); $smarty->assign ("Arr4", $res 4); Associative two-dimensional array 2 $res 5=array ( ' Emp1 ' =>array (' id ' = ' 001 ', ' name ' = ' Zhang San ', ' email ' = ' zhangsan@1163.com '), ' Emp2 ' =>array (' url ' = ' http://www.baidu.com ', ' age ' = ' 28 ') ); $smarty->assign ("Arr5", $res 5); |
Template file
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st |
Indexed array: element 1:<{$arr [0]}>, Element 2:<{$arr [1]}>, Element 3:<{$arr [2]}>
Associative array extraction 1 (not recommended): element 1:<{$arr 2[' city1 ']}>, element 2:<{$arr 2[' city2 ']}>, element 3:<{$arr 2[' city3 ']}>
Associative array extraction 2 (recommended): element 1:<{$arr 2.city1}>, element 2:<{$arr 2.city2}>, element 3:<{$arr 2.city3}>
Two-dimensional indexed arrays: Element 1:<{$arr 3[0][0]}> Element 2:<{$arr 3[0][1]}> Element 3:<{$arr 3[0][2]}> Element 4:<{$arr 3[1][0]}> Element 5:<{$arr 3[1][1]}>
Associative two-dimensional array form 1: id-<{$arr 4[0].id}> name-<{$arr 4[0].name}> email-<{$arr 4[0].email}> url-<{$arr 4[1].url}> age-<{$arr 4[1].age}>
Associative two-dimensional array form 2: id-<{$arr 5.emp1.id}> name-<{$arr 5.emp1.name}> email-<{$arr 5.emp1.email}> url-<{$arr 5.emp2.url}> age-<{$arr 5.emp2.age}>
|
The browser displays the results:
Third, the allocation of composite data Objects
?
1 2 3 4 5 6 7 8 9 Ten + + / / / + + + + + + |
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, $arr 6, $master) { $this->name= $name; $this->age= $age; $this->color= $color; $this->arr= $arr 6; $this->master= $master; } } $arr 6=array (' 001 ', ' 002 ', ' 003 '); $master = new Master (' Xiaoming '); $dog 1 = new Dog (' Small white ', 1, ' Bai ', $arr 6, $master); $smarty->assign ("Dog", $dog 1); |
Template file
?
1 2 3 4 5 6 7 8 9 10 11 12 |
Object:
Basic properties name-<{$dog->name}> age-<{$dog->age}> color-<{$dog->color}>
Array properties arr-<{$dog->arr[0]}> arr-<{$dog->arr[1]}> arr-<{$dog->arr[2]}>
Object Properties object-<{$dog->master->name}> object-<{$dog->master->age}>
|
Browser Display Results
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/976535.html www.bkjia.com true http://www.bkjia.com/PHPjc/976535.html techarticle smarty Allocation data type of template engine this article mainly introduces the allocation data types of the Smarty template engine, and analyzes the usage techniques of the Smarty template engine data types, with a certain reference ...