smarty的简明安装配置

一. 安装 

 下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来演示给大家一个安装实例,看过应该会举一反三的。  

(1) 在根目录(你的网站目录)下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, config/.  

注意:templates_c/的权限为777

(2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下:

php代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01
 <html>
 <head>
 <metahttp-equiv="Content-Type" content="text/html;charset=gb2312">
 <title>Smarty</title></head>
 <body>{$hello}</body>
 </html> 



 新建index.php,将此文件放在learn/下:

Php代码

<?php
require 'smarty/Smarty.class.php';
$smarty = new Smarty;//设置各个目录的路径,这里是安装的重点
$smarty->template_dir ="smarty/templates/templates";
$smarty->compile_dir ="smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir ="smarty/templates/cache";
//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching = false;
$hello = "Hello World!";//赋值
$smarty->assign("hello",$hello);//引用模板文件
$smarty->display('index.tpl');
?>


 //注意,若是报有”Parse error: syntax error, unexpected T_STRING in D:\ProgramFiles\AppServ\www\learn\index.php on line 2”的错误,检查每一行的开头是否有空格,删掉后重新执行index.php。

  (3) 执行index.php就能看到Hello World!了。

 

 

如果必须得使用绝对路径,则是因为服务器不会知道文件在第几层目录中被调用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值