足球游戏_中国足彩网¥体育资讯$

Smarty变量用法详解
来源:易贤网 阅读:640 次 日期:2016-08-23 15:58:48
温馨提示:易贤网小编为您整理了“Smarty变量用法详解”,方便广大网友查阅!

本文实例讲述了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程序设计有所帮助。

中国足彩网信息请查看网络编程
易贤网手机网站地址:Smarty变量用法详解
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标