Smarty变量用法详解


Posted in PHP onMay 11, 2016

本文实例讲述了Smarty变量用法。分享给大家供大家参考,具体如下:

1. 从PHP分配的变量

调用从PHP分配的变量需在前加"$"符号.(译注:同php一样)

调用模板内的assign函数分配的变量也是这样.(译注:也是用$加变量名来调用)

示例:

index.php:

$smarty = new Smarty;
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastLoginDate', 'January11th, 2001');
$smarty->display('index.tpl');

index.tpl:

Hello {$firstname}, glad to see you couldmake it.
<p>
Your last login was on {$lastLoginDate}.

输出:

Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.

2. 从配置文件读取的变量

配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(后面会讲到)

第二种语法在变量作为属性值并被引号括住的时候非常有用.

(译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量,但可以这样表示{include file="`$smarty.config.includefile`"}不要忘了加``)

示例:

foo.conf:

pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"

index.tpl:

{config_load file="foo.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
    <td>First</td>
    <td>Last</td>
    <td>Address</td>
</tr>
</table>
</body>
</html>

index.tpl:

{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}"bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
    <td>First</td>
    <td>Last</td>
    <td>Address</td>
</tr>
</table>
</body>
</html>

上述两种模板写法都输出:

<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
    <td>First</td>
    <td>Last</td>
    <td>Address</td>
</tr>
</table>
</body>
</html>

配置文件的变量只有在它们被加载以后才能使用.

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

PHP 相关文章推荐
php日历[测试通过]
Mar 27 PHP
php循环输出数据库内容的代码
May 24 PHP
有关php运算符的知识大全
Nov 03 PHP
如何用phpmyadmin设置mysql数据库用户的权限
Jan 09 PHP
PHP无限分类(树形类)的深入分析
Jun 02 PHP
php结合正则获取字符串中数字
Jun 19 PHP
PHP+Mysql+jQuery文件下载次数统计实例讲解
Oct 10 PHP
两款万能的php分页类
Nov 12 PHP
yum命令安装php7和相关扩展
Jul 04 PHP
php 截取GBK文档某个位置开始的n个字符方法
Mar 08 PHP
PHP将数据导出Excel表中的实例(投机型)
Jul 31 PHP
php实现微信分享朋友链接功能
Feb 18 PHP
基于PHP实现商品成交时发送短信功能
May 11 #PHP
Smarty环境配置与使用入门教程
May 11 #PHP
理解php依赖注入和控制反转
May 11 #PHP
php结合web uploader插件实现分片上传文件
May 10 #PHP
配置Nginx+PHP的正确思路与过程
May 10 #PHP
WordPress中设置Post Type自定义文章类型的实例教程
May 10 #PHP
php+MySQL实现登录时验证登录名和密码是否正确
May 10 #PHP
You might like
PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)
2010/04/28 PHP
深入apache host的配置详解
2013/06/09 PHP
php中利用explode函数分割字符串到数组
2014/02/08 PHP
PHP处理会话函数大总结
2015/08/05 PHP
CodeIgniter配置之database.php用法实例分析
2016/01/20 PHP
PHP加密解密类实例代码
2016/07/20 PHP
php数组函数array_push()、array_pop()及array_shift()简单用法示例
2020/01/26 PHP
Javascript 原型和继承(Prototypes and Inheritance)
2009/04/01 Javascript
日期 时间js控件
2009/05/07 Javascript
用Javascript实现Windows任务管理器的代码
2012/03/27 Javascript
seaJs的模块定义和模块加载浅析
2014/06/06 Javascript
angularJS中router的使用指南
2015/02/09 Javascript
ES6学习笔记之正则表达式和字符串正则方法分析
2017/04/25 Javascript
Angular浏览器插件Batarang介绍及使用
2018/02/07 Javascript
详解React中setState回调函数
2018/06/14 Javascript
Vue动态组件和异步组件原理详解
2019/05/06 Javascript
JavaScript Reflect Metadata实现详解
2019/12/12 Javascript
jquery实现图片放大镜效果
2020/12/23 jQuery
[05:31]干嘛呢兄弟!DOTA2 TI9语音轮盘部分出处
2019/05/14 DOTA
python调用新浪微博API项目实践
2014/07/28 Python
简介Python中用于处理字符串的center()方法
2015/05/18 Python
使用WingPro 7 设置Python路径的方法
2019/07/24 Python
python中如何使用虚拟环境
2020/10/14 Python
Python使用paramiko连接远程服务器执行Shell命令的实现
2021/03/04 Python
Omio美国:全欧洲低价大巴、火车和航班搜索和比价
2017/11/08 全球购物
Hurley官方网站:扎根于海滩生活方式的全球青年文化品牌
2020/05/18 全球购物
size?荷兰官方网站:英国高级运动鞋精品店
2020/07/24 全球购物
总经理的岗位职责
2014/02/23 职场文书
2014新课程改革心得体会
2014/03/10 职场文书
2014村党支部书记党建工作汇报材料
2014/11/02 职场文书
我的1919观后感
2015/06/03 职场文书
nginx处理http请求实现过程解析
2021/03/31 Servers
MySQL root密码的重置方法
2021/04/21 MySQL
分析MySQL抛出异常的几种常见解决方式
2021/05/18 MySQL
一篇文章弄懂Python关键字、标识符和变量
2021/07/15 Python
MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
2022/06/14 MySQL