本文实例讲述了smarty模板引擎从php中获取数据的方法。分享给大家供大家参考。具体如下:
smarty可以分配($smarty->assign)的变量类型:所有php支持的数据类型——基本数据类型、复合数据类型、特殊数据类型(具体见smarty相关手册)。
操作/显示文件:index.php
复制代码 代码如下:<"./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->assign("aa","hello word");//分配字符串
$smarty->assign("bb",123);//分配整型
$smarty->assign("cc",90.8);//分配float型,浮点型
$smarty->assign("dd",true);//分配字符串
//分配数组,数组一般从数据库取出,这里直接给数组
$arr1 = array("北京","上海","广州");//索引数组
$smarty->assign("arr1",$arr1);//分配索引数组
$arr2 = array("city1"=>"北京","city2"=>"上海","city3"=>"广州");//关联数组
$smarty->assign("arr2",$arr2);//分配关联数组
$arr3 = array(array("北京","上海","广州"),array("关羽","张飞","美女"));
$smarty->assign("arr3",$arr3);
$arr4 = array("aa"=>array("北京","上海","广州"),"bb"=>array("关羽","张飞","美女"));
$smarty->assign("arr4",$arr4);
//对象类型
class Master{
public $name;
public $address;
}
$master = new Master();
$master->name="百度";
$master->address = "中关村";
class Dog{
public $name;
public $age;
public $color;
public $arr;
public $master;
function __construct($name,$age,$color,$arr){
$this->name = $name;
$this->age = $age;
$this->color = $color;
$this->arr = $arr;
}
}
$dog = new Dog("小狗",4,"金黄色",$arr2);
$dog->master = $master;
$smarty->assign("dog",$dog);
$smarty->display("index.tpl");
"codetitle">复制代码 代码如下:<html>
<h2>smarty变量操作</h2>
<p style="color:green;">取字符串:{$aa}</p>
<p style="color:red;">取整数:{$bb}</p>
<p style="color:blue;">取浮点型:{$cc}</p>
<p style="color:orange;">取布尔值:{$dd}</p>
<p style="color:indigo;">取数组(索引数组):{$arr1[0]}--{$arr1[1]}--{$arr1[2]}</p>
<p style="color:green;">取数组(关联数组):{$arr2.city1}--{$arr2.city2}--{$arr2.city3}</p>
<p style="color:red;">取二组数组(索引,取单个):{$arr3[0][0]}</p>
<p style="color:red;">取二组数组(索引,遍历全部):</p>
<p style="color:blue;">取二维数组(关联):{$arr4.aa[2]}</p>
<p style="color:blue;">取二维数组(关联、遍历):</p>
<p style="color:orange;">取对象(普通属性):{$dog->name}</p>
<p style="color:orange;">取对象(数组属性):{$dog->arr.city1}</p>
<p style="color:orange;">取对象(对象属性):{$dog->master->name}</p>
</html>
希望本文所述对大家的php程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[FLAC/分轨][251.78MB]
- 黄韵玲2001-巴结音乐[全员集合][WAV+CUE]
- 郑绪岚2015-流金岁月·精选[首版][WAV+CUE]
- 周冰倩1995《沧桑情歌》JVC日本录制[WAV+CUE]
- 辛禄乐队.2024-热烈的少年【星辰】【FLAC分轨】
- 成龙.1996-龙的心【滚石】【WAV+CUE】
- 童安格.1991-一世情缘【宝丽金】【WAV+CUE】
- 韩红2010-擦肩而过[央金玛][WAV+CUE]
- RiccardoTesi-UnballoLiscio,Vol.II(2024)[24-48]FLAC
- 金池VS刘罡2009《双声情》[WAV+CUE]
- 冯奥迪《卄》[320K/MP3][67.08MB]
- 冯奥迪《卄》[FLAC/分轨][172.46MB]