「C:\tmp\Smarty\configs\test.tpl」 を作成して、以下の内容を記述してください。
titleName = "名前"
titleAddress = "メールアドレス"
titleAge = "年齢"
PHPが動作する任意の場所にPHPファイルを作成し、以下の内容を記述してください。
define( 'SMARTY_DIR', '/tmp/Smarty/libs/' );
require_once( SMARTY_DIR . 'Smarty.class.php' );
$smarty = new Smarty();
// 各ディレクトリの設定
$smarty->template_dir = '/tmp/Smarty/templates/';
$smarty->compile_dir = '/tmp/Smarty/templates_c/';
$smarty->config_dir = '/tmp/Smarty/configs/';
$smarty->cache_dir = '/tmp/Smarty/cache/';
// 読み込む設定ファイルを指定します
$smarty->config_load( 'smarty.conf' );
// 設定ファイルから読み込む要素を指定します
// キーに'titleAddress'を指定しているので、値として'メールアドレス'を取得します
$address = $smarty->get_config_vars( 'titleAddress' );
// 設定ファイルから取得した値を変数にセット
$smarty->assign( 'test', $address );
// テンプレートファイルを表示
$smarty->display( 'test.tpl' );
「C:\tmp\Smarty\templates\test.tpl」 を作成して、以下の内容を記述してください。
PHPから渡された変数を画面に表示します。
<html>
{$test}
</html>
上で作成したPHPを実行してください。
以下のように表示されます。
メールアドレス
[PHP - Smarty - 使い方]
基本の使用例 (文字列をテンプレートファイルに表示) 【assign】
条件分岐を行う 【if】
配列分ループを行う 【foreach】
HTMLのリストボックス・コンボボックスを使用する 【html_options】
HTMLのラジオボタンを使用する 【html_radios】