Smartyを利用する場合、はじめにSmarty.class.phpを読み込みます。
そして、Smartyのインスタンスを生成することで利用可能になります。
require_once('Smarty.class.php'); $smarty = new Smarty; |
Smartyを『 C:\php\Smarty 』にインストールして、
PHPの設定ファイルphp.iniに『 include_path = ".;C:\php\Smarty" 』と設定しました。
ので上記でSmarty.class.phpを読み込こめるはずです。
参考資料
もしもPHPの設定ファイルphp.iniに『 include_path = ".;C:\php" 』と設定した場合には、
require_once('Smarty/Smarty.class.php'); とSmartyフォルダを指定した記述となります。
読み込めない場合
その1
Smarty.class.phpがNo such file or directoryとなる場合、SMARTY_DIRにSmarty.class.phpのあるフォルダを設定する方法があります。
SMARTY_DIR は、終わりに必ずスラッシュを含める必要があります。
define('SMARTY_DIR','C:/php/Smarty/'); require_once(SMARTY_DIR.'Smarty.class.php'); $smarty = new Smarty; |
その2
Smartyは、Smarty.class.phpファイルさえ見つけられれば利用可能になるので、
フルパスで指定『require_once('C:/php/Smarty/Smarty.class.php');』しても動作します。
問題点の究明
上記の方法でアクセスできるならば、何処かの設定に間違いがありますので、原因を探して普通にアクセスする状態を作成してください。
『 require_once('Smarty.class.php'); 』の記述でアクセスできないようでは問題があります。
Smartyの作業領域
次は、作成するアプリケーションのためにSmarty用の作業フォルダを設定しなければなりません。
フォルダ名称 | 意味 |
templates |
テンプレートファイルを格納するフォルダ。 |
templates_c |
テンプレートファイルを利用して、phpファイルに展開されたファイルを格納するキャッシュ用フォルダ。 |
configs |
テンプレートなどを利用する際の設定や初期値などを登録するファイルを格納するフォルダ。 |
cache |
built-in caching という機能を有効($smarty->caching = true;)にした際に使用されるキャッシュ用ディレクトリ。 |
デフォルトではアプリケーションを構成するファイルと同じディレクトリに置きます。
ドキュメントルート | ─┬─ | index.html | |||
└─ | SampleApp/ | ─┬─ | index.php | ||
├─ | templates/ | ||||
├─ | templates_c/ | ||||
├─ | configs/ | ||||
└─ | cache/ |
動作確認
Smartyが動作するか確認プログラムを作成します(下記を入力したファイルindex.phpを作成)。
<?php require_once('Smarty.class.php'); // create object $smarty = new Smarty; // assign some content. $smarty->assign('name', 'sagasaga'); $smarty->assign('path', ini_get('include_path')); // display it $smarty->display('index.tpl'); ?> |
(下記を入力したテンプレート・ファイルindex.tplを作成)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP"> <title>basic</title> </head> <body> 名前:{$name} 日付:{$smarty.now|date_format:"%Y年 %m月 %d日 "} phpのinclude_path:{$path} </body> </html> |
作成したファイルを以下のように配置します。
ドキュメントルート | ─┬─ | index.html | |||||
└─ | SampleApp/ | ─┬─ | index.php | ||||
├─ | templates/ | ── | index.tpl | ||||
├─ | templates_c/ | ||||||
├─ | configs/ | ||||||
└─ | cache/ |
ブラウザで、http://localhost/SampleApp/index.phpへアクセスすると以下のように表示されます。

一番下は登録した環境パスを表示させております、設定作業中に役立つ情報のはずです。
注意事項
作成するファイルの文字コードに注意してください、漢字を使っていますので間違えると文字化けします。
このままでは、Webブラウザからテンプレートや設定ファイルが見えてしまいます。
そこで、これらのファイルをドキュメントルートの外に配置するのが普通です。
これらのファイルは、Smartyのライブラリがアクセスできれば良いのでドキュメントルート内に無くても動作します。
配置方法は、Smartyクラスのプロパティ $template_dir, $compile_dir, $config_dir, $cache_dir に場所を設定します。
詳細説明はページを改めて『実用環境』でおこないます。