just do it

简易smarty

简易smarty类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
class myminismarty
{
	public  $template_dir="./templates/";                  //模板目录
	public  $complie_dir="./templates_c/";                 //编译目录
	public  $tpl_vars=array();                             //传值
	function assign($tpl_var,$val=null)                   //赋值<span id="more-459"></span>
	{
		if ($tpl_var!=null) {
			$this->tpl_vars[$tpl_var]=$val;
		}
	}
 
	function display($tpl_file)                             //编译显示
	{
		$tpl_file_path=$this->template_dir.$tpl_file;      //模板文件
		$complie_file_path=$this->complie_dir."com_".$tpl_file.".php"; //编译文件
 
		if (!file_exists($tpl_file_path)) {
			return false;
		}
		//编译文件不存在则编译,模板修改过则重新编译
		if (!file_exists($complie_file_path)||filemtime($complie_file_path)<filemtime($tpl_file_path)) {
 
 
		$tpl_file_con=file_get_contents($tpl_file_path);
 
		$pattren=array(
		'/\{\s*%([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i'
 
		);
		$replace=array(
		'<?php echo $this->tpl_vars["\1"];?>'
		);
		$newstring=preg_replace($pattren,$replace,$tpl_file_con);
		file_put_contents($complie_file_path,$newstring);
 
		}
		include($complie_file_path);   //显示编译文件
 
	}
 
}

传值页面

1
2
3
4
5
6
7
<?php
require_once('myminismarty.class.php');
$mysmarty=new myminismarty();
$mysmarty->assign("title","hello");
 
$mysmarty->assign("say","hello");
$mysmarty->display("intro.html");

模板页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>{%title}</title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
 
 <body>
  <p>{%title}</p><p>{%say}</p>
 </body>
</html>
点赞