拡張セットアップ
これは、基本的なインストールの続きです。
まず先にこちらから読んで下さい!
Smartyをより僅かに柔軟にするセットアップ方法は、クラスを拡張してあなたのSmartyの環境を初期化する事です。
繰り返しディレクトリパスの設定や同じ変数を割り当てる代わりとして、一箇所でそれらを行う事が出来ます。
新しいディレクトリ"/php/includes/guestbook/"を作成し、"setup.php"という新しいファイルを作成しましょう。
この例の環境では"/php/includes"がinclude_pathです。例と同一にするか、絶対パスを使用して下さい。
例 2-10. /php/includes/guestbook/setup.php を編集する
<?php
// Smartyライブラリを読み込む require('Smarty.class.php');
// setup.phpはアプリケーションに必要なライブラリファイルを // 読み込むのに適した場所です。例: // require('guestbook/guestbook.lib.php');
class Smarty_GuestBook extends Smarty {
function Smarty_GuestBook() { // クラスのコンストラクタ。これらは新しいインスタンスで自動的にセットされます。
$this->Smarty();
$this->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/'; $this->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/'; $this->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/'; $this->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/'; $this->caching = true; $this->assign('app_name','Guest Book'); }
} ?>
|
|
では、setup.phpを使うためにindex.phpファイルを修正してみましょう。
例 2-11. /web/www.mydomain.com/docs/guestbook/index.php を編集する
<?php
require('guestbook/setup.php');
$smarty = new Smarty_GuestBook;
$smarty->assign('name','Ned');
$smarty->display('index.tpl'); ?>
|
|
これで、アプリケーションのために全てを自動的に初期化するSmarty_GuestBookクラスを使って、
Smartyのインスタンスを作成する事がとても簡単である事が分かりました。