Smartyインストール(簡単編)

sasaki 土曜, 2007-09-29 18:38

Smartyのインストール。基本的には、libsに置くが、root管理者ではない場合や、色々なサーバで同じPHPプログラムを利用したいときや、とりあえずテストしたいときにもこの方法が早くて楽ちん。

 

■Smartyをダウンロードする

ダウンロード先:

http://smarty.php.net/download.php

 

 

■Smartyライブラリファイルを設置する

ポータビリティ(持ち運びなど)を考えて、自分のホームディレクトリにとりあえず置く。 

/home/foo/Smarty-バージョン番号 というように設置する。

# cd $HOME
# tar zxvf Smarty-2.6.18.tar.gz

 

 

■Smartyの指定方法は2つ

  • PHPファイルごとに/usr/local/lib/Smarty-2.6.18/ というように絶対パスを指定する。
  • PHPの設定ファイルphp.iniで指定する。

 今回は、とにかくすぐにSmartyを試せるので、絶対パスを指定する。特にデメリットも無い。

 

 

■自分のアプリケーション内にSmarty用のディレクトリをセットアップする

Smartyを使用する各アプリケーションごとに4つのディレクトリが必要。

  • templates/
  • templates_c/
  • configs/
  • cache/

ルートドキュメントの外に設置することが推奨されているので、

/home/foo/public_html/index.php
/home/foo/templates/
/home/foo/templates_c/
/home/foo/configs/
/home/foo/cache/

となるようにディレクトリをmkdirコマンドで作成しておく。

 

templates_c/ と cache/ の2つのディレクトリはApache実行ユーザが「書き込み権限でアクセスする必要」がある。

# chmod 777 /home/foo/template_c
# chmod 777 /home/foo/cache

 

■テンプレートを設置する

試しにテンプレートを作成する。{* Smarty *}はコメント。

/home/foo/templates/index.tml

{* Smarty *}
こんにちは、{$name}。ようこそ Smarty へ!

 

このテンプレートを使った index.php を編集する。

/home/foo/public_html/index.php

<?php

require_once("/home/foo/Smarty.class.php");

$smarty = new Smarty();
$smarty->template_dir = '/home/satow/www/templates/';
$smarty->compile_dir = '/home/satow/www/templates_c/';
$smarty->config_dir = '/home/satow/www/configs/';
$smarty->cache_dir = '/home/satow/www/cache/';

$smarty->assign('name', 'Ned');

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

?>

 

ブラウザからindex.phpにアクセスすると、このように表示されるはず。

こんにちは、Ned。ようこそ Smarty へ!