smarty模板引擎从php中获取数据的方法
《:smarty模板引擎从php中获取数据的方法》要点: 本篇章节讲解smarty模板引擎从php中获取数据的办法.分享给大家供大家参考.具体如下:PHP学习 smarty可以分配($smarty->assign)的变量类型:所有php支持的数据类型――基本数据类型、复合数据类型、特殊数据类型(具体见smarty相关手册). 操作/显示文件:index.php
代码如下:
<?php
//创建smarty对象 require_once("./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"=>"广州");//关联数组 $arr3 = array(array("北京","广州"),array("关羽","张飞","美女")); $arr4 = array("aa"=>array("北京","bb"=>array("关羽","美女")); //对象类型 $smarty->display("index.tpl"); 模板文件:index.tpl
代码如下:
<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程序设计有所赞助. 编程之家培训学院每天发布《:smarty模板引擎从php中获取数据的方法》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |