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 microtime获取浮点的时间戳
Feb 21 PHP
PHP冒泡排序算法代码详细解读
Jul 17 PHP
php 记录进行累加并显示总时长为秒的结果
Nov 04 PHP
coreseek 搜索英文的问题详解
Jun 08 PHP
php中通过数组进行高效随机抽取指定条记录的算法
Sep 09 PHP
php的mkdir()函数创建文件夹比较安全的权限设置方法
Jul 28 PHP
php中的mongodb select常用操作代码示例
Sep 06 PHP
实现PHP框架系列文章(6)mysql数据库方法
Mar 04 PHP
zend framework中使用memcache的方法
Mar 04 PHP
PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)
Dec 02 PHP
基于laravel where的高级使用方法
Oct 10 PHP
Thinkphp 框架配置操作之配置加载与读取配置实例分析
May 15 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
BBS(php &amp; mysql)完整版(四)
2006/10/09 PHP
一些php技巧与注意事项分析
2011/02/03 PHP
php实现redis数据库指定库号迁移的方法
2015/01/14 PHP
Laravel SQL语句记录方式(推荐)
2016/05/26 PHP
PHP获取表单数据与HTML嵌入PHP脚本的实现
2017/02/09 PHP
php使用lua+redis实现限流,计数器模式,令牌桶模式
2019/04/04 PHP
不常用但很实用的PHP预定义变量分析
2019/06/25 PHP
JavaScript触发器详解
2007/03/10 Javascript
让JavaScript拥有类似Lambda表达式编程能力的方法
2010/09/12 Javascript
JavaScript中的View-Model使用介绍
2011/08/11 Javascript
jquery 倒计时效果实现秒杀思路
2013/09/11 Javascript
Ajax请求在数据量大的时候出现超时的解决方法
2014/02/27 Javascript
jQuery中cookie插件用法实例分析
2015/12/04 Javascript
JS实现超简单的汉字转拼音功能示例
2016/12/22 Javascript
详解vuejs几种不同组件(页面)间传值的方式
2017/06/01 Javascript
浅谈在fetch方法中添加header后遇到的预检请求问题
2017/08/31 Javascript
Node.js Stream ondata触发时机与顺序的探索
2019/03/08 Javascript
微信小程序合法域名配置方法
2019/05/06 Javascript
微信小程序实现禁止分享代码实例
2019/10/19 Javascript
[54:25]Ti4 循环赛第三日LGD vs MOUZ
2014/07/12 DOTA
[01:18:36]LGD vs VP Supermajor 败者组决赛 BO3 第一场 6.10
2018/07/04 DOTA
python3使用urllib示例取googletranslate(谷歌翻译)
2014/01/23 Python
在Python的setuptools框架下生成egg的教程
2015/04/13 Python
python如何读写csv数据
2018/03/21 Python
python 命令行传入参数实现解析
2019/08/30 Python
python读取Excel表格文件的方法
2019/09/02 Python
Python super()函数使用及多重继承
2020/05/06 Python
Perfume’s Club英国官网:购买香水和护肤品
2019/11/02 全球购物
美国滑板店:Tactics
2020/11/08 全球购物
计算机应用专业应届毕业生中文求职信范文
2013/11/29 职场文书
文明餐桌活动方案
2014/02/11 职场文书
小学运动会开幕词
2015/01/28 职场文书
建议书格式
2015/02/04 职场文书
电影建党伟业观后感
2015/06/01 职场文书
贫困证明书范文
2015/06/16 职场文书
校园运动会广播稿
2015/08/19 职场文书