Skip to content

Instantly share code, notes, and snippets.

@horsley
Created October 29, 2012 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horsley/3974666 to your computer and use it in GitHub Desktop.
Save horsley/3974666 to your computer and use it in GitHub Desktop.
PHP Simple Template Engine
<?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