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 empty函数判断mysql表单是否为空
Apr 12 PHP
PHP CURL CURLOPT参数说明(curl_setopt)
Sep 30 PHP
php操作XML、读取数据和写入数据的实现代码
Aug 15 PHP
php实现给图片加灰色半透明效果的方法
Oct 20 PHP
CI框架Session.php源码分析
Nov 03 PHP
php中使用key,value,current,next和prev函数遍历数组的方法
Mar 17 PHP
Laravel 5框架学习之表单验证
Apr 08 PHP
Zend Framework开发入门经典教程
Mar 23 PHP
php通过curl添加cookie伪造登陆抓取数据的方法
Apr 02 PHP
PHP验证码无法显示的原因及解决办法
Aug 11 PHP
thinkPHP5框架auth权限控制类与用法示例
Jun 12 PHP
php设计模式之工厂方法模式分析【星际争霸游戏案例】
Jan 23 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 5.3.5安装memcache注意事项小结
2011/04/12 PHP
php中创建和调用webservice接口示例
2014/07/25 PHP
VB中的RasEnumConnections函数返回632错误解决方法
2014/07/29 PHP
ThinkPHP、ZF2、Yaf、Laravel框架路由大比拼
2015/03/25 PHP
php curl模拟post请求和提交多维数组的示例代码
2015/11/19 PHP
wordpress网站转移到本地运行测试的方法
2017/03/15 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
php获取微信基础接口凭证Access_token
2018/08/23 PHP
JavaScript 验证浏览器是否支持javascript的方法小结
2009/05/17 Javascript
JavaScript类和继承 constructor属性
2010/03/04 Javascript
33个优秀的jQuery 教程分享(幻灯片、动画菜单)
2011/07/08 Javascript
轻松创建nodejs服务器(6):作出响应
2014/12/18 NodeJs
JS实现5秒钟自动封锁div层的方法
2015/02/20 Javascript
在vue中实现简单页面逆传值的方法
2017/11/27 Javascript
Vue.Draggable拖拽功能的配置使用方法
2020/07/29 Javascript
Jquery的Ajax技术使用方法
2019/01/21 jQuery
详解Jest结合Vue-test-utils使用的初步实践
2019/06/27 Javascript
vue监听用户输入和点击功能
2019/09/27 Javascript
python绘制直线的方法
2018/06/30 Python
浅谈flask源码之请求过程
2018/07/26 Python
多个python文件调用logging模块报错误
2020/02/12 Python
python脚本监控logstash进程并邮件告警实例
2020/04/28 Python
CSS3的文字阴影—text-shadow的使用方法
2012/12/25 HTML / CSS
Coach澳大利亚官方网站:美国著名时尚奢侈品牌
2017/05/24 全球购物
西班牙鞋子和箱包在线销售网站:zapatos.es
2020/02/17 全球购物
心理健康心得体会
2014/01/02 职场文书
网络维护中文求职信
2014/01/03 职场文书
五好党支部事迹材料
2014/02/06 职场文书
机械工程及自动化专业求职信
2014/09/03 职场文书
2015小学语文教师个人工作总结
2015/05/20 职场文书
升学宴来宾致辞
2015/07/27 职场文书
美德少年主要事迹材料
2015/11/04 职场文书
导游词之开封禹王台风景区
2019/12/02 职场文书
ztree+ajax实现文件树下载功能
2021/05/18 Javascript
Python手拉手教你爬取贝壳房源数据的实战教程
2021/05/21 Python
C3 线性化算法与 MRO之Python中的多继承
2021/10/05 Python