在Windows上安装PHP模板引擎Smarty

安装Smarty

下载页面:http://www.smarty.net/download.php,下载Latest Stable Release,我选择的版本是2.6.26

解压到D:\php\smarty\,这个是我的安装路径,可以根据自己的需要更改,但是注意以下有这个路径的都需要修改

配置Smarty

修改PHP安装目录下的php.ini,找到如下两行所在的位置:

1
2
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"

修改为:

1
2
; Windows: "\path1;\path2"
include_path = ".;D:\php\smarty\libs"

重启Apache或者IIS

在你的网站目录(我网站的路径为D:/php/workspace/happylink,你同样需要根据实际情况修改)下新建两个文件夹
D:/php/workspace/happylink/smarty/templates
D:/php/workspace/happylink/smarty/configs

在smarty的安装目录(即d:/php/smarty)下新建两个文件夹:
d:/php/smarty/templdates_c
d:/php/smarty/cache

在网站目录(即D:/php/workspace/happylink)下创建index.php,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
// load Smarty library
require('Smarty.class.php');
 
// change the following 2 lines
define('WEB_ROOT', 'D:/php/workspace/happylink');
define('SMARTY_ROOT', 'D:/php/smarty');
 
$smarty = new Smarty;
$smarty->template_dir = WEB_ROOT.'/smarty/templates';
$smarty->config_dir = WEB_ROOT.'/smarty/config';
$smarty->cache_dir = SMARTY_ROOT.'/cache';
$smarty->compile_dir = SMARTY_ROOT.'/templates_c';
 
$smarty->assign('name','fish boy!');
$smarty->display('index.tpl');
?>

在D:/php/workspace/happylink/smarty/templates目录下建立index.tpl,代码如下:

1
2
3
4
5
<html>
    <body>
    Hello, {$name}!
    </body>
</html>

访问http://localhost/happy/index.php,即可以看到显示了“Hello, fish boy!! ”的页面,到目前为止smarty就安装和配置好了。

如果出现如下的警告信息:

Warning: strftime() [function.strftime]: It is not safe to rely on the system's timezone settings. You are
*required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used
any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
We selected 'UTC' for '8.0/no DST' instead in D:\php\smarty\libs\Smarty_Compiler.class.php on line 400

第一种解决办法是修改php.ini,把"date.timezone="取消注释(即把前面的分号去掉)并赋值为PRC,保存后重启Apache或者IIS就行了。

第二种是在index.php的开头添加设置默认时区的代码即可:
1
2
3
< ?php
date_default_timezone_set("PRC");
?>

你可能对下面的文章感兴趣

  1. PHP获取本周所有天的日期

4 thoughts on “在Windows上安装PHP模板引擎Smarty

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">