Extended Setup

This is a continuation of the basic installation, please read that first!

A slightly more flexible way to setup Smarty is to extend the class and initialize your Smarty environment. So instead of repeatedly setting directory paths, assigning the same vars, etc., we can do that in one place. Lets create a new directory "/php/includes/guestbook/" and make a new file called "setup.php". In our example environment, "/php/includes" is in our include_path. Be sure you set this up too, or use absolute file paths.

我们创建一个目录 "/php/includes/guestbook/" 建立一个文件"setup.php"

Example 2-10. Editing /php/includes/guestbook/setup.php
例2-10.编辑 /php/includes/guestbook/setup.php

// load Smarty library

// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');是一个很好的加载应用程序的类库文件(就是扩展类)

class Smarty_GuestBook extends Smarty {

 function Smarty_GuestBook() {
 		// Class Constructor. These automatically get set with each new instance.


		$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');


Now lets alter the index.php file to use setup.php:

Example 2-11. Editing /web/www.mydomain.com/docs/guestbook/index.php
例 2-11.编辑/web/www.mydomain.com/docs/guestbook/index.php


$smarty = new Smarty_GuestBook;



Now you see it is quite simple to bring up an instance of Smarty, just use Smarty_GuestBook which automatically initializes everything for our application.