What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
これは、基本的なインストール の続きです。まず先にこちらから読んで下さい!
Smarty をより柔軟にするセットアップ方法は、 クラスを拡張 してあなたの Smarty の環境を初期化する事です。 ディレクトリパスの設定を同じ変数に何度も割り当てる代わりに、一箇所でそれらを行う事が出来ます。
新しいディレクトリ/php/includes/guestbook/
を作成し、setup.php
という新しいファイルを作成しましょう。
この例の環境では /php/includes
が include_path
です。
例と同じようにするか、あるいは絶対パスを使用して下さい。
Example 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 __construct() { // クラスのコンストラクタ。 // これらは新しいインスタンスで自動的にセットされます。 parent::__construct(); $this->template_dir = '/web/www.example.com/guestbook/templates/'; $this->compile_dir = '/web/www.example.com/guestbook/templates_c/'; $this->config_dir = '/web/www.example.com/guestbook/configs/'; $this->cache_dir = '/web/www.example.com/guestbook/cache/'; $this->caching = Smarty::CACHING_LIFETIME_CURRENT; $this->assign('app_name', 'Guest Book'); } } ?>
では、index.php
ファイルを修正し、
setup.php
を使うようにしてみましょう。
Example 2.11. /web/www.example.com/guestbook/htdocs/index.php
<?php require('guestbook/setup.php'); $smarty = new Smarty_GuestBook(); $smarty->assign('name','Ned'); $smarty->display('index.tpl'); ?>
このように、アプリケーションのために全てを自動的に初期化する
Smarty_GuestBook()
クラスを使う事で、Smarty のインスタンスをとても簡単に作成することができました。