找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 778|回复: 0
打印 上一主题 下一主题

Smarty变量用法详解

[复制链接]

2487

主题

2487

帖子

7391

积分

论坛元老

Rank: 8Rank: 8

积分
7391
跳转到指定楼层
楼主
发表于 2018-2-14 05:36:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了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.
Your last login was on {$lastLoginDate}.
输出:
Hello Doug, glad to see you could make it.
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"}
{#pageTitle#}

FirstLastAddress


index.tpl:
{config_load file="foo.conf"}
{$smarty.config.pageTitle}

FirstLastAddress


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

FirstLastAddress


配置文件的变量只有在它们被加载以后才能使用.
更多关于Smarty相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总
希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • smarty的保留变量问题
  • php smarty的预保留变量总结
  • php smarty模版引擎中变量操作符及使用方法
  • php使用Smarty的相关注意事项及访问变量的几种方式
  • smarty模板中使用get、post、request、cookies、session变量的方法
  • Smarty变量调节器失效的解决办法
  • Smarty中常用变量操作符汇总
  • smarty模板引擎中变量及变量修饰器用法实例
  • php中smarty变量修饰用法实例分析
  • PHP模板引擎Smarty中的保留变量用法分析
  • Smarty保留变量用法分析
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端