一个简单的Smarty模板

项目目录:

Smarty.class.php代码:

 1 <?php
 2 class Smarty{
 3     private $template_dir = "./templates";
 4     private $compile_dir = "./templates_c";
 5     private $cache_dir = "./cache";
 6 
 7     public $tpl_var = array();
 8     public $caching = false;
 9 
10     public function __construct(){
11         $this->checkDir();
12     }
13 
14     private function checkDir(){
15         if(!is_dir($this->template_dir)){
16             exit("模板文件目录templates不存在!请手动创建");
17         }
18         if(!is_dir($this->compile_dir)){
19             exit("编译文件目录templates_c不存在!请手动创建");
20         }
21         if(!is_dir($this->cache_dir)){
22             exit("缓存文件目录cache不存在!请手动创建");
23         }
24     }
25 
26     public function assign($tpl_var, $var=null){
27         if(isset($tpl_var) && !empty($tpl_var)){
28             $this->tpl_var[$tpl_var] = $var;
29         }else{
30             exit("模板变量名没有设置好");
31         }
32     }
33 
34     public function display($file){
35         $tpl_file = $this->template_dir.'/'.$file;
36         if(!file_exists($tpl_file)){
37             exit('Error:模板文件不存在!');
38         }
39         $parse_file = $this->compile_dir.'/'.md5($file).'.php';
40 
41         if(!file_exists($parse_file) || filemtime($tpl_file)){
42             include "SmartyCompile.class.php";
43             $compile = new SmartyCompile($tpl_file);
44             $compile->parse($parse_file);
45         }
46 
47         if($this->caching){
48             $cache_file = $this->cache_dir.'/'.md5($file).'.html';
49 
50             if(!file_exists($cache_file) || filemtime($cache_file) < filemtime($parse_file)){
51                 include $parse_file;
52                 $content = ob_get_clean();
53                 if(!file_put_contents($cache_file, $content)){
54                     exit('缓存文件生成出错');
55                 }
56             }
57             include $cache_file;
58         }else{
59             include $parse_file;
60         }
61     }
62 }
63 ?>

SmartyCompile.class.php代码:

 1 <?php
 2 class SmartyCompile{
 3     private $content = '';
 4 
 5     public function __construct($tpl_file){
 6         $this->content = file_get_contents($tpl_file);
 7     }
 8 
 9     public function parseVar(){
10         $pattern = '/{$([w]+)}/';
11         if(preg_match($pattern, $this->content)){
12             $this->content = preg_replace($pattern, '<?php echo $this->tpl_var["$1"]?>', $this->content);
13         }
14     }
15 
16     public function parse($parse_file){
17         $this->parseVar();
18         if(!file_put_contents($parse_file, $this->content)){
19             exit('编译文件生成出错! ');
20         }
21     }
22 }
23 ?>

index.php代码:

 1 <?php
 2     require "Smarty.class.php";
 3 
 4     $smarty = new Smarty();
 5 
 6     $smarty->caching = true;
 7 
 8     $webname = "Smarty测试";
 9     $author = "weixing";
10     $title = "这是一个测试标题";
11     $content = "这是一段测试内容";
12 
13     $smarty->assign('webname', $webname);
14     $smarty->assign('author', $author);
15     $smarty->assign('title', $title);
16     $smarty->assign('content', $content);
17 
18     $smarty->display('index.tpl');
19 ?>

index.tpl代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{$webname}</title>
</head>
<body>
    <div>
        <h3>{$title} </h3>
        <span>作者:{$author}</span>
        <p>内容:{$content}</p>
    </div>
</body>
</html>
原文地址:https://www.cnblogs.com/hell0x/p/5363798.html