�以通过PHP的�留��
{$smarty}�访问一些环境��。
下�是这些��的列表:
页�请求��
如$_GET, $_POST,
$_COOKIE, $_SERVER,
$_ENV 和 $_SESSION
�以通过下�的方��使用:
Example 4.8. 显示页�请求��
{* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *}
{$smarty.get.page}
{* display the variable "page" from a form ($_POST['page']) *}
{$smarty.post.page}
{* display the value of the cookie "username" ($_COOKIE['username']) *}
{$smarty.cookies.username}
{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}
{* display the system environment variable "PATH" *}
{$smarty.env.PATH}
{* display the php session variable "id" ($_SESSION['id']) *}
{$smarty.session.id}
{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}
Note
由于历�愿�,{$SCRIPT_NAME}��会作为
{$smarty.server.SCRIPT_NAME}的缩写。
<a href="{$SCRIPT_NAME}?page=smarty">click me</a>
<a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>
Note
虽然Smartyæ��供了较方便直接访问PHP超全局å�˜é‡�的方法,但必须谨慎使用。 直接访问超全局å�˜é‡�会弄乱应用程åº�底层代ç �和模æ�¿è¯æ³•。 最佳的实践是从PHP将需è¦�çš„å�˜é‡�对模æ�¿è¿›è¡Œèµ‹å€¼å†�使用。
当�的时间戳
�以通过{$smarty.now}�获�。
时间戳是从1970年1月1日开始计算到当�的秒数。
当�时间戳�以被date_format
修饰器使用并显示。
注�:在�次使用该��时都会调用time()函数。
比如说一个程�花了三秒�执行,在开头和结�时都调用了$smarty.now,那么这两个数值将差三秒。
{* use the date_format modifier to show current date and time *}
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}
直接访问PHP的常�。�� smarty 常�.
<?php
// the constant defined in php
define('MY_CONST_VAL','CHERRIES');
?>
在模æ�¿ä¸æ˜¾ç¤ºå¸¸é‡�
{$smarty.const.MY_CONST_VAL}
Note
虽然Smarty有较方便直接访问PHP常é‡�的方法。 但这通常会弄乱应用程åº�底层代ç �和模æ�¿è¯æ³•。 最佳的实践是从PHP将需è¦�çš„å�˜é‡�对模æ�¿è¿›è¡Œèµ‹å€¼å†�使用。
Smarty内置函数
{capture}..{/capture}å�¯ä»¥æ�•获其ä¸çš„代ç �输出。
通过{$smarty.capture}å�˜é‡�å�¯ä»¥ä½¿ç”¨è¿™äº›è¾“出的代ç �。
��
{capture}函数。
{$smarty.config}���以获�
�置��。
{$smarty.config.foo}�获�设置��的
{#foo#}。��
{config_load}。
{$smarty.section}��是{section}
循环ä¸çš„属性。
它�供了一些很有用的值,如
.first, .indexç‰ç‰ã€‚
Note
{$smarty.foreach}å�˜é‡�在新的{foreach} è¯æ³•ä¸å·²ç»�ä¸�å†�使用。
但ä»�然支æŒ�Smarty 2.x é£Žæ ¼çš„ foreach è¯æ³•。
返回编译当�模�的Smarty版本。
<div id="footer">由 Smarty {$smarty.version} 引擎驱动</div>
返回å�模æ�¿æ��供的区å�—代ç �。 å�‚è§�模æ�¿ç»§æ‰¿ã€‚
返回父模æ�¿æ��供的区å�—代ç �。 å�‚è§�模æ�¿ç»§æ‰¿ã€‚
用于显示左定界符和å�³å®šç•Œç¬¦ã€‚ç‰å�ŒäºŽ
{ldelim},{rdelim}。
�� 赋值�� 和 �置��
