smarty模板引擎从php中获取数据的方法
本文实例讲述了smarty模板引擎从php中获取数据的方法。分享给大家供大家参考。具体如下:
smarty可以分配($smarty->assign)的变量类型:所有php支持的数据类型——基本数据类型、复合数据类型、特殊数据类型(具体见smarty相关手册)。
操作/显示文件:index.php
<?php //创建smarty对象 require_once("./libs/Smarty.class.php"); $smarty=newSmarty(); $smarty->assign("aa","helloword");//分配字符串 $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);
//对象类型 classMaster{ public$name; public$address; } $master=newMaster(); $master->name="百度"; $master->address="中关村"; classDog{ 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=newDog("小狗",4,"金黄色",$arr2); $dog->master=$master; $smarty->assign("dog",$dog);
$smarty->display("index.tpl"); ?>