模板引擎类的display和fetch方法改进

This commit is contained in:
thinkphp
2013-03-29 11:52:19 +08:00
parent 8ac5b41c42
commit 9d0f671d2a
2 changed files with 14 additions and 20 deletions

View File

@@ -103,18 +103,18 @@ class Template {
public function get($name){ public function get($name){
return $this->tVar[$name]; return $this->tVar[$name];
} }
/** /**
* 渲染模板文件 * 渲染模板文件
* @access public * @access public
* @param string $template 模板文件 * @param string $template 模板文件
* @param array $var 模板变量 * @param array $vars 模板变量
* @param string $prefix 模板标识前缀 * @param string $cacheId 模板缓存标识
* @return void * @return void
*/ */
public function display($template,$prefix='',$cacheId='') { public function display($template,$vars=[],$cacheId='') {
$prefix = $prefix ? $prefix : $this->config['cache_prefix'];
$template = $this->parseTemplateFile($template); $template = $this->parseTemplateFile($template);
$cacheFile = $this->config['cache_path'].$prefix.md5($template).$this->config['cache_suffix']; $cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($template).$this->config['cache_suffix'];
if(!$this->checkCache($template,$cacheFile)) { // 缓存无效 if(!$this->checkCache($template,$cacheFile)) { // 缓存无效
// 模板编译 // 模板编译
$this->compiler(file_get_contents($template),$cacheFile); $this->compiler(file_get_contents($template),$cacheFile);
@@ -123,7 +123,7 @@ class Template {
ob_start(); ob_start();
ob_implicit_flush(0); ob_implicit_flush(0);
// 读取编译存储 // 读取编译存储
$this->storage->read($cacheFile,$this->tVar); $this->storage->read($cacheFile,$vars?$vars:$this->tVar);
// 获取并清空缓存 // 获取并清空缓存
$content = ob_get_clean(); $content = ob_get_clean();
if($cacheId && $this->config['display_cache']) { if($cacheId && $this->config['display_cache']) {
@@ -137,19 +137,17 @@ class Template {
* 渲染模板内容 * 渲染模板内容
* @access public * @access public
* @param string $content 模板内容 * @param string $content 模板内容
* @param array $var 模板变量 * @param array $vars 模板变量
* @param string $prefix 模板标识前缀
* @return void * @return void
*/ */
public function fetch($content,$prefix='') { public function fetch($content,$vars=[]) {
$prefix = $prefix ? $prefix : $this->config['cache_prefix']; $cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($content).$this->config['cache_suffix'];
$cacheFile = $this->config['cache_path'].$prefix.md5($content).$this->config['cache_suffix'];
if(!$this->checkCache($content,$cacheFile)) { // 缓存无效 if(!$this->checkCache($content,$cacheFile)) { // 缓存无效
// 模板编译 // 模板编译
$this->compiler($content,$cacheFile); $this->compiler($content,$cacheFile);
} }
// 读取编译存储 // 读取编译存储
$this->storage->read($cacheFile,$this->tVar); $this->storage->read($cacheFile,$vars?$vars:$this->tVar);
} }
/** /**

View File

@@ -13,16 +13,12 @@ namespace Think\View\Driver;
use Think\Template; use Think\Template;
class Think { class Think {
private $template = null; private $template = null;
public function __construct($config=array()){ public function __construct($config=[]){
$tpl = new Template($config); $this->template = new Template($config);
$this->template = $tpl;
//$tpl->tpl_path = MODULE_PATH.'view/';
//$tpl->cache_path = MODULE_PATH.'cache/';
} }
public function fetch($template,$data=array()){ public function fetch($template,$data=[]){
$this->template->assign($data); $this->template->display($template,$data);
$this->template->display($template);
} }
} }