自定義模板引擎類
MyTpl.class.php
<?phpclass MyTpl{ private $tpl_vars = array(); //分配 public function assign($key,$value){ $this->tpl_vars[$key] = $value; } public function display($tpl){ $contents = file_get_contents($tpl); foreach ($this->tpl_vars as $k => $v){ //替換 將{$name} 替換成真實的數據 $contents = str_replace('{$'."$k".'}',"$v", $contents); $compile = './templates_c/'.md5('show.html') . '.php'; file_put_contents($compile, $contents); require $compile; } }}$tpl = new MyTpl;$tpl-> assign('name','張四');$tpl-> display('./template/show.html');
自定義視圖
template/show.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> {$name} </body> </html>