Windows下安装 Smarty
前提:服务器和php已经安装好并可以正常运行。
下载 Smarty -
http://smarty.php.net
解压文件, 重命名 Smarty.x.x.x 为 smarty (出于方便,建议不要把文件放在服务器目录下)
例如: d:/smarty
运行 phpinfo.php 找到 php.ini 文件的位置
编辑 php.ini的 include_path ,加入smarty的libs的路径。
例如: include_path = ".;d:/smarty/libs"
重启 IIS/Apache
在服务器目录下创建下面两个文件夹:
(wwwroot)/smarty/templates (模板以后放在这里)
(wwwroot)/smarty/configs
在非服务器目录下创建下面两个文件夹:
d:/smarty/templates_c
d:/smarty/cache (这个我目前根据本文的测试,没有用)
设置系统,保证这四个文件是可写的。
在 (wwwroot) 建立 index.php 然后建立 (wwwroot)/smarty/templates/index.tpl ,代码如下:
index.php:
<?php
// load Smarty library
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = '
d:/inetpub/wwwroot/
smarty/templates';
$smarty->config_dir = '
d:/inetpub/wwwroot/
smarty/config';
$smarty->cache_dir = 'd:/smarty/cache';
$smarty->compile_dir = 'd:/smarty/templates_c';
$smarty->assign('name','fish boy!');
$smarty->display('index.tpl');
?>
index.tpl
<html>
<body>
Hello, {$name}!
</body>
</html>
现在在浏览器中打开 index.php
上面是一个简单的例子,下面建立一个使用class的例子:
smarty_connect.php:
<?php
// load Smarty library
require('Smarty.class.php');
class smarty_connect extends Smarty
{
function smarty_connect()
{
// Class Constructor.
// These automatically get set with each new instance.
$this->Smarty();
$this->template_dir = '
d:/inetpub/wwwroot/
smarty/templates';
$this->config_dir = '
d:/inetpub/wwwroot/
smarty/config';
$this->compile_dir = 'd:/smarty/templates_c';
$this->cache_dir = 'd:/smarty/cache';
$this->assign('app_name', 'Intranet');
}
}
?>
index.php:
<?php
require('smarty_connect.php');
$smarty = new smarty_connect;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
?>
index.tpl:
<html>
<body>
Hello, {$name}!
</body>
</html>
如果出现错误,请察看php.ini文件是否有错误,include是否正确。
注:我自己是把
config templates templates_c
都放到服务器目录下了,而且没有建立
cache
文件夹,程序正常运行。