PHP Smarty 模板安装与配置

PHP Smarty 模板下载地址:http://smarty.php.net/

当前版本: Smarty-3.1.15

下载以后,解压到你的服务器目录下,拷贝libs文件夹,并且在同以目录下面创建 templates,templates_c,cache三个文件夹。

并且创建 main.php文件,代码如下:

 1 <?php
 2 include_once("libs/Smarty.class.php"); //包含smarty类文件 
 3 
 4 $tpl = new Smarty(); //建立smarty实例对象$smarty 
 5 $tpl->setTemplateDir("./templates"); //设置模板目录 
 6 $tpl->setCompileDir("./templates_c"); //设置编译目录 
 7 $tpl->setCacheDir("./cache"); //缓存目录 
 8 $tpl->cache_lifetime = 0; //缓存时间 
 9 $tpl->caching = true; //缓存方式 
10 
11 $tpl->left_delimiter = "<{"; 
12 $tpl->right_delimiter = "}>"; 
13 ?>

这样配置Smarty就完成了,下面我们创建个例子看看

1、创建test.php文件,代码如下:

 1 <?php
 2 include("main.php");
 3 class index{
 4     
 5     private $tpl;    
 6     function __construct($tpl){
 7       $this->tpl = $tpl;    
 8       }
 9     
10     function __destruct() {
11           unset($this->tpl);
12        }
13     
14     public function main(){
15       $smarty = $this->tpl;
16       $smarty->assign("title","测试");
17        $smarty->assign("hello","Hello World!");
18       $smarty->display("index.tpl");    
19     }
20 }
21 $obj2 = new index($tpl);
22 $obj2->main();
23 ?>

2、在templates文件夹下创建 index.tpl 文件,代码如下:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title><{$title}></title>
 6 </head>
 7 
 8 <body>
 9 <{$hello}>
10 </body>
11 </html>

到这里,整个Smarty就配置完成了!

转载于:https://www.cnblogs.com/dingyan/p/3358835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值