search
Homephp教程php手册smarty模板引擎之分配数据类型

smarty模板引擎之分配数据类型

Jun 13, 2016 am 09:08 AM
smartymaindistributeenginedataarticletemplatetype

smarty模板引擎之分配数据类型

 这篇文章主要介绍了smarty模板引擎之分配数据类型,实例分析了smarty模板引擎数据类型的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

 

 

本文实例讲述了smarty模板引擎之分配数据类型用法。分享给大家供大家参考。具体分析如下:

一、分配基本数据

?

1

2

3

4

5

6

7

8

9

10

11

//分配基本数据

$smarty->assign("str","hello smarty!");

$smarty->assign("int",143);

$smarty->assign("double",12.1344);

$smarty->assign("bool",true);

$smarty->assign("bool2",false);

字符串类型:

整型:

浮点型:

布尔类型真:

布尔类型假:

浏览器显示结果:

1表示真,0表示假,假的时候为null,什么都不显示。

二、分配复合数据之数组

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//索引数组

$res=array('上海','北京','深圳');

$smarty->assign("arr",$res);

//关联数组

$res2=array('city1'=>'北京','city2'=>'广州','city3'=>'湖南');

$smarty->assign("arr2",$res2);

//索引二维数组

$res3 = array(

array('潇晓','常山','吴蓓'),array('珊珊','常明')

);

$smarty->assign("arr3",$res3);

//关联二维数组

$res4 = array(

array('id'=>'001','name'=>'张三','email'=>'zhangsan@1163.com'),

array('url'=>'http://www.baidu.com','age'=>'28')

);

$smarty->assign("arr4",$res4);

//关联二维数组2

$res5=array(

'emp1'=>array('id'=>'001','name'=>'张三','email'=>'zhangsan@1163.com'),

'emp2'=>array('url'=>'http://www.baidu.com','age'=>'28')

);

$smarty->assign("arr5",$res5);

模板文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

索引数组:元素1:,元素2:,元素3:

关联数组取法1(不推荐):元素1:,元素2:,元素3:

关联数组取法2(推荐):元素1:,元素2:,元素3:

二维索引数组:

元素1:,

元素2:,

元素3:,

元素4:,

元素5:

关联二维数组形式1:

id-,

name-,

email-,

url-,

age-

关联二维数组形式2:

id-,

name-,

email-,

url-,

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('小明',22);

$dog1 = new Dog('小白',1,'white',$arr6,$master);

$smarty->assign("dog",$dog1);

模板文件

?

1

2

3

4

5

6

7

8

9

10

11

12

对象:

//基本属性

name-name}>,

age-age}>,

color-color}>

//数组属性

arr-arr[0]}>,

arr-arr[1]}>,

arr-arr[2]}>

//对象属性

object-master->name}>,

object-master->age}>

浏览器显示结果

希望本文所述对大家的php程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.