html页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>模板页面</title>
</head>
<body>
<fieldset style="width:800px;margin:0 auto;">
<legend>模板页面</legend>
{$title}
{$content}
{$san}
</fieldset>
</body>
</html>
php页面
// 1、引入迷你MiniSmarty.class.php
require "MiniSmarty.class.php";
// 2、实例化smarty对象
$smarty = new MiniSmarty;
// 3、把字符串信息设置为模板引擎类的属性信息
$smarty -> assign('title','九尾狐标题');
$smarty -> assign('content','九尾狐内容');
$smarty -> assign('san','九尾狐');
// 4、调用compile 方法,同时传递01.html模板文件参数
// 5、在该方法里把01.html 内部标记替换为php标记
$smarty -> display('01.html');
MiniSmarty页面
// 模板引擎类
class MiniSmarty{
public $template_dir="./view/";//模板目录
public $templatec_dir="./view_c/";//编译文件目录
// 给该类声明属性,用于存储外部的变量信息
public $tpl_var = array();
// 把外部变量设置为类内部属性的一部分
function assign($k,$v){
$this-> tpl_var[$k] = $v;
}
function display($tpl){
$n = $this->complie($tpl);
require $n;
}
function complie($tpl){
$con_file = $this->templatec_dir.$tpl.".php";
$tpl_file = $this->template_dir.$tpl;
// 1、判断该混编文件必须存在
// 2、混编文件的修改时间 大于 模板文件的修改时间
if(file_exists($con_file) && filemtime($con_file) > filemtime($tpl_file)){
return $con_file;
}
echo "firse run new file";
// 获取模板文件内部具体的模板内容
$cont = file_get_contents($tpl_file);
$cont = str_replace("{\$","<?php echo \$this->tpl_var['",$cont);
$cont = str_replace("}","'] ; ?> ",$cont);
// 将替换完的内容写入文件中
file_put_contents($con_file,$cont);
// 引入文件
return $con_file;
}
}