Skip to content

入门指南

要求

Smarty 可以在 PHP 7.1 到 PHP 8.2 环境下运行。

安装

Smarty 可以通过Composer进行安装。

获取 Smarty 的最新稳定版本,可以使用:

composer require smarty/smarty

获取 Smarty 的最新未发布版本,可以使用:

composer require smarty/smarty:dev-master

获取 Smarty 的前一个稳定版本,即 Smarty 3,可以使用:

composer require smarty/smarty:^3

以下是如何在你的 PHP 脚本中创建一个 Smarty 实例:

<?php

require 'vendor/autoload.php';
$smarty = new Smarty();

现在库文件已经就位,是时候为你的应用程序设置 Smarty 目录了。

Smarty 需要四个目录,它们默认命名为 templatesconfigstemplates_c 以及 cache, 这些都相对于当前工作目录。

默认设置可以按如下方式更改:

$smarty = new Smarty();
$smarty->setTemplateDir('/some/template/dir');
$smarty->setConfigDir('/some/config/dir');
$smarty->setCompileDir('/some/compile/dir');
$smarty->setCacheDir('/some/cache/dir');

编译目录和缓存目录需要对运行 PHP 脚本的用户具有写权限。

注意

通常,用户和组都是"nobody"。对于 OS X 用户,默认的用户和组是"www"。如果你正在使用 Apache,你可以在你的httpd.conf文件中查看正在使用的用户和组。

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/

你可以通过testInstall()来验证你的系统是否为这些目录设置了正确的访问权限:

$smarty = new Smarty();
$smarty->setTemplateDir('/some/template/dir');
$smarty->setConfigDir('/some/config/dir');
$smarty->setCompileDir('/some/compile/dir');
$smarty->setCacheDir('/some/cache/dir');
$smarty->testInstall();

现在,让我们创建 Smarty 将要显示的index.tpl文件。这需要位于$template_dir

{* Smarty *}
Hello {$name}, welcome to Smarty!

注意

{* Smarty *}是一个模板注释。它不是必需的,但是在所有的模板文件中都以这个注释开始是一种良好的实践。它使得文件容易被识别,无论文件扩展名是什么。例如,文本编辑器可以识别文件并打开特殊的语法高亮。

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

<?php

require 'vendor/autoload.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');
$smarty->display('index.tpl');

注意

在我们的例子中,我们设置了所有 Smarty 目录的绝对路径。如果/web/www.example.com/guestbook/在你的 PHP include_path 中,则这些设置不是必需的。然而,设置它们为绝对路径更有效率,也更不容易出错。这确保 Smarty 从你预期的目录中获取文件。

现在,运行你的 PHP 文件。你应该会看到 "Hello Ned, welcome to Smarty!"

你已经完成了 Smarty 的基本设置!

扩展设置

这是对基本安装的延续,请先阅读那部分!

设置 Smarty 的一种稍微灵活一些的方法是扩展 Smarty 类并初始化你的 Smarty 环境。所以,我们可以在一个地方设置目录路径,分配相同的变量等,而不是反复地这样做。

<?php

class Smarty_GuestBook extends Smarty {

   public function __construct()
   {
        parent::__construct();

        $this->setTemplateDir('/web/www.example.com/guestbook/templates/');
        $this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
        $this->setConfigDir('/web/www.example.com/guestbook/configs/');
        $this->setCacheDir('/web/www.example.com/guestbook/cache/');

        $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
        $this->assign('app_name', 'Guest Book');
   }

}

现在,我们可以在我们的脚本中使用Smarty_GuestBook代替Smarty

<?php
$smarty = new Smarty_GuestBook();
$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');