如果混编文件存在并且混编文件的修改时间大于模板修改时间,则直接引入。否则重新生成
<?php
class Smarty{
private $tpl_var = array();
public function assign($k, $v){
$this->tpl_var[$k] = $v;
}
public function compile($tpl){
$compile_path = $tpl . '.php';
//如果文件存在并且创建混编文件大于模板修改的时间
if(file_exists($compile_path) && filemtime($compile_path) > filemtime($tpl)){
require $compile_path;
} else {
//取出demo.html文档中的内容
$str = file_get_contents($tpl);
//替换定界符的代码,在oop中取值的方法,注意面向过程不同
$str = str_replace('{', '<?php echo $this->tpl_var["', $str);
$str = str_replace('}', '"]; ?>', $str);
//将这个文件写入demo.html.php,demo.html.php为混编文件
file_put_contents($compile_path, $str);
require $compile_path;
}
}
}