Heim  >  Artikel  >  php教程  >  PHP 中使用 Smarty 之三:Smarty中的保留变量

PHP 中使用 Smarty 之三:Smarty中的保留变量

WBOY
WBOYOriginal
2016-06-13 10:45:141034Durchsuche

 

  在Smarty 中,有一些保留变量,它们是不需要PHP 脚本去分配就可以直接使用,即不用使用$_tpl->assign('var','value') 去分配。

        1、在模板中访问页面请求的变量  

 

{$smarty.get.user}  == $_GET['user'] 

{$smarty.post.user}  == $_POST['user'] 

{$smarty.cookie.username}  == $_COOKIE['username'] 

{$smarty.session.username}  == $_SESSION['username'] 

{$smarty.server.REMOTE_ADDR}  == $_SERVER["REMOTE_ADDR"] 

{$smarty.env.PATH}  ==  $_ENV['PATH'] 

{$smarty.request.username}  ==  $_REQUEST['username'] 

 

        2、在模板文件中访问PHP 脚本中定义的常量和系统常量

 

{$smarty.const.__FILE__}   当前执行的PHP 文件 

{$smarty.const.CONST_VAR}   访问PHP 脚本中define 定义的常量 

 

        3、在模板文件中获取当前服务器的时间

 

{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}  == PHP脚本中的date('Y-m-d H:i:s',time()) 

 

        4、配置文件在模板中的访问

可以通过{$smarty.config.配置变量} 来访问配置文件中的模板变量。这样的话,在模板中访问配置文件中的变量就有两种方法:一、{#配置变量#};二、{$smarty.config.配置变量},如果有区域的话,也是如此。

 

 

        5、获取Smarty 内建函数capture 捕获的数据

capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面,任何在{capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定。在模板中通过$smarty.capture.foo 访问该变量,如果没有指定name 属性,函数默认将使用"default" 作为参数

 


摘自:Lee.的专栏

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP的回调方法Nächster Artikel:Java和PHP在Web开发方面的比较