Smarty ――内置变量的使用
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2014-04-29 06:04:39
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
在模版里,我们可以透过?Smarty?预先定义好的变量,以存取?PHP?中的?$_SERVER?变量或是?$_SESSION?变量等超全哉变量(Superglobals)。
???在模版中的用法如下: {$smarty.server.SERVER_NAME}?相当于?$_SERVER['SERVER_NAME'] {$smarty.get.var}?相当于?$_GER['var'] {$smarty.session.id}?相当于?$_SESSION['id'] {$smarty.const.MY_CONST}?相当于?MY_CONNSTSmarty?保留变量的名称等同的PHP变量$smarty.get$_GET$smarty.post$_POST$smarty.request$_REQUEST$smarty.session$_SESSION$smarty.cookies$_COOKIE$smarty.env$_ENV$smarty.server$_SERVER$smarty.const利用define函数定义的常量 配置文件变量 Smarty?也可以透过配置文件来载入模版变量,虽然这种方式在实用中较为少见,但有时也有它的妙用。使用配置文件载入变量之前,我们需要建立一个配置文件: 假如配置文件名叫var.conf
- site_title?=?"?Sample?title"
- background_color?=?#eeeeee
- common_content?=?"This?is?common?Content"
- $smarty->config_dir?=?配置文件的路径;
- $smarty->config_load('var.conf');//配置文件名
- <html>
- <head>
- <title>{#site_title#}</title>
- </head>
- <body?style="background-color:?{?#background_color#?}">
- <p>{?#common_content#?}</p>
- </body>
- </html>
- {config_load?file="ch05/05.conf"}
- <html>
- <head>
- <title>{#site_title#}</title>
- </head>
- <body?style="background-color:?{?#background_color#?}">
- <p>{?#common_content#?}</p>
- </body>
- </html>