知识点 词汇表 联系我们
按类别浏览
内容搜索    
当前位置:WEB开发技术知识库(www.cn-web.com) .: PHP技术 .: 基础教程 .: php模板引擎smarty变量类型

php模板引擎smarty变量类型


php模板引擎smarty的变量有多种类型,不同类型的变量是以它的前缀符号来区分的。
如果要输出一个变量,用定界符将它括起来就可以,例如:
{$Name}
{$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}">

第一种类型:从PHP分配的变量和模板内的assign函数分配的变量显示时前面需要带"$"符号.(比较常见,实例可参考上述代码)

第二种类型:从配置文件读取的变量或者是smarty的变量($smarty.config),显示时由"#"括起来,代码实例:
foo.conf 配置文件
 
pageTitle = "cn-web.com演示"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor ? "#cccccc"
 
index.tpl 文件(调用方式1)
 
{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: (alternate syntax)调用方式2
 
{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>
 
 
OUTPUT: (两个例子输出结果相同)
 
<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.变量}保留变量可以被用于访问一些特殊的模板变量
如:
Request variables[页面请求变量]
{$smarty.now}
{$smarty.const}
{$smarty.capture}
{$smarty.config}
{$smarty.section}, {$smarty.foreach}
{$smarty.template}


对此文章打分评级

用户评论

增加评论
此文章还没有任何评论!
网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号