自定义模板引擎类

MyTpl.class.php

<?phpclassMyTpl{private$tpl_vars=array();//分配publicfunctionassign($key,$value){$this->tpl_vars[$key]=$value;}publicfunctiondisplay($tpl){$contents=file_get_contents($tpl);foreach($this->tpl_varsas$k=>$v){//替换将{$name}替换成真实的数据$contents=str_replace('{$'."$k".'}',"$v",$contents);$compile='./templates_c/'.md5('show.html').'.php';file_put_contents($compile,$contents);require$compile;}}}$tpl=newMyTpl;$tpl->assign('name','张四');$tpl->display('./template/show.html');自定义视图

template/show.html

<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>Inserttitlehere</title></head><body>{$name}</body></html>