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就配置完成了!