php中smarty的使用与配置

安装环境
Smarty需要运行PHP 4.0.6以后版本的WEB服务器上
基本安装
复制安装包/libs/子目录下的Smarty库文件。它们是不需要修改的PHP文件。它们可以用于所有程序中,而且仅在你升级新版本的Smarty时更新就可以了。
例2.1 必需的Smarty库文件
Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
/internals/*.php (all of them1)
/plugins/*.php (all of them to be safe, maybe your site only needs a subset2)
注:1、所有的文件。
2、最好是所有的文件,也许你的网站只需要其中的一部分。
Smarty使用一个叫做SMARTY_DIR的PHP常量 [http://php.net/define],它是Smarty的“libs/”文件夹的完整系统路径。基本上说,如果你的程序可以找到Smarty.class.php文件,你就不需要设置SMARTY_DIR,Smarty将会认为那是它本身所在的位置。所以,如果Smarty.class.php不在你的include_path内,或者你不能提供在你程序中的绝对路径,你就必须手工定义SMARTY_DIR。SMARTY_DIR必须以斜线结尾。
这是建立Smarty到你的PHP脚本中的一个例子。
例2.2 建立Smarty的实例
<?php
// NOTE: Smarty has a capital 'S'
// 注意:Smarty有一个大写字母“S”
require_once('Smarty.class.php');
$smarty = new Smarty();
?>
尝试运行上面的脚本。如果你看到Smarty.class.php文件没有找到的错误报告,你不得不用下面的一 种方法:
<?php
// *nix style (note capital 'S')
// *nix系统(注意大写“S”)
define('SMARTY_DIR', '(Smarty 所在路径)/libs/');
// windows style
// windows 系统
define('SMARTY_DIR', '(Smarty 所在路径)/libs/');
// hack version example that works on both *nix and windows
// 替换掉以上两个例子中的版本
// Smarty is assumend to be in 'includes/' dir under current script
// 假定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
// *nix style (note capital 'S')
// *nix系统(注意大写“S”)
require_once('/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php');
// windows style
// windows 系统
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');
$smarty = new Smarty();
?>
<?php
// Edit your php.ini file, add the Smarty library
// directory to the include_path and restart web server.
// then the following should work:
// 编辑你的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服务器的根目录。在我们的例子中,这个目录为/docs/。Smarty目录只能通过Smarty类被存取,而不能直接通过WEB浏览器。因此,为避免任何安全问题,建议将这些文件夹放置在根目录之外
在我们的安装实例中,我们将为一个留言本(guest book)程序设置Smarty环境。我们选择一个程序只是为了目录名称设定的目的。你可以为任何程序使用相同的环境,只需要将“guestbook”替换为你程序的名字。我们把Smarty目录放置在smarty/guestbook/下。
你需要至少有一个文件在你的根目录里,它是一个可以通过WEB浏览器访问到的脚本文件。我们将我们的文件命名为“index.php”,并把它放到根目录下的子目录/guestbook/里
技术要点:设置WEB服务器使得“index.php”能够被作为目录的默认首页是非常方便的,如果你访问http://www.example.com/guestbook/,“index.php”文件将会被执行,而不需要在URL后加上“index.php”。在Apache服务器上,你可以像httpd.conf范例一样追加“index.php”到DirectoryIndex设置中(条目间用空格分割)
让我们看一下目前的文件结构:
libs/Smarty.class.php
libs/Smarty_Compiler.class.php
libs/Config_File.class.php
libs/debug.tpl
libs/internals/*.php
libs/plugins/*.php
guestbook/templates/
guestbook/templates_c/
guestbook/configs/
guestbook/cache/
guestbook/index.php
Smarty需要对$compile_dir和$cache_dir进行写操作,所以必须使WEB服务器用户可以对它们写入(windows用户忽略)。通常是所有者“nobody”和群组“nobody”。对于OS X用户,默认是所有者“www”和群组“www”。如果你使用的是Apache,你可以访问http.conf文件(通常在/usr/local/apache/conf/),检查所有者和群组是否被使用。
我们需要建立“index.tpl”文件,Smarty将会读取它。它将位于$template_dir里。
例2.8 编辑/web/.../index.tpl
{* Smarty *}
Hello {$name}, welcome to Smarty!
技术要点:{*Smarty*}是一个模板注释,并不是必须的,但以这种注释开始的模板文件是一个好习惯。在不注意扩展名的时候,它将使得文件更容易被识别。例如,文本编辑器能识别该文件,并将特别语法高亮显示。
现在让我们来编辑“index.php”。我们将建立一个Smarty实例,分配一个模板变量并显示“index.tpl”文件。
例2.9 编辑 index.php
<?php
// load Smarty library
require_once(SMARTY_DIR . 'Smarty.class.php'); 或是srequire_once(lib/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'guestbook/templates/';
$smarty->compile_dir = 'guestbook/templates_c/';
$smarty->config_dir = 'guestbook/configs/';
$smarty->cache_dir = '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需要运行PHP 4.0.6以后版本的WEB服务器上
基本安装
复制安装包/libs/子目录下的Smarty库文件。它们是不需要修改的PHP文件。它们可以用于所有程序中,而且仅在你升级新版本的Smarty时更新就可以了。
例2.1 必需的Smarty库文件
Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
/internals/*.php (all of them1)
/plugins/*.php (all of them to be safe, maybe your site only needs a subset2)
注:1、所有的文件。
2、最好是所有的文件,也许你的网站只需要其中的一部分。
Smarty使用一个叫做SMARTY_DIR的PHP常量 [http://php.net/define],它是Smarty的“libs/”文件夹的完整系统路径。基本上说,如果你的程序可以找到Smarty.class.php文件,你就不需要设置SMARTY_DIR,Smarty将会认为那是它本身所在的位置。所以,如果Smarty.class.php不在你的include_path内,或者你不能提供在你程序中的绝对路径,你就必须手工定义SMARTY_DIR。SMARTY_DIR必须以斜线结尾。
这是建立Smarty到你的PHP脚本中的一个例子。
例2.2 建立Smarty的实例
<?php
// NOTE: Smarty has a capital 'S'
// 注意:Smarty有一个大写字母“S”
require_once('Smarty.class.php');
$smarty = new Smarty();
?>
尝试运行上面的脚本。如果你看到Smarty.class.php文件没有找到的错误报告,你不得不用下面的一 种方法:
<?php
// *nix style (note capital 'S')
// *nix系统(注意大写“S”)
define('SMARTY_DIR', '(Smarty 所在路径)/libs/');
// windows style
// windows 系统
define('SMARTY_DIR', '(Smarty 所在路径)/libs/');
// hack version example that works on both *nix and windows
// 替换掉以上两个例子中的版本
// Smarty is assumend to be in 'includes/' dir under current script
// 假定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
// *nix style (note capital 'S')
// *nix系统(注意大写“S”)
require_once('/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php');
// windows style
// windows 系统
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');
$smarty = new Smarty();
?>
<?php
// Edit your php.ini file, add the Smarty library
// directory to the include_path and restart web server.
// then the following should work:
// 编辑你的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服务器的根目录。在我们的例子中,这个目录为/docs/。Smarty目录只能通过Smarty类被存取,而不能直接通过WEB浏览器。因此,为避免任何安全问题,建议将这些文件夹放置在根目录之外
在我们的安装实例中,我们将为一个留言本(guest book)程序设置Smarty环境。我们选择一个程序只是为了目录名称设定的目的。你可以为任何程序使用相同的环境,只需要将“guestbook”替换为你程序的名字。我们把Smarty目录放置在smarty/guestbook/下。
你需要至少有一个文件在你的根目录里,它是一个可以通过WEB浏览器访问到的脚本文件。我们将我们的文件命名为“index.php”,并把它放到根目录下的子目录/guestbook/里
技术要点:设置WEB服务器使得“index.php”能够被作为目录的默认首页是非常方便的,如果你访问http://www.example.com/guestbook/,“index.php”文件将会被执行,而不需要在URL后加上“index.php”。在Apache服务器上,你可以像httpd.conf范例一样追加“index.php”到DirectoryIndex设置中(条目间用空格分割)
让我们看一下目前的文件结构:
libs/Smarty.class.php
libs/Smarty_Compiler.class.php
libs/Config_File.class.php
libs/debug.tpl
libs/internals/*.php
libs/plugins/*.php
guestbook/templates/
guestbook/templates_c/
guestbook/configs/
guestbook/cache/
guestbook/index.php
Smarty需要对$compile_dir和$cache_dir进行写操作,所以必须使WEB服务器用户可以对它们写入(windows用户忽略)。通常是所有者“nobody”和群组“nobody”。对于OS X用户,默认是所有者“www”和群组“www”。如果你使用的是Apache,你可以访问http.conf文件(通常在/usr/local/apache/conf/),检查所有者和群组是否被使用。
我们需要建立“index.tpl”文件,Smarty将会读取它。它将位于$template_dir里。
例2.8 编辑/web/.../index.tpl
{* Smarty *}
Hello {$name}, welcome to Smarty!
技术要点:{*Smarty*}是一个模板注释,并不是必须的,但以这种注释开始的模板文件是一个好习惯。在不注意扩展名的时候,它将使得文件更容易被识别。例如,文本编辑器能识别该文件,并将特别语法高亮显示。
现在让我们来编辑“index.php”。我们将建立一个Smarty实例,分配一个模板变量并显示“index.tpl”文件。
例2.9 编辑 index.php
<?php
// load Smarty library
require_once(SMARTY_DIR . 'Smarty.class.php'); 或是srequire_once(lib/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'guestbook/templates/';
$smarty->compile_dir = 'guestbook/templates_c/';
$smarty->config_dir = 'guestbook/configs/';
$smarty->cache_dir = '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!”