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/';
// ランダムで「1〜3」の値を発生させ、
// 変数「$randvalue」にセットします
$smarty->assign( 'randvalue', rand(1, 3) );
// テンプレートファイルを表示
$smarty->display( 'test.tpl' );
「C:\tmp\Smarty\templates\test.tpl」 を作成して、以下の内容を記述してください。
PHPで発生した乱数の結果により、表示される文字を変えます。
【other】ランダム値=1の場合、「小」と表示される。
ランダム値=2の場合、「中」と表示される。
ランダム値=3の場合、「大」と表示される。【/other】
<html>
ランダム値は
{if $randvalue == 1}
小
{elseif $randvalue == 2}
中
{else}
大
{/if}
です。
</html>
上で作成したPHPを実行してください。
以下のように表示されます。
(「大」の部分はランダムで「中」、もしくは「小」になりますが)
ランダム値は 大 です。
[PHP - Smarty - 使い方]
基本の使用例 (文字列をテンプレートファイルに表示) 【assign】
配列分ループを行う 【foreach】
HTMLのリストボックス・コンボボックスを使用する 【html_options】
HTMLのラジオボタンを使用する 【html_radios】
設定ファイルの内容を読み込む 【get_config_vars】