跳至内容

开始使用

要求

Smarty 可使用 PHP 7.2 到 PHP 8.3 运行。

安装

Smarty 可通过 Composer 安装。

若要获取 Smarty 的最新稳定版本,请使用

composer require smarty/smarty

若要获取最新的尚未发布版本,请使用

composer require smarty/smarty:dev-master

若要获取 Smarty 的先前的稳定版本 Smarty 4,请使用

composer require smarty/smarty:^4

下面介绍如何在 PHP 脚本中创建 Smarty 实例

<?php

// Instantiated via composer
require 'vendor/autoload.php';
use Smarty\Smarty;
$smarty = new Smarty();

// or ...

// Instantiated directly
require("/path/to/smarty/libs/Smarty.class.php");
use Smarty\Smarty;
$smarty = new Smarty();

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

Smarty 需要四个目录,它们相对于当前工作目录的默认名称为 templatesconfigstemplates_ccache

默认值可以如下更改

<?php
use Smarty\Smarty;
$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”用户和“nobody”组。对于 OS X 用户,默认值为“www”用户和“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() 验证你的系统是否对这些目录具有正确的访问权限

<?php
use Smarty\Smarty;
$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 *}
<h1>Hello {$name|escape}, welcome to Smarty!</h1>

注意

{* Smarty *}是模板注释。这不是必需的,但所有模板文件都以这个注释开头是个好习惯。它使文件易于识别,无论文件扩展名如何。例如,文本编辑器可以识别文件并打开特定的语法高亮。

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

<?php

require 'vendor/autoload.php';

use Smarty\Smarty;
$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 的基本设置!

转义

您可能注意到上面的示例模板使用escape 修改器渲染 `$name` 变量。该修改器可在 HTML 页面上下文中将字符串变为“安全”字符串。

如果您主要将 Smarty 用于 HTML 页面,建议启用自动转义。这样,您无需在网页中使用的每个变量中都添加 |escape。Smarty 会自动为您处理!

为 HTML 启用自动转义,如下所示

$smarty->setEscapeHtml(true);

扩展设置

这是一个基本安装,请先阅读它!

设置 Smarty 的一种更灵活的方式是扩展 Smarty 类并初始化您的 Smarty 环境。所以,我们可以在一个地方设置目录路径、分配相同的变量等等,而不是反复设置。

<?php

use Smarty\Smarty;

class My_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->setEscapeHtml(true);

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

}

现在,我们可以在脚本中使用 My_GuestBook 而不是 Smarty

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