Home > Backend Development > PHP Tutorial > smarty template engine allocation data type, smarty template data type_PHP tutorial

smarty template engine allocation data type, smarty template data type_PHP tutorial

WBOY
Release: 2016-07-13 09:59:06
Original
740 people have browsed it

The allocation data type of smarty template engine, the smarty template data type

The example in this article describes the usage of allocation data type of smarty template engine. Share it with everyone for your reference. The specific analysis is as follows:

1. Distribute basic data

//分配基本数据
$smarty->assign("str","hello smarty!");
$smarty->assign("int",143);
$smarty->assign("double",12.1344);
$smarty->assign("bool",true);
$smarty->assign("bool2",false); 
字符串类型:<{$str}> <br/>
整型:<{$int}> <br/>
浮点型:<{$double}> <br/>
布尔类型真:<{$bool}> <br/>
布尔类型假:<{$bool2}> <br/> 
Copy after login

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

//索引数组 
$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);
Copy after login

Template file

索引数组:元素1:<{$arr[0]}>,元素2:<{$arr[1]}>,元素3:<{$arr[2]}> <br/> 
关联数组取法1(不推荐):元素1:<{$arr2['city1']}>,元素2:<{$arr2['city2']}>,元素3:<{$arr2['city3']}> <br/> 
关联数组取法2(推荐):元素1:<{$arr2.city1}>,元素2:<{$arr2.city2}>,元素3:<{$arr2.city3}> <br/> 
二维索引数组: 
元素1:<{$arr3[0][0]}>, 
元素2:<{$arr3[0][1]}>, 
元素3:<{$arr3[0][2]}>, 
元素4:<{$arr3[1][0]}>, 
元素5:<{$arr3[1][1]}> <br/> 
关联二维数组形式1: 
id-<{$arr4[0].id}>, 
name-<{$arr4[0].name}>, 
email-<{$arr4[0].email}>, 
url-<{$arr4[1].url}>, 
age-<{$arr4[1].age}> <br/> 
关联二维数组形式2: 
id-<{$arr5.emp1.id}>, 
name-<{$arr5.emp1.name}>, 
email-<{$arr5.emp1.email}>, 
url-<{$arr5.emp2.url}>, 
age-<{$arr5.emp2.age}><br/>
Copy after login

The browser displays the result:

3. Allocate composite data objects

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);
Copy after login

Template file

对象:<br/> 
//基本属性 
name-<{$dog->name}>, 
age-<{$dog->age}>, 
color-<{$dog->color}> <br/> 
//数组属性 
arr-<{$dog->arr[0]}>, 
arr-<{$dog->arr[1]}>, 
arr-<{$dog->arr[2]}> <br/> 
//对象属性 
object-<{$dog->master->name}>, 
object-<{$dog->master->age}> <br/> 
Copy after login

Browser displays results

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976032.htmlTechArticlesmarty template engine allocation data type, smarty template data type This article describes the usage of smarty template engine allocation data type . Share it with everyone for your reference. Specific analysis is as follows...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template