開發自己的模板引擎

自定義模板引擎類

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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章