模板引擎类的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){
return $this->tVar[$name];
}
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $var 模板变量
* @param string $prefix 模板标识前缀
* @param array $vars 模板变量
* @param string $cacheId 模板缓存标识
* @return void
*/
public function display($template,$prefix='',$cacheId='') {
$prefix = $prefix ? $prefix : $this->config['cache_prefix'];
public function display($template,$vars=[],$cacheId='') {
$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)) { // 缓存无效
// 模板编译
$this->compiler(file_get_contents($template),$cacheFile);
@@ -123,7 +123,7 @@ class Template {
ob_start();
ob_implicit_flush(0);
// 读取编译存储
$this->storage->read($cacheFile,$this->tVar);
$this->storage->read($cacheFile,$vars?$vars:$this->tVar);
// 获取并清空缓存
$content = ob_get_clean();
if($cacheId && $this->config['display_cache']) {
@@ -137,19 +137,17 @@ class Template {
* 渲染模板内容
* @access public
* @param string $content 模板内容
* @param array $var 模板变量
* @param string $prefix 模板标识前缀
* @param array $vars 模板变量
* @return void
*/
public function fetch($content,$prefix='') {
$prefix = $prefix ? $prefix : $this->config['cache_prefix'];
$cacheFile = $this->config['cache_path'].$prefix.md5($content).$this->config['cache_suffix'];
public function fetch($content,$vars=[]) {
$cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($content).$this->config['cache_suffix'];
if(!$this->checkCache($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;
class Think {
private $template = null;
public function __construct($config=array()){
$tpl = new Template($config);
$this->template = $tpl;
//$tpl->tpl_path = MODULE_PATH.'view/';
//$tpl->cache_path = MODULE_PATH.'cache/';
public function __construct($config=[]){
$this->template = new Template($config);
}
public function fetch($template,$data=array()){
$this->template->assign($data);
$this->template->display($template);
public function fetch($template,$data=[]){
$this->template->display($template,$data);
}
}