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 相关文章推荐
phpmyadmin的#1251问题
Nov 25 PHP
PHP抽象类 介绍
Jun 13 PHP
PHP在线生成二维码代码(google api)
Jun 03 PHP
php+mysql实现数据库随机重排实例
Oct 17 PHP
常见的四种POST 提交数据方式(小总结)
Oct 08 PHP
PHP响应post请求上传文件的方法
Dec 17 PHP
php自动加载方式集合
Apr 04 PHP
php 实现301重定向跳转实例代码
Jul 18 PHP
PHP小偷程序的设计与实现方法详解
Oct 15 PHP
php json相关函数用法示例
Mar 28 PHP
thinkPHP5框架实现多数据库连接,跨数据连接查询操作示例
May 29 PHP
php查看一个变量的占用内存的实例代码
Mar 29 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 file_get_contents函数轻松采集html数据
2010/04/22 PHP
PHP中删除变量时unset()和null的区别分析
2011/01/27 PHP
PHP Cookei记录用户历史浏览信息的代码
2016/02/03 PHP
php中foreach结合curl实现多线程的方法分析
2016/09/22 PHP
如何判断图片地址是否失效
2007/02/02 Javascript
JS实现下拉框的动态添加(附效果)
2013/04/03 Javascript
JS实现图片无间断滚动代码汇总
2014/07/30 Javascript
javascript使用prototype完成单继承
2014/12/24 Javascript
简介JavaScript中Math.LOG10E属性的使用
2015/06/14 Javascript
实现音乐播放器的代码(html5+css3+jquery)
2015/08/04 Javascript
利用JavaScript阻止表单提交的两种方法
2016/08/11 Javascript
JS动态的把左边列表添加到右边的实现代码(可上下移动)
2016/11/17 Javascript
JS中检测数据类型的几种方式及优缺点小结
2016/12/12 Javascript
JS/jquery实现一个网页内同时调用多个倒计时的方法
2017/04/27 jQuery
基于jQuery Ajax实现下拉框无刷新联动
2017/12/06 jQuery
angularjs实现table表格td单元格单击变输入框/可编辑状态示例
2019/02/21 Javascript
详解在Angular4中使用ng2-baidu-map的方法
2019/06/19 Javascript
Python运算符重载用法实例分析
2015/06/01 Python
Python中条件判断语句的简单使用方法
2015/08/21 Python
Python编程对列表中字典元素进行排序的方法详解
2017/05/26 Python
python 获取图片分辨率的方法
2019/01/08 Python
详解Python计算机视觉 图像扭曲(仿射扭曲)
2019/03/27 Python
jupyter notebook 参数传递给shell命令行实例
2020/04/10 Python
CSS3 mask 遮罩的具体使用方法
2017/11/03 HTML / CSS
分享29个基于Bootstrap的HTML5响应式网页设计模板
2015/11/19 HTML / CSS
RentCars.com巴西:汽车租赁网站
2016/08/22 全球购物
MONNIER Frères英国官网:源自巴黎女士奢侈品配饰电商平台
2018/12/06 全球购物
俄罗斯首家面向中国消费者的一站式购物网站:Wruru
2020/05/08 全球购物
Java平台和其他软件平台有什么不同
2015/06/05 面试题
C#笔试题集合
2013/06/21 面试题
中学生学习生活的自我评价
2013/10/26 职场文书
人事主管岗位职责
2014/01/30 职场文书
交警个人先进事迹材料
2014/05/11 职场文书
学习党的群众路线剖析材料
2014/10/09 职场文书
Java基础之this关键字的使用
2021/06/30 Java/Android
云服务器部署 Web 项目的实现步骤
2022/06/28 Servers