+-----------------------------------------------------------------------
|
| 導入と設定
|
+-----------------------------------------------------------------------

1.パッケージのダウンロード
http://smarty.php.net/download.php

2.smartyライブラリを解凍してphp定義ライブラリに保存
cd /tmp
tar xvfz Smarty-xxxxxxxx.tar.gz
mkdir /usr/local/lib/php/Smarty
mv /temp/Smarty解凍したディレクトリ/libs/* /usr/local/lib/php/Smarty/

3.パスの追加
php.iniファイルにSmartyのパスをinclude_pathに追加
vi /usr/local/lib/php.ini

include_path = ".:/usr/local/lib/php:/usr/local/lib/php/Smarty"
※上記一行を最終行に追加(どこでもいいみたい)

4.Apache再起動
/etc/init.d/httpd stop 又は/usr/local/apache2/bin/apachectl stop
/etc/init.d/httpd start 又は/usr/local/apache2/bin/apachectl start

------------------------------------------------------------------------------------------

+-----------------------------------------------------------------------
|
| サンプルを動かしてみる
|
+-----------------------------------------------------------------------

稼動前提.ディレクトリ構成

documentroute -----+----index.html
                   |
                   +----SampleApp/----+----- index.php
                                      |
                                      +----- templates/------ index.tpl
                                      |
                                      +----- templates_c/
                                      |
                                      +----- configs/
                                      |
                                      +----- cache/


Smartyは、templates, templates_c, configs という三つのディレクトリを用意しなければなりません。
もし、ページの描画を高速化する built-in caching という機能を有効にした場合、cacheというディレ
クトリも必要です。

[templates]
テンプレートファイルを格納するディレクトリ。各テンプレートファイルの拡張子は .tpl。

[templates_c]
テンプレートファイルを利用して php ファイルに展開されたファイルを格納するキャッシュ用デ
ィレクトリ。Webサーバー(httpd)がファイルを作成するので、httpd.conf で設定されているWeb
サーバーを起動するアカウント(User, Group)が書き込みできるオーナーとパーミッションにし
なければなりません。

[configs]
テンプレートなどを利用する際の設定や初期値などを登録するファイルを格納するディレクトリ。
設定ファイルが test.conf という名前の場合、テンプレートファイル内で以下のように利用します。

[cache]
built-in caching という機能を有効($smarty->caching = true;)にした際に使用されるキャッシュ
用ディレクトリ。Webサーバー(httpd)がファイルを作成するので、httpd.conf で設定されているWeb
サーバーを起動するアカウント(User, Group)が書き込みできるオーナーとパーミッションにしなけ
ればなりません。



※テスト環境ではdocumentroute  --> /usr/loca/apache2/htdocs/です。
※テスト環境のapacheユーザ及びグループ名は(/usr/local/apache2/conf/httpd.conより)
User nobody
Group #-1


1.ディレクトリ構成を作成
cd /usr/loca/apache2/htdocs/
mkdir SampleApp
cd SampleApp
mkdir templates/ templates_c/ configs/ cache/

chown nobody:nobody templates_c cache      <--- apacheのUserで指定したアカウントを指定
chmod 770 templates_c cache


2.index.phpのソース
---------------------------------------------------------------------------- >> ここから
<?php
require_once('Smarty/Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content.
$smarty->assign('name', 'Shinta');
$smarty->assign('url', 'http://www.my-domain.com/');

// display it
$smarty->display('index.tpl');

?>
---------------------------------------------------------------------------- << ここまで

Smarty を利用する場合、はじめに
2-1.Smarty.class.phpを読み込みます。
そして、
2-2.Smarty のインスタンスを生成することで利用可能になります。
以下が定義箇所
-----------------------------------------------------------------------------------
require_once('Smarty/Smarty.class.php');          // 2-1.Smarty.class.phpを読み込みます
$smarty = new Smarty;                             // 2-2.Smarty のインスタンスを生成
-----------------------------------------------------------------------------------

どうしても、 Smarty.class.php が No such file or directory となる場合、
SMARTY_DIR に Smarty.class.phpのあるディレクトリを設定する必要があります。
SMARTY_DIR は、終わりに必ずスラッシュを含める必要があります。

define('SMARTY_DIR','/usr/local/lib/php/Smarty/');
require_once(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty;


3.テンプレート・ファイル index.tpl を作成:
---------------------------------------------------------------------------- >> ここから
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<title>User Info</title>
</head>
<body>

<p>ユーザー情報:</p>
名前:{$name}<br>
URL: <a href="{$url}">{$url}</a><br>
日付:{$smarty.now|date_format:"%Y年%m月%d日"}<br>

</body>
</html>
---------------------------------------------------------------------------- << ここまで

4.サイトへアクセスしてみる。
Web ブラウザで、http://<server-name>/SampleApp/index.php へアクセスし以下の内容が表示
されればOK
--------------------------------------------- >> ここから
ユーザー情報:

名前:Shinta
URL: http://www.my-domain.com/
日付:2007年12月10日
--------------------------------------------- << ここまで


ご注意!!!
このままでは Web ブラウザからテンプレートファイルや設定ファイルが覗かれてしまいます。
そこで、テンプレートファイルや設定ファイルをドキュメントルートの外に配置しましょう。
テンプレートファイルや設定ファイルは、Smarty のライブラリがアクセスできれば良いので
ドキュメントルート内に無くても動作するようになっています。
ただし、キャッシュファイルは Webサーバー(httpd) が作成するのでオーナーとパーミッション
には注意してください。


設定変更例
ドキュメントルート /usr/loca/apache2/htdocs
アプリケーションSampleAppの場所 /usr/loca/apache2/htdocs/SampleApp/
テンプレートを置く場所 /usr/loca/apache2/smarty/
SampleAppの Smarty用の場所 /usr/loca/apache2/smarty/SampleApp
SampleAppのテンプレートを置く場所 /usr/loca/apache2/smarty/SampleApp/template
SampleAppのキャッシュを置く場所 /usr/loca/apache2/smarty/SampleApp/template_c
SampleAppの設定ファイルを置く場所 /usr/loca/apache2/smarty/SampleApp/configs
SampleAppのキャッシュを置く場所 /usr/loca/apache2/smarty/SampleApp/cache

ファイルを以下のように配置:

    ---+--- htdocs/ ---+--- index.html
       |               |
       |               +--- SampleApp/ ---+--- index.php
       |
       +--- smarty/ ---+--- SampleApp/ ---+--- templates/ ------ index.tpl
                                          |
                                          +--- templates_c/
                                          |
                                          +--- configs/
                                          |
                                          +--- cache/

ロジック・ファイル index.php を修正:
<?php
require_once('Smarty/Smarty.class.php');

// create object
$smarty = new Smarty;

// template, cache, configuration files
$smarty->template_dir = '/usr/loca/apache2/smarty/SampleApp/templates/';
$smarty->compile_dir  = '/usr/loca/apache2/smarty/SampleApp/templates_c/';
$smarty->config_dir   = '/usr/loca/apache2/smarty/SampleApp/configs/';
// $smarty->cache_dir = '/usr/loca/apache2/smarty/SampleApp/cache/';

// assign some content.
$smarty->assign('name', 'Shinta');
$smarty->assign('url', 'http://www.my-domain.com/');

// display it
$smarty->display('index.tpl');

?>






SASCAS
最終更新:2008年01月07日 16:39