更多设置
本篇是基本安装的继续,请先阅读该文!
略微灵活的设置Smarty的方法是扩展该类并初始化你的Smarty环境。因此,不用重复设置目录,给相同的变量赋值,我们可以在一个地方完成这些工作。让我们创建一个新的目录/php/includes/guestbook/,同时创建一个新文件setup.php。在我们的示例环境中,/php/includes在我们的include_path中。请确保你也如此设置或者使用绝对文件路径。
例子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 Smarty_GuestBook() {
// 类的构造
// 对每个新的实例,会自动设置如下的内容
$this->Smarty();
$this->template_dir = '/web/www.example.com/smarty/guestbook/templates/'; $this->compile_dir = '/web/www.example.com/smarty/guestbook/templates_c/'; $this->config_dir = '/web/www.example.com/smarty/guestbook/configs/'; $this->cache_dir = '/web/www.example.com/smarty/guestbook/cache/';
$this->caching = true; $this->assign('app_name', 'Guest Book'); }
} ?>
|
|
让我们修改index.php文件以使用setup.php:
例子2-11. 编辑/web/www.example.com/docs/guestbook/index.php
<?php
require('guestbook/setup.php');
$smarty = new Smarty_GuestBook();
$smarty->assign('name','Ned');
$smarty->display('index.tpl'); ?>
|
|
你看,创建Smarty实例确实很简单,只要使用Smarty_GuestBook()自动初始化我们应用的所有即可。