search
HomeBackend DevelopmentPHP TutorialSmarty template engine assigns data types_PHP tutorial

Smarty template engine assigns data types_PHP tutorial

Jul 13, 2016 am 09:59 AM
smartymaindistributeenginedataarticletemplatetype

The allocation data type of smarty template engine

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

?

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 2

3

4smarty模板引擎之分配数据类型    帮客之家

5

6

7

8

9

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);

10 11
//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:
Integer type:
Floating point type:
Boolean type true:
Boolean type false:
The browser displays the result: 1 means true, 0 means false. When false, it is null and nothing is displayed. 2. Distribute array 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 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

?

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

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);

9 10 11 12 13 14 15 16 17 18 19 20 21
Index array: Element 1: , Element 2: , Element 3:
Associative array method 1 (not recommended): Element 1: , Element 2: , Element 3:< ;{$arr2['city3']}>
Associative array selection method 2 (recommended): Element 1: , Element 2: , Element 3:
Two-dimensional index array: Element 1:, Element 2:, Element 3:, Element 4:, Element 5:
Associative two-dimensional array form 1: id-, name-, email-, url-, age-
Associative two-dimensional array form 2: id-, name-, email-, url-, age-
The browser displays the result: 3. Allocate 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('Xiao Ming',22); $dog1 = new Dog('小白',1,'white',$arr6,$master); $smarty->assign("dog",$dog1);

Template file

?

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

1 2

3

4

5

6 7

8

10 11 12
Object:
//Basic attributes name-name}>,
age-age}>,
color-color}>
//Array attribute arr-arr[0]}>, arr-arr[1]}>, arr-arr[2]}>
//Object properties object-master->name}>, object-master->age}>
Browser displays results I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/976535.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976535.htmlTechArticleThe allocation data type of smarty template engine This article mainly introduces the allocation data type of smarty template engine and analyzes the examples Tips for using smarty template engine data types, with certain parameters...
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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 Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)