Allocation data type of the smarty template engine

Source: Internet
Author: User
Tags smarty template

Allocation data type of the 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 }>< br/>

Integer: <{$ int}> <br/>

Floating Point: <{$ double}> <br/>

Boolean Type: <{$ bool}> <br/>

Boolean Type false: <{$ bool2}> <br/>

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]}> <br/>

Join array method 1 (not recommended): element 1: <{$ arr2 ['city1 ']}>, element 2: <{$ arr2 ['city2']}>, element 3: <{$ arr2 ['city3']}> <br/>

Join array method 2 (recommended): element 1: <{$ arr2.city1}>, element 2: <{$ arr2.city2}>, element 3: <{$ arr2.city3}> <br/>

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]}> <br/>

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}> <br/>

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}> <br/>

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: <br/>

// Basic attributes

Name-<{$ dog-> name}>,

Age-<{$ dog-> age}>,

Color-<{$ dog-> color}> <br/>

// Array attributes

Arr-<{$ dog-> arr [0]}>,

Arr-<{$ dog-> arr [1]}>,

Arr-<{$ dog-> arr [2]}> <br/>

// Object Attributes

Object-<{$ dog-> master-> name}>,

Object-<{$ dog-> master-> age}> <br/>

Browser display result

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.