The allocation data type of the smarty template engine. The assignment data type of the smarty template engine this article mainly introduces the allocation data type of the smarty template engine. the instance analyzes the usage skills of the smarty template engine data type, allocation data type with certain parameter smarty template engine
This article mainly introduces the distribution data type of the smarty template engine. the instance analyzes the usage skills of the smarty template engine data type and has some reference value. For more information, see
This document describes how to use the smarty template engine to allocate data types. Share it with you for your reference. The specific analysis is as follows:
1. allocate basic data
?
|
1
2
3
4
5
6
7
8
9
10
11 |
// Allocate 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: <{$ int}>
Floating point: <{$ double}>
Boolean type: <{$ bool}>
Boolean type false: <{$ bool2}>
|
Browser Display result:
1 indicates true, 0 indicates false, null indicates false, and nothing is displayed.
2. allocating arrays of composite data
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
// Index the array
$ Res = array ('Shanghai', 'Beijing', 'shenzhen ');
$ Smarty-> assign ("arr", $ res );
// Associate an array
$ Res2 = array ('city1 '=> 'Beijing', 'city2' => 'Guangzhou ', 'city3' => 'hunan ');
$ Smarty-> assign ("arr2", $ res2 );
// Index two-dimensional array
$ Res3 = array (
Array ('Xiao Xiao ', 'changshan', 'Wu Bei'), array ('shanshan', 'changming ')
);
$ Smarty-> assign ("arr3", $ res3 );
// Associate a two-dimensional array
$ Res4 = array (
Array ('id' => '001', 'name' => 'Zhang San', 'Email '=> 'zhangsan @ zhang3.com '),
Array ('URL' => 'http: // www.baidu.com ', 'age' => '28 ')
);
$ Smarty-> assign ("arr4", $ res4 );
// Associate two-dimensional array 2
$ Res5 = array (
'Emp1' => array ('id' => '001', 'name' => 'zhangsan', 'Email '=> 'zhangsan @ zhang3.com '),
'Emp2' => array ('URL' => 'http: // www.baidu.com ', 'age' => '28 ')
);
$ Smarty-> assign ("arr5", $ res5 );
|
Template File
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
Index array: Element 1: <{$ arr [0]}>, element 2: <{$ arr [1]}>, Element 3: <{$ arr [2]}>
Join array method 1 (not recommended): Element 1: <{$ arr2 ['city1 ']}>, element 2: <{$ arr2 ['city2']}>, element 3: <{$ arr2 ['city3']}>
Join array 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]}>
Join 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}>
Join 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}>
|
Browser Display result:
3. allocating composite data objects
?
|
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 ('xiaoming ', 22 );
$ Dog1 = new Dog ('White ', 1, 'White', $ arr6, $ master );
$ Smarty-> assign ("dog", $ dog1 );
|
Template File
?
|
1
2
3
4
5
6
7
8
9
10
11
12 |
Object:
// Basic attributes
Name-<{$ dog-> name}>,
Age-<{$ dog-> age}>,
Color-<{$ dog-> color}>
// Array attributes
Arr-<{$ dog-> arr [0]}>,
Arr-<{$ dog-> arr [1]}>,
Arr-<{$ dog-> arr [2]}>
// Object attributes
Object-<{$ dog-> master-> name}>,
Object-<{$ dog-> master-> age}>
|
Browser Display result
I hope this article will help you with php programming.
This article describes the distribution data type of the smarty template engine. the instance analyzes the usage skills of the data type of the smarty template engine and has certain parameters...