Created
October 29, 2012 16:28
-
-
Save horsley/3974666 to your computer and use it in GitHub Desktop.
PHP Simple Template Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//模板文件路径定义 | |
if (!defined('TPL_ROOT_PATH')) define('TPL_ROOT_PATH', dirname(__FILE__).'/tpl'); | |
class Template { | |
private $_data = array(); | |
private $_tpl_name; | |
function __construct($tpl_name = '') { | |
$this->_tpl_name = $tpl_name; | |
} | |
public function assign($data = array()) { | |
$this->_data = array_merge($this->_data, $data); | |
} | |
public function fetch($tpl_name = '') { | |
if($tpl_name == '' && $this->_tpl_name != '') { | |
$tpl_name = $this->_tpl_name; | |
} | |
$tpl_path = TPL_ROOT_PATH . '/' . $tpl_name . '.php'; | |
if (file_exists($tpl_path)) { | |
extract($this->_data, EXTR_SKIP); | |
ob_start(); | |
include $tpl_path; | |
$content = ob_get_contents(); | |
ob_end_clean(); | |
return $content; | |
} else { | |
trigger_error('模板文件不存在!'); | |
return FALSE; | |
} | |
} | |
public function show($tpl_name = '') { | |
if($content = $this->fetch($tpl_name)) { | |
echo $content; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment