Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

予約変数 {$smarty}

PHP の予約変数 {$smarty} を使用すると、 環境変数やリクエスト変数にアクセスすることができます。 アクセスできる内容について、以下に説明します。

リクエスト変数

$_GET$_POST$_COOKIE$_SERVER$_ENV および $_SESSION ( $request_vars_order および $request_use_auto_globals を参照してください) といった リクエスト変数 にアクセスするには、下の例のようにします。

Example 4.7. リクエスト変数の表示


{* ($_GET) http://www.example.com/index.php?page=foo から page の内容を表示 *}
{$smarty.get.page}

{* ($_POST['page']) フォームから送信された変数"page"の値を表示 *}
{$smarty.post.page}

{* クッキーに登録された"username"の値を表示 ($_COOKIE['username']) *}
{$smarty.cookies.username}

{* サーバ変数"SERVER_NAME"の値を表示 ($_SERVER['SERVER_NAME']) *}
{$smarty.server.SERVER_NAME}

{* 環境変数"PATH"の値を表示 *}
{$smarty.env.PATH}

{* phpのセッション変数"id"の値を表示 ($_SESSION['id']) *}
{$smarty.session.id}

{* get/post/cookies/server/envの値から、変数"username"の値を表示 *}
{$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>

{$smarty.now}

現在の タイムスタンプ にアクセスするには {$smarty.now} を使用します。 この値は、いわゆるエポック (1970年1月1日) からの経過秒数が含まれます。 また、これを直接 date_format 修飾子に渡して表示させることができます。実行するたびに time() がコールされることに注意しましょう。つまり、全体を処理するのに3秒かかるスクリプトがあったとして、 その最初と最後でそれぞれ $smarty.now をコールすると、その値には2秒の差が生じます。


{* date_format 修飾子を用いて、現在の日付と時刻を表示します *}
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}

   

{$smarty.const}

PHP 定数の値に直接アクセスできます。smarty 定数 も参照してください。


<?php
// php で定義されている定数
define('MY_CONST_VAL','CHERRIES');
?>

定数を出力するテンプレート


{$smarty.const.MY_CONST_VAL}

{$smarty.capture}

組み込みの {capture}..{/capture} 関数でキャプチャしたテンプレートの出力にアクセスするには {$smarty.capture} 変数を使用します。 詳細は {capture} のページを参照してください。

{$smarty.config}

{$smarty.config} 変数は、読み込まれた config 変数 を参照するのに使用できます。 {$smarty.config.foo}{#foo#} と同義です。詳細は {config_load} のページを参照してください。

{$smarty.section}、{$smarty.foreach}

{$smarty.section} 変数および {$smarty.foreach} 変数は、 {section} および {foreach} のループプロパティを参照するために使用します。 この中には .first.index といった有用な値が含まれます。

{$smarty.template}

現在処理中のテンプレートの名前を返します。 次の例の container.tpl と、そこからインクルードしている banner.tpl の両方で {$smarty.template} を使用しています。


<b>Main container is {$smarty.template}</b>
{include file='banner.tpl'}

出力は、このようになります。


<b>Main page is container.tpl</b>
banner.tpl

{$smarty.version}

このテンプレートをコンパイルした Smarty のバージョンを返します。


<div id="footer">Powered by Smarty {$smarty.version}</div>

{$smarty.ldelim}、{$smarty.rdelim}

これらの変数を使用して、左右のデリミタをそのまま表示します。 {ldelim}、{rdelim} と同じです。

assigned variables および config variables も参照してください。

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors