.Water-Sunlight
.サイトメニュー
.検索
.オンライン状況
登録ユーザ: 0
ゲスト: 5
.
<< Prev
ブロック関数と配列変数の使用
« INDEX »
PHP テンプレート編
Next >>
HTMLをブラウザに出力する
作成日:2006/08/07

4B.3 プログラムの作成

 例題 ( ss-tpl ) に従って、Smartyを使ったプログラムの作成方法を説明します。 ss-tpl のプログラムは以下のスクリプトファイルから構成されます。 完全なソースリストについては、 章末の「付録 ソースコード」を参照して下さい。

  • MySmarty.class.php --- Smatryの派生クラスの定義
  • index.php ------------ メインプログラム
  • utils.php ------------ メインプログラムで使用する関数(本編では説明しません)

4B.3.1 Smartyの派生クラスを作成する

 一般にSmartyクラスのインスタンスを直接作成する事はありません。 その代わりに、Smartyの派生クラスを作ってから、そのクラスのインスタンスを作ります。 Smartyの設定はこの派生クラスのコンストラクタの中で行います。 このようにすることで、システム共通のSmartyの設定を一元管理できます。 以下が例題で使用しているSmartyの派生クラスです。

MySmarty.class.php
<?php
require('Smarty/Smarty.class.php');

class MySmarty extends Smarty
{
  function MySmarty ()
  {
    $this->Smarty();

    $mydir = dirname(__FILE__);
    $this->template_dir = "$mydir/templates/";
    $this->compile_dir  = "$mydir/templates_c/";
    $this->config_dir   = "$mydir/configs/";
    $this->cache_dir    = "$mydir/cache/"; 
    array_unshift($this->plugins_dir, "$mydir/plugins/"); 

    $this->debugging     = false;
    $this->force_compile = false;
    $this->caching       = false;

    $this->left_delimiter  = '<{';
    $this->right_delimiter = '}>';
  }
}
?>

 まず、Smartyのクラス定義( Smarty.class.php ) をインクルードし、 Smartyを 基本クラスとした派生クラスを作成します。

例題の環境では、PHPのインクルードパスの下に Smartyのインストールディレクトリ(Smarty/)があります。

require('Smarty/Smarty.class.php');
class MySmarty extends Smarty
{
};

Smarty.class.php の所在は、環境により異なります。 Smatry設置入方については、筆者の運営するWikiサイト(Ground-Sunlight)を参照して下さい。

Ground-SunLight --- スクリプト言語編 PHP5.0/7.Smartyを使う

 次に、コンストラクタを定義し、基本クラスのコンストラクタを呼び出します。

function MySmarty ()
 {
    $this->Smarty();
    ・・・・
}

基本クラスのコンストラクタを呼び出した後に独自の設定を書きます。

$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir  = "$mydir/templates_c/";
$this->config_dir   = "$mydir/configs/";
$this->cache_dir    = "$mydir/cache/"; 
array_unshift($this->plugins_dir, "$mydir/plugins/"); 

$this->debugging     = false;
$this->force_compile = false;
$this->caching       = false;

$this->left_delimiter  = '<{';
$this->right_delimiter = '}>';

例題では、以下の初期設定を行っています。

  • Smartyが使う各種ディレクトリパス
  • デバッグ用の設定
  • デリミタの設定

各設定の説明を簡単にします。

メンバ変数説明
$template_dir
テンプレートファイルを置くディレクトリ ( デフォルトは ./templates )
$compile_dir
コンパイルされたテンプレートが置かれるディレクトリ ( デフォルトは ./templates_c )
$config_dir
テンプレート用の設定ファイルを置くディレクトリ ( デフォルトは ./configs )
$cache_dir
テンプレートのキャッシュが格納されるディレクトリ ( デフォルトは ./cache )
$plugins_dir
プラグインを設置するディレクトリ ( デフォルトは SMARTY_DIR/plugins )
※ 独自のプラグインディレクトリを作る場合は、デフォルト値に追加します。
$debugging
デバッギングコンソールの表示の指定 ( デフォルトは 表示しない )
$force_compile
テンプレートの強制コンパイルの指定( デフォルトは 強制コンパイルしない )
$caching
テンプレート出力のキャッシングの指定 ( デフォルトは キャッシングしない )
$left_delimiter
テンプレートタグの左デリミタ ( デフォルトは { )
$right_delimiter
テンプレートタグの右デリミタ ( デフォルトは } )
<< Prev
ブロック関数と配列変数の使用
« INDEX »>
Page Top
Next >>
HTMLをブラウザに出力する

.