Smarty是什么?
为什么使用它?
用例和工作流
语法比较
模板继承
最佳实践
速成教程
安装位于发行版 /libs/ 子目录中的 Smarty 库文件。这些文件是 .php 文件,您不应该编辑它们。它们在所有应用程序中共享,而且只有在您升级到 Smarty 的新版本时才更改。
在以下示例中,Smarty tar 包已解压到
/usr/local/lib/Smarty-v.e.r/(适用于 *nix 机器)
以及 c:\webroot\libs\Smarty-v.e.r\(适用于 Windows 环境)。
示例 2.1. 必需的 Smarty 库文件
Smarty-v.e.r/
libs/
Smarty.class.php
debug.tpl
sysplugins/* (everything)
plugins/* (everything)
Smarty 使用一个名为 SMARTY_DIR 的 PHP 常量,它是 完整的系统文件路径,指向 Smarty libs/ 目录。基本上,如果您的应用程序可以找到 Smarty.class.php 文件,您则不必设置 SMARTY_DIR,因为 Smarty 会自行解决。因此,如果 Smarty.class.php 不在您的 include_path 中,或者您没有在应用程序中提供它的绝对路径,则必须手动定义 SMARTY_DIR。 SMARTY_DIR 必须包含一个尾部斜杠/。
以下是您在 PHP 脚本中创建 Smarty 实例的方式
<?php
// NOTE: Smarty has a capital 'S'
require_once('Smarty.class.php');
$smarty = new Smarty();
?>
尝试运行上述脚本。如果您收到一条错误消息,提示找不到 Smarty.class.php 文件,您需要执行以下操作之一
示例 2.2. 手动设置 SMARTY_DIR 常量
<?php
// *nix style (note capital 'S')
define('SMARTY_DIR', '/usr/local/lib/Smarty-v.e.r/libs/');
// windows style
define('SMARTY_DIR', 'c:/webroot/libs/Smarty-v.e.r/libs/');
// hack version example that works on both *nix and windows
// Smarty is assumend to be in 'includes/' dir under current script
define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-v.e.r/libs/');
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
?>
示例 2.3. 为库文件提供绝对路径
<?php
// *nix style (note capital 'S')
require_once('/usr/local/lib/Smarty-v.e.r/libs/Smarty.class.php');
// windows style
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');
$smarty = new Smarty();
?>
示例 2.4. 将库路径添加到 php.ini 文件中
;;;;;;;;;;;;;;;;;;;;;;;;; ; Paths and Directories ; ;;;;;;;;;;;;;;;;;;;;;;;;; ; *nix: "/path1:/path2" include_path = ".:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/" ; Windows: "\path1;\path2" include_path = ".;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\"
示例 2.5. 使用 ini_set() 在 php 脚本中附加 include 路径
<?php
// *nix
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'/usr/local/lib/Smarty-v.e.r/libs/');
// windows
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'c:/webroot/lib/Smarty-v.e.r/libs/');
?>
库文件现已就位,现在是时候为您的应用程序设置 Smarty 目录了
Smarty 需要四个默认名为 templates/、templates_c/、configs/ 和 cache/ 的目录
其中每个目录可以通过 Smarty 类属性 $template_dir、 $compile_dir、 $config_dir 和 $cache_dir 分别进行定义
强烈建议为每个将使用 Smarty 的应用程序设置一组单独的目录
可以使用 testInstall() 验证您的系统是否为这些目录拥有正确的访问权限。
对于我们的安装示例,我们将为访客簿应用程序设置 Smarty 环境。我们仅为了目录命名约定的目的而选择了应用程序。您可以将相同环境用于任何应用程序,只需将 guestbook/ 替换为您的应用程序名称。
示例 2.6. 文件结构的外观
/usr/local/lib/Smarty-v.e.r/libs/
Smarty.class.php
debug.tpl
sysplugins/*
plugins/*
/web/www.example.com/
guestbook/
templates/
index.tpl
templates_c/
configs/
cache/
htdocs/
index.php
确保您了解 Web 服务器的文档根目录作为文件路径的位置。在以下示例中,文档根目录是 /web/www.example.com/guestbook/htdocs/。Smarty 目录只会由 Smarty 库访问,而永远不会由 Web 浏览器直接访问。因此,为了避免任何安全问题,建议(但不是强制)将这些目录置于 Web 服务器的文档根目录的 外部。
您至少需要在文档根目录下放置一个文件,即 Web 浏览器访问的脚本。我们将脚本命名为 index.php,并将其置于文档根目录 /htdocs/ 下的子目录中。
Smarty 将需要对 $compile_dir 和 $cache_dir 目录(templates_c/ 和 cache/)拥有 写入访问权限(Windows 用户请忽略),因此请确保 Web 服务器用户帐户可以对它们进行写入。
这通常是用户 “nobody” 和组 “nobody”。对于 OS X 用户,默认值为用户 “www” 和组 “www”。如果您使用 Apache,您可以在 httpd.conf 文件中查看正在使用的用户和组。
示例 2.7. 权限和使目录可写
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
chmod 770 /web/www.example.com/guestbook/templates_c/
chown nobody:nobody /web/www.example.com/guestbook/cache/
chmod 770 /web/www.example.com/guestbook/cache/
chmod 770 的安全性相当高,它只允许用户 “nobody” 和群组 “nobody” 读写访问这些目录。如果你希望向任何人开放读取权限(主要用于方便自己查看这些文件),则可以使用 775 代替。
我们需要创建 index.tpl 文件,供 Smarty 显示。该文件需要位于 $template_dir 中。
示例 2.8。/web/www.example.com/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
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir('/web/www.example.com/guestbook/templates/');
$smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
$smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
$smarty->setCacheDir('/web/www.example.com/guestbook/cache/');
$smarty->assign('name','Ned');
//** un-comment the following line to show the debug console
//$smarty->debugging = true;
$smarty->display('index.tpl');
?>
在本例中,我们正在为所有 Smarty 目录设置绝对路径。如果 /web/www.example.com/guestbook/ 在你的 PHP include_path 内,则这些设置不是必需的。然而,将它们设置为绝对路径会更有效率,并且(从经验来看)更不容易出错。这可以确保 Smarty 从你希望的目录中获取文件。
现在使用 Web 浏览器导航到 index.php 文件。你应该看到 “你好 Ned,欢迎来到 Smarty!”
恭喜,你完成了 Smarty 的基本设置!