ひとり勉強ログ

ITエンジニアの勉強したことメモ

今さらSmartyの入門vol.5~予約変数~

Smartyには「テンプレート変数」の他に「予約変数」が用意されている。

引き続き、下記ファイルに追記。

phpファイル 格納ディレクトリ:/htdocs/直下 ファイル名:variables.php

■テンプレートファイル 格納ディレクトリ:/php_libs/smarty/templates/ ファイル名:variables.tpl

$_GET

variables.tpl [html] <form method="get" action=""> <input type="text" name="get_text"> <input type="submit"> </form>

<p>{$smarty.get.get_text}</p> [/html]

$_POST

variables.tpl [html] <form method="post" action=""> <input type="text" name="post_text"> <input type="submit"> </form>

{if isset($smarty.post.post_text)} <p>{$smarty.post.post_text}</p> {/if} [/html]

$_COOKIE

variables.php [php] // Cookieをセット if (!isset($_COOKIE['email'])) { setcookie('email', 'testmail@test.com', time() + 60 *60 * 24); } [/php]

variables.tpl [html] {if isset($smarty.cookies.email)} <p>{$smarty.cookies.email}</p> {/if} [/html]

サーバ変数

[html] <p>{$smarty.server.SERVER_NAME}</p> [/html]

定数

variables.php [php] define ("TEST_CONST", "定数のテスト"); [/php]

variables.tpl [html] <p>セットされる定数:{$smarty.const.TEST_CONST}</p> [/html]

Smartyの情報

variables.tpl [html] <p>現在のテンプレートファイル名:{$smarty.template}</p> <p>Smartyのバージョン:{$smarty.version}</p> [/html]

エスケープ

[html] <p>左デリミタ:{$smarty.ldelim}</p> <p>右デリミタ:{$smarty.rdelim}</p> [/html]