Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

基础安装

在Smarty发行包中,目录/libs/ 里面的文件就是Smarty的库文件。 库文件都是.php文件,开发者不应该编辑它们。 库文件可以多个应用程序共用,而且只在升级新版本的Smarty时进行覆盖。

下面的例子Smarty的库文件被解压:

  • /usr/local/lib/Smarty-v.e.r/ 在 *nix 的环境

  • 以及 c:\webroot\libs\Smarty-v.e.r\ 在 windows 环境.

Example 2.1. 载入Smarty库文件


Smarty-v.e.r/
   libs/
      Smarty.class.php
      debug.tpl
      sysplugins/* (everything)
      plugins/*    (everything)

    

Smarty的SMARTY_DIR 常量,是定义 Smarty库文件libs/目录的 完整系统路径。 一般来说,如果你的程序可以找到Smarty.class.php文件, 那么你不需要设置SMARTY_DIR, Smarty会自行进行赋值。 然而,如果Smarty.class.php文件不在你的 include_path内, 或者你不能在程序中使用绝对路径的时候,那么你需要定义SMARTY_DIRSMARTY_DIR 必须以斜杠(/)结尾。.

在PHP中实例化Smarty对象的方法:


<?php
// NOTE: Smarty has a capital 'S'
require_once('Smarty.class.php');
$smarty = new Smarty();
?>

    

运行上面的程序。如果发生错误提示说找不到Smarty.class.php文件, 那么你需要按下面的其中一种方法来处理:

Example 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();
?>

    

Example 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();
?>

    

Example 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\"


Example 2.5. 通过函数ini_set() 来设置include path


<?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是否有权限读写这些目录。

在下面的安装例子中,我们将为一个留言本程序建立Smarty环境。 我们提供了一个目录命名约定的例子。 你可以为任何的程序建立同样的环境,仅需要修改guestbook/名称。

Example 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服务器文档根目录之外

将会有至少一个文件是放到文档根目录的,这个文件也会被浏览器访问到。 我们将这文件命名为index.php, 放置到文档根目录/htdocs/中。

Smarty需要一些对目录的 读写权限 (windows用户请忽略),包括 $compile_dir $cache_dir 目录 (templates_c/cache/), 所以,要确保web服务器用户有权限读写它们。

Note

通常是用户nobody 和组nobody. OS X用户,默认用户是www和组www. 如果你使用Apache,你可以看看你的httpd.conf 来确定是使用什么用户和组的。

Example 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目录内。

Example 2.8. /web/www.example.com/guestbook/templates/index.tpl


{* Smarty *}

Hello {$name}, welcome to Smarty!

    

技术说明

{* Smarty *}是模板的 注释. 虽然不是必须的,但在模板内添加注释这是个很好的习惯。 它可以帮助识别出文件类型,而不需要看后缀。 比如说,代码编辑器可以识别该文件并自动语法高亮。

现在,我们来修改index.php. 我们将创建Smarty的实例,给模板assign() 赋值变量, 并且display()显示该模板文件 index.tpl

Example 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的目录路径被设置成正确的。

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

你已经完成了Smarty的基础安装!

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors