smarty模板引擎从php中获取数据的方法


Posted in PHP onJanuary 22, 2015

本文实例讲述了smarty模板引擎从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"=>"广州");//关联数组

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

?>

模板文件: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程序设计有所帮助。

PHP 相关文章推荐
PHP 执行系统外部命令 system() exec() passthru()
Aug 11 PHP
PHP中英混合字符串截取函数代码
Jul 17 PHP
获取URL文件名后缀
Oct 24 PHP
PHP curl 获取响应的状态码的方法
Jan 13 PHP
thinkphp文件处理类Dir.class.php的用法分析
Dec 08 PHP
PHP实现获取FLV文件的时间
Feb 10 PHP
php简单获取目录列表的方法
Mar 24 PHP
PHP读MYSQL中文乱码的快速解决方法
Oct 01 PHP
PHP模拟http请求的方法详解
Nov 09 PHP
PHP调用QQ互联接口实现QQ登录网站功能示例
Oct 24 PHP
php如何获取Http请求
Apr 30 PHP
PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库
Apr 16 PHP
smarty模板引擎中变量及变量修饰器用法实例
Jan 22 #PHP
smarty内置函数capture用法分析
Jan 22 #PHP
smarty内置函数config_load用法实例
Jan 22 #PHP
smarty内置函数foreach用法实例
Jan 22 #PHP
smarty内置函数{loteral}、{ldelim}和{rdelim}用法实例
Jan 22 #PHP
smarty内置函数section的用法
Jan 22 #PHP
smarty自定义函数htmlcheckboxes用法实例
Jan 22 #PHP
You might like
php mysql索引问题
2008/06/07 PHP
php实现数组按指定KEY排序的方法
2015/03/30 PHP
php+ajax制作无刷新留言板
2015/10/27 PHP
Laravel下生成验证码的类
2017/11/15 PHP
PHP session垃圾回收机制实例分析
2019/06/28 PHP
php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】
2020/05/27 PHP
Jquery 1.42 checkbox 全选和反选代码
2010/03/27 Javascript
jQuery学习基础知识小结
2010/11/25 Javascript
jQuery中$.click()无效问题分析
2015/01/29 Javascript
JavaScript使用indexOf获得子字符串在字符串中位置的方法
2015/04/06 Javascript
jQuery幻灯片特效代码分享--鼠标滑过按钮时切换(2)
2020/11/18 Javascript
不用一句js代码初始化组件
2016/01/27 Javascript
如何使用vuejs实现更好的Form validation?
2017/04/07 Javascript
vuex中使用对象展开运算符的示例
2017/09/25 Javascript
微信小程序实现下载进度条的方法
2017/12/08 Javascript
详解Vue SSR( Vue2 + Koa2 + Webpack4)配置指南
2018/11/13 Javascript
vue resource发送请求的几种方式
2019/09/30 Javascript
vue实现点击按钮“查看详情”弹窗展示详情列表操作
2020/09/09 Javascript
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
Python实现的圆形绘制(画圆)示例
2018/01/31 Python
和孩子一起学习python之变量命名规则
2018/05/27 Python
Django添加KindEditor富文本编辑器的使用
2018/10/24 Python
Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
2018/12/05 Python
python进行文件对比的方法
2018/12/24 Python
使用python实现名片管理系统
2020/06/18 Python
opencv 阈值分割的具体使用
2020/07/08 Python
python使用bs4爬取boss直聘静态页面
2020/10/10 Python
Python中pass的作用与使用教程
2020/11/13 Python
荷兰牛仔裤网上商店:Jeans Centre
2018/04/03 全球购物
Everything But Water官网:美国泳装品牌
2019/03/17 全球购物
文明礼仪伴我行演讲稿
2014/05/12 职场文书
婚宴邀请函
2015/01/30 职场文书
Python入门学习之类的相关知识总结
2021/05/25 Python
详解Java实践之适配器模式
2021/06/18 Java/Android
redis 解决库存并发问题实现数量控制
2022/04/08 Redis
windows系统安装配置nginx环境
2022/06/28 Servers