基本安装

    安装分发包中/libs/子目录下的Smarty库文件。这些PHP文件你不应该加以编辑。它们为所有应用共享,只有在你升级到新版本的Smarty时这些文件才得以更新。

例子2-1. 必需的Smarty库文件

Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
/internals/*.php (所有的)
/plugins/*.php (安全起见需要全部,但是你的站点可能只要一部分)

    Smarty使用PHP常量SMARTY_DIR,指向Smartylibs/目录的完整系统路径。一般而言,如果你的应用可以找到Smarty.class.php文件,你不需要设置SMARTY_DIR因为Smarty会自动发现。因此,如果Smarty.class.php不在你的include_path中又或者在你的应用中你并未提供一个指向它的完整路径,你才必须手动定义SMARTY_DIRSMARTY_DIR必须以/结尾。

例子2-2. 创建Smarty实例

    在你的PHP脚本中,你应该这样创建一个Smarty的实例:

<?php
// 注意:Smarty的S是大写的
require_once('Smarty.class.php');
$smarty = new Smarty();
?>

    试着运行上述脚本。如果你得到一个错误说Smarty.class.php文件无法找到,你应该做如下的动作之一:

例子2-3. 手动设置SMARTY_DIR常量

<?php
// *nix样式(注意大写S)
define('SMARTY_DIR''/usr/local/lib/php/Smarty-v.e.r/libs/');

// Windows样式
define('SMARTY_DIR''c:/webroot/libs/Smarty-v.e.r/libs/');

// 黑客版本,在*nix和Windows下都可以用
// 假定Smarty存放在当前脚本的'includes/'目录中
define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-v.e.r/libs/');

require_once(
SMARTY_DIR 'Smarty.class.php');
$smarty = new Smarty();
?>

例子2-4. 提供库文件的绝对路径

<?php
// *ni样式(注意大写S)
require_once('/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php');

// Windows样式
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');

$smarty = new Smarty();
?>

例子2-5. 在PHP的include_path中增加库目录

<?php
// 编辑你的php.ini文件,加入Smarty库目录
// 于include_path中,并重新启动WEB服务器
// 以下代码应该可以工作:
require_once('Smarty.class.php');
$smarty = new Smarty();
?>

    既然库文件已经找到,现在可以设置应用的Smarty目录。

    Smarty需要四个目录,缺省时命名为:'templates/','templates_c/','configs/'以及'cache/'。

    它们都可以通过Smarty类属性定义,分别是:$template_dir$compile_dir$config_dir以及$cache_dir。我们强烈建议,对于每个使用到Smarty的应用,你要设置一套独立的目录。

    要确保你知道你WEB服务器的文档根目录。在我们的例子中,文档根目录是/web/www.example.com/docs/。Smarty目录只由Smarty库访问而从不会被WEB浏览器直接访问。因此,为了避免任何安全方面的顾虑,建议将这些目录放置在文档根目录之外

    在我们的安装例子中,我们将为一个访客留言本应用设置Smarty环境。我们选择一个应用只是为了目录命名约定的目的。你可以在任何应用中使用相同的环境,只要将"guestbook"替换为你的应用名即可。我们将我们的Smarty目录放置在/web/www.example.com/smarty/guestbook/之下。

    在文档根目录下,你至少需要一个文件,即WEB浏览器要访问的脚本。我们将其称为index.php,并放置在文档根目录的/guest子目录下。

技术注解:将WEB服务器设置为将index.php作为缺省目录索引是比较方便的。这样的话,你可以访问http://www.example.com/guestbook/,而将执行index.php脚本而不需要在URL后加入index.php。Apache中,可以通过在DirectoryIndex设置后加入index.php而实现(每个项以空格分割),如下面的httpd.conf例子:

DirectoryIndex index.htm index.html index.cgi index.php

    让我们来看以下目前为止的文件结构:

例子2-6. 目前的文件结构

/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php
/usr/local/lib/php/Smarty-v.e.r/libs/Smarty_Compiler.class.php
/usr/local/lib/php/Smarty-v.e.r/libs/Config_File.class.php
/usr/local/lib/php/Smarty-v.e.r/libs/debug.tpl
/usr/local/lib/php/Smarty-v.e.r/libs/internals/*.php
/usr/local/lib/php/Smarty-v.e.r/libs/plugins/*.php

/web/www.example.com/smarty/guestbook/templates/
/web/www.example.com/smarty/guestbook/templates_c/
/web/www.example.com/smarty/guestbook/configs/
/web/www.example.com/smarty/guestbook/cache/

/web/www.example.com/docs/guestbook/index.php

    Smarty需要写权限 (Windows用户可以忽略这个)于$compile_dir$cache_dir,因此请保证WEB服务器用户可以在其中写。通常是用户"nobody"和组"nobody"。对于OS X用户,缺省的是用户"www"和组"www"。如果你使用Apache,你可以查看你的httpd.conf而知道使用的是什么用户和组。

例子2-7. 设置文件权限

chown nobody:nobody /web/www.example.com/smarty/guestbook/templates_c/
chmod 770 /web/www.example.com/smarty/guestbook/templates_c/

chown nobody:nobody /web/www.example.com/smarty/guestbook/cache/
chmod 770 /web/www.example.com/smarty/guestbook/cache/

注意:chmod 770是非常严格的安全性,它只允许用户"nobody"和组"nobody"读/写这些目录。如果你想对所有人打开读权限(主要是为了你自己浏览这些文件时的方便),你可以改用775。

    我们需要创建Smarty将调用的index.tpl文件。它应该存放在$template_dir指定的目录下。

例子2-8. 我们的/web/www.example.com/smarty/guestbook/templates/index.tpl

{* Smarty *}

Hello {$name}, welcome to Smarty!

技术注解:{* Smarty *}是模板注释。它不是必需的,但是用该注释开始你所有的模板文件是个很好的做法。它使得文件容易被识别而不论其扩展名是什么。例如,文本编辑器可以识别该文件并打开特别的语法高亮设置。

    现在开始编辑index.php。我们将创建一个Smarty实例,assign()一个模板变量并display()index.tpl文件。

例子2-9. 编辑/web/www.example.com/docs/guestbook/index.php

<?php

// 调用Smarty库
require_once(SMARTY_DIR 'Smarty.class.php');

$smarty = new Smarty();

$smarty->template_dir '/web/www.example.com/smarty/guestbook/templates/';
$smarty->compile_dir '/web/www.example.com/smarty/guestbook/templates_c/';
$smarty->config_dir '/web/www.example.com/smarty/guestbook/configs/';
$smarty->cache_dir '/web/www.example.com/smarty/guestbook/cache/';

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

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

技术注解:我们的例子中,我们为所有的Smarty目录设置了绝对路径。如果/web/www.example.com/smarty/guestbook/在你的PHP的include_path中,那么这些设置不是必需的。但是,设置为绝对路径会更有效以及(根据经验)更不容易出错。这将保证Smarty从你确定的目录中获得文件。

    现在可以在WEB浏览器中访问index.php文件。你应该看到"Hello Ned, welcome to Smarty!"

    你完成了Smarty的基本设置!